Skip to main content

Choose who owns the state

Caching reuses input computation without enlarging the model’s context window. A Responses state ID does not bypass capacity limits either. Reserve output and required reasoning capacity, including tool definitions, media, and protocol overhead. Characters, UTF-8 bytes, and message counts are not token counts. See coverage for counting-endpoint boundaries; do not assume a usable general token-counting API exists.

Automatic truncation and context editing boundaries

The native Responses DTO accepts truncation and context_management; upstream support and semantics depend on the model and channel. Current Responses → Chat conversion rejects stateful fields including context_management and previous_response_id, and has no mapping copying truncation. Do not rely on automatic trimming after conversion. Claude context_management is a protocol-specific object; Claude → Responses conversion does not copy it verbatim. OpenRouter’s transforms: ["middle-out"] is also outside this site’s verified general contract. Start with the existing explicit compaction example when compression is needed. Identically named fields across protocols do not establish equivalent implementations.

Runnable: a local window of complete text turns

This example handles plain-text Chat history without tools, media, or reasoning state. It keeps system and the latest question, removing oldest complete user / assistant pairs to fit a turn limit and a UTF-8 limit on serialized messages. It neither estimates tokens nor guarantees a fit within the model’s window, and does not call a summarization model. Create history.json:
Use the REST runtime, keeping examples/context-window.mjs and http-client.mjs together:
The input file is limited to 128 KiB; KEEP_TURNS ranges from 0–20 and MAX_BYTES from 128–65536. These are local example limits, not server or model limits. Unknown input fields, incomplete text turns, invalid UTF-8 / JSON, or required instructions and the latest question exceeding the budget fail before a request. Strings are never truncated. Success output contains the answer, retained / omitted turn counts, and sent message bytes. The original file remains intact and failures are not retried automatically. Omitting history changes semantics. Keep indispensable business constraints in trusted persistent state rather than assuming an older conversation stays within the window.

Tools, summaries, and reasoning state

Keep tool calls and results together rather than deleting one side. Reasoning ciphertext, native signatures, and compaction items are protocol state and cannot be handled by this text-trimming script. Verify unchanged replay using the reasoning example, or preserve the complete returned window with the compaction example. If the application generates summaries, retain facts, user constraints, pending tasks, and source references, and establish which details the business allows it to discard. Summaries are not lossless compression. Do not automatically promote user content or tool output to system instructions. Keep original history if compaction fails, then validate before updating session state. Context-limit errors should not trigger uncontrolled regeneration. Sources: revision 30a6d88a4, Responses / Claude DTOs, relaykit/relayconvert/internal/oai_responses/to_oai_chat_req.go, and relaykit/relayconvert/internal/claude_messages/to_oai_responses_req.go. References: OpenRouter message transforms and OpenAI conversation state. Conversations · Caching · Responses API 阅读中文