Skip to content

OpenAI API errorOpenAI: “context_length_exceeded”

The AI feature works in testing, then fails in production with context_length_exceeded. The request asked for more tokens than the model’s context window holds: a chat history that keeps growing, a large document pasted in whole, or a high max output setting on top. The fix is to control what goes into each request. Here it is in OpenAI’s words, or hand it over at a fixed price.

Short answer

Count the tokens before you send, and keep each request inside the model’s context window, including the output you ask for. Send only the recent turns plus a short summary of older ones, split or retrieve parts of large documents instead of sending them whole, and lower the maximum output if it is set high. For long conversations on the Responses API, turn on compaction.

Why the request is too long, and the fix

  1. 01

    The window counts input and output together

    “This max tokens number includes input, output, and reasoning tokens.” OpenAI, Conversation state

    The limit covers everything in the request: the system prompt, the history, any documents, and the output tokens you reserve. A request can fail even when the input alone looks small enough.

  2. 02

    The chat history keeps growing

    “Compaction helps you balance quality, cost, and latency as conversations grow.” OpenAI, Compaction

    Apps that resend the whole conversation every turn hit the limit after enough messages. Keep the last few turns as they are and replace older ones with a summary, or drop them. Compaction, below, does this on the server.

  3. 03

    Use compaction on the Responses API

    “When the rendered token count crosses the configured threshold, the server runs server-side compaction.” OpenAI, Compaction

    Set context_management with a compact_threshold on the request, and the server compacts the context when it grows past that size.

  4. 04

    A whole document goes in every request

    “You can estimate the number of tokens your messages will use with the tokenizer tool.” OpenAI, Conversation state

    Count a file’s tokens before sending it. If it doesn’t fit, split it, or search it and send only the relevant parts.

Checked against the vendor’s docs on : OpenAI API docs: Conversation state (managing the context window); OpenAI API docs: Compaction; OpenAI API docs: Error codes.

Rather hand it over?

Fixed scope, fixed price, paid upfront. The work happens in writing; no call needed.

Related: OpenAI 429 insufficient_quota · OpenAI 401 Incorrect API key provided · Anthropic API 529 overloaded_error · Gemini API 429 RESOURCE_EXHAUSTED

Questions

More on this error

Should I just switch to a model with a bigger context window?

It buys time, but a history that keeps growing will reach any limit, and longer requests cost more. Trim or compact the context as well.

Is this the same as a 429 error?

No. A 429 is about rate limits or quota. This error is about the size of one request, so retrying the same request fails the same way.

A different error?

Describe it and get a written quote

Send the error and what you were doing. You get a written scope, a fixed price and a timeline; we reply by email, usually within one business day.