OpenAI APIRunbook 0/4 done
context_length_exceeded

OpenAI API Error context_length_exceeded: Maximum context length exceeded

Error✕ Not retryable · fix the payloadHTTP 400 Bad RequestAI Provider
TL;DR · 10-second read

The messages (plus tools and the output budget) do not fit in the model’s context window. Count tokens before sending and trim or summarize the oldest turns.

Identify: does your log match?

What you see · exact log / response
openai.BadRequestError: Error code: 400 - {'error': {'message': "This model's maximum context length is 128000 tokens. However, your messages resulted in 131072 tokens. Please reduce the length of the messages.", 'type': 'invalid_request_error', 'param': 'messages', 'code': 'context_length_exceeded'}}
  1. High

    Chat history grows without a cap

    Every turn is resent, so a long conversation eventually exceeds the window.

  2. Medium

    Retrieved documents stuffed in whole

    RAG pipelines paste entire files instead of the top-k relevant chunks.

  3. Low

    Large tool schemas

    Dozens of function definitions count toward input tokens on every request.

Fix

Trim history to a token budget

✕ Broken · Python
resp = client.chat.completions.create(model="gpt-4o", messages=history)
✓ Fixed · Python
import tiktoken
enc = tiktoken.encoding_for_model("gpt-4o")

def fit(history, budget=120_000):
    system, rest = history[0], history[1:]
    keep, used = [], 0
    for m in reversed(rest):              # newest first
        n = len(enc.encode(m["content"])) + 4
        if used + n > budget: break
        keep.append(m); used += n
    return [system] + keep[::-1]          # always keep the system prompt

resp = client.chat.completions.create(model="gpt-4o", messages=fit(history))

Leave headroom below the hard limit for the reply; the window covers input and output together.

Verify: reproduce it on purpose

Reproduce locally · python
$ python3 -c "from openai import OpenAI; OpenAI().chat.completions.create(model='gpt-4o-mini', messages=[{'role':'user','content':'word '*200000}])"

Run it against a dev environment to trigger context_length_exceeded on purpose and test your error handler.

Prevent

  • Log token counts per request so growth shows up before it fails.
  • Send top-k retrieved chunks, not whole documents.
  • Only include the tools the current step can use.

FAQ

Does max_tokens count toward the limit?

Yes. Input tokens plus the requested output must fit within the context window.

Should I just switch to a bigger model?

It helps once, but unbounded history will overflow any window. Add trimming either way.

Related errors

Last verified 2026-10-22 · Source: OpenAI error-code guide + SDK exception classes · Official docs ↗ · ~12,100 searches/mo