Submit a toolSubmit

OpenAI returned context_length_exceeded. Shrink input or compact

Retrying the same oversized prompt will fail with `context_length_exceeded`. Shrink the input, compact, or switch to a larger model. The prompt (and required output room) no longer fits the model’s context window: cut or summarize older turns, drop oversized tool results, or use Responses compaction so the next turn starts from a smaller window. Short answer: 5 steps. 1) Read the error as a size problem, not a transient blip; 2) Check the model’s maximum context length; 3) Shrink what you send on the next request; 4) For long Responses runs, enable compaction before you overflow; 5) Continue with a smaller window or a larger model.

Last updated: · This page is the decision checklist; the tools list for “Handle OpenAI context_length_exceeded” is a separate URL.

The problem: Retrying the same oversized prompt will fail with `context_length_exceeded`. Shrink the input, compact, or switch to a larger model. The prompt (and required output room) no longer fits the model’s context window: cut or summarize older turns, drop oversized tool results, or use Responses compaction so the next turn starts from a smaller window.

How do I fix this: OpenAI returned context_length_exceeded?

  1. Read the error as a size problem, not a transient blip

    In Agents API turn errors, `context_length_exceeded` means the input exceeds the model’s context window. Official recovery is to reduce the input, and if the conversation is too long, start a new session with a shorter summary—do not hammer retries with the same transcript. Docs: https://developers.openai.com/api/docs/guides/agents-api/errors

  2. Check the model’s maximum context length

    For text generation, the prompt and generated output combined must stay within the model’s maximum context length; embeddings inputs must also stay under that model’s limit. Confirm which model string you called and look up its window in OpenAI’s model docs before you keep appending history. Concepts: https://developers.openai.com/api/docs/concepts

  3. Shrink what you send on the next request

    Truncate, omit, or otherwise shrink messages until they fit. Beware that removing a message from the input means the model loses knowledge of it. Prefer summarizing older turns, stripping huge tool payloads, and capping how many prior tool results you replay. Advanced usage: https://developers.openai.com/api/docs/guides/advanced-usage

  4. For long Responses runs, enable compaction before you overflow

    OpenAI’s Compaction guide covers server-side compaction via `context_management` with `compact_threshold` on Responses create, and a standalone `/responses/compact` endpoint for stateless workflows. Important: the window you send to `/responses/compact` must still fit the model—compact earlier, not after the hard failure. Pass compaction output through as-is; do not prune it yourself. Guide: https://developers.openai.com/api/docs/guides/compaction

  5. Continue with a smaller window or a larger model

    After you shrink or compact, retry with the reduced input. If you still need the full working set in one call, switch to a model whose published context window fits your payload. Keep new user turns short and avoid re-attaching the entire raw history once a compaction item (or summary) is carrying state forward.

Which tools are best to handle OpenAI context_length_exceeded?

Agents API errors

Official meaning of context_length_exceeded and the reduce-input / new-session recovery path.

Compaction guide

Server-side compact_threshold and /responses/compact for long-running Responses workflows.

API concepts

Reminder that prompt plus output must fit the model’s maximum context length.

Advanced usage

Practical guidance to truncate or omit messages when a conversation no longer fits.

All tools for “Handle OpenAI context_length_exceeded” →

Related fixes

See also AI tools launched today on the daily board.