- hold and send the conversation
- stay inside the model’s context window
- trim or summarise long conversations
- switch models partway through a conversation
- keep each turn affordable
Hold the history in your app
On every Gloo AI generation endpoint, your app holds the conversation and sends it with each request: ininput on the Responses endpoints, in messages on Chat Completions. Instructions that apply to the whole conversation go in instructions (Responses) or a system message (Chat Completions), and you send them each time too.
- Don’t rely on server-side state. Gloo doesn’t store your conversations:
previous_response_id,conversationandstoredon’t keep earlier turns on Gloo’s Responses endpoints, and on some models sending them can make the request fail. Leave them out and send the earlier turns ininput. See Moving offencrypted_contentandprevious_response_id. - Store each conversation durably with the turns as you sent them and as the model returned them, so a user can come back to it and you can investigate problems later.
- Drop a question that got no answer. If a request fails, don’t keep the unanswered turn in the history, or the next request sends it twice.
- Send output items back as you received them on the Responses endpoints, including tool calls and their results. Reasoning from earlier turns isn’t carried forward: the model reasons afresh each turn. See Reasoning across turns.
Stay inside the context window
Each model’scontext_window and max_input_tokens are in the model catalog. Everything you send counts toward the input limit: instructions, every earlier turn, documents, images and tool definitions. The answer needs room too, up to the output limit you set.
- Track the size of each conversation. The
usageon each response tells you how many input tokens the last request used (input_tokenson Responses,prompt_tokenson Chat Completions). The next request is roughly that, plus the answer, plus the new message. - Set your own budget below the model’s limit. Trimming at, say, half of
max_input_tokenskeeps requests fast and affordable and leaves room for long messages. - Handle the error anyway. A request bigger than the model’s window fails with
400and the codecontext_length_exceeded(1002 / CONTEXT_LENGTH_EXCEEDED). It isn’t retryable: shorten the conversation, or move to a model with a larger window, before you send it again. The error body includes the model’smax_input_tokensand asuggestionnaming a model with a larger window. See the error code reference.
Trim or summarise long conversations
Pick a strategy before your first long conversation, not after the first error.- Trim from the oldest end. Keep the instructions and the most recent turns; drop older ones until the conversation fits your budget. Trim by size as well as count, because a few long answers can outweigh many short ones.
- Keep tool calls with their results. A
function_calland itsfunction_call_outputshare acall_idand belong together: if you drop one, drop the other. - Summarise what you drop. For long-running conversations, have a small, fast model summarise the dropped turns into a short note, and send that note at the start of the history. Mark it as a summary so the model doesn’t treat it as something the user just said.
- Move lasting facts into the instructions. A user’s name, a choice they made or the document you’re discussing shouldn’t depend on an old turn surviving the trim.
usage.input_tokens for your own traffic:
trim raises an error instead of returning a history that would still be too long: catch it and shorten or summarise that item. Send your instructions separately, in instructions, so trimming never removes them. Character counts are only an estimate, and images count very differently from text, so leave a margin.
Switch models with care
Routing a conversation between models, for cost or capability, is common. Check that the next model can take the whole conversation as it stands:- Context window. A conversation that fits a 1,000,000-token model may not fit a smaller one. Trim to the smaller model’s budget before you switch.
- Images. A conversation that holds an image can only go to a model with
supports_vision: true, or you can describe the image once and send the description instead. See Switching models mid-conversation. - Tools. A conversation with tool calls in it needs a model that supports tools if you still send
tools. - Replacements. When a model you use is deprecated, its requests can be served by a replacement with a smaller context window;
deprecation_notesays so when that happens. See Choosing, Pinning and Updating Models. - Format. Keep history in plain messages and standard output items, not provider-specific blocks, so any model can read it.
Keep each turn affordable
Because the whole conversation is sent each turn, every turn bills all the turns before it as input again.- Append, don’t rewrite. Add new turns at the end and leave earlier ones exactly as they were. The unchanged start of the conversation can then be read from the prompt cache at a lower rate. Rewriting or reordering earlier turns breaks the cache.
- Put stable content first: instructions, then reference documents, then the conversation, then the new question.
- Trim earlier rather than later. A smaller budget costs less on every turn, not just the long ones.
- Shrink images or describe them once. An image is billed as input tokens on every turn it stays in the history.
Summary
- Your app sends the whole conversation every turn; Gloo doesn’t keep it, so don’t rely on
previous_response_id. - Track size from
usage, trim to your own budget belowmax_input_tokens, and handlecontext_length_exceededwithout retrying. - Trim from the oldest end, keep tool calls with their results, and summarise what you drop.
- Before switching models, check the context window, vision and tool support.
- Append new turns without rewriting old ones, so the start of the conversation stays cacheable.
Next steps
Conversations That Remember
The lesson: history, context windows and trimming, with code.
Images in Conversations
Keep conversations with images working across models.
Direct Responses
How earlier turns and reasoning items are handled.
Controlling Cost
Caching, trimming and the other levers on your bill.

