Short answer
A 400 with "code": "context_length_exceeded" means the request needs more tokens than the model's context window holds. The window covers the input and the output, including reasoning tokens, so a large max_output_tokens (or max_tokens) can push a request over even when the input alone fits.
Retrying the same request fails the same way. Make it fit:
- Count the input with the input token count endpoint (
POST /v1/responses/input_tokens) before you send. - Send less: drop or summarize old turns, trim long tool outputs and documents, or split the work.
- Lower
max_output_tokensso input plus output fits. - Let the API manage it in the Responses API:
truncation: "auto"drops the oldest items, andcontext_managementwith acompact_thresholdcompacts the conversation on the server.
In Codex, the same condition shows as Codex ran out of room in the model's context window. Run /compact, or start a new thread with /new.
What the error looks like
OpenAI's current message, as returned through the Responses API in openai/codex#52567 and quoted in BerriAI/litellm#43013:
{"error": {"message": "Your input exceeds the context window of this model. Please adjust your input and try again.", "type": "invalid_request_error", "param": "input", "code": "context_length_exceeded"}}
Older OpenAI responses, and some OpenAI-compatible servers today, use a wording with the numbers in it. This one is from OpenAI's cookbook example on embedding long inputs:
Error code: 400 - {'error': {'message': "This model's maximum context length is 8192 tokens, however you requested 10001 tokens (10001 in your prompt; 0 for the completion). Please reduce your prompt; or completion length.", 'type': 'invalid_request_error', 'param': None, 'code': None}}
A version that shows the output reservation at work, from a DeepSeek model in CommandCodeAI/command-code#892:
Error: 400 This model's maximum context length is 1048576 tokens. However, you requested 1049647 tokens (985647 in the messages, 64000 in the completion).
The input fit. The 64,000 tokens reserved for the answer didn't.
In Codex, from codex-rs/protocol/src/error.rs:
Codex ran out of room in the model's context window. Start a new thread or clear earlier history before retrying.
The OpenAI Python SDK raises BadRequestError for this 400. Check e.code == "context_length_exceeded" to tell it apart from other 400s.
Why it happens
OpenAI's conversation state guide defines the context window as the maximum number of tokens in a single request, counting input, output and, for reasoning models, reasoning tokens. Each model's window is on its model page.
Common ways a request gets too big:
- The conversation keeps growing. Every turn re-sends the history, including earlier tool calls and their outputs.
- Tool outputs are large. A file read, a search result or a log dump can add tens of thousands of tokens in one step.
- Documents and images in the input. Files and images count toward the input too.
- A large output reservation.
max_output_tokensis reserved on top of the input, as in the command-code example. - A model with a smaller window. Switching to a model with a smaller window makes a history that fit before fail.
Not the same as a 429. Request too large for <model> ... on tokens per min (TPM) is a rate limit: the request is bigger than your per-minute allowance, not the model's window. See OpenAI API 429 "Rate limit reached".
How to fix it
1. Count before you send
The input token count endpoint takes the same input as the Responses API and returns the exact input tokens the model will receive:
from openai import OpenAI
client = OpenAI()
count = client.responses.input_tokens.count(model="gpt-6-sol", input=conversation)
print(count.input_tokens)
Compare the result plus your max_output_tokens with the model's context window, and trim before sending if it's close.
2. Make the input smaller
- Drop old turns, or replace them with a short summary.
- Keep only the part of a tool output the model needs, for example the matching lines instead of the whole file.
- Split a large document into chunks and process them in separate requests. The cookbook example chunks embedding inputs the same way.
3. Lower the output reservation
Set max_output_tokens (Chat Completions: max_completion_tokens or max_tokens) to what the answer needs. Near the end of a long session, input plus a large reservation is the usual reason a request stops fitting.
4. Let the Responses API drop or compact history
- Truncation. The Responses create reference has a
truncationparameter. With"auto", the API drops items from the beginning of the conversation to fit the window. With"disabled", the default, the request fails with a 400 instead. - Server-side compaction. Per the compaction guide, set
context_managementwith acompact_threshold. When the context crosses that threshold, the server compacts it and continues, and the response includes a compaction item to carry forward. There's also a standalone/responses/compactendpoint; the window you send to it must still fit the model.
resp = client.responses.create(
model="gpt-6-sol",
input=conversation,
store=False,
context_management=[{"type": "compaction", "compact_threshold": 200_000}],
)
truncation: "auto" silently loses the oldest context, so prefer compaction or your own summary when earlier turns matter.
If you see it in Codex
Codex ran out of room in the model's context window means the thread is too long for the model. Run /compact to summarize the visible chat, or /new to start a new chat in the same session. Compaction can fail too when the thread is already far over the window, as in openai/codex#51368. In that case, start a new thread and paste a short summary of where you were.
Using one key for several model families
TeamoRouter is an API gateway with one key for GPT, Claude, Gemini and DeepSeek. It's OpenAI-compatible (/v1/chat/completions and /v1/responses), and GET /v1/models lists the model IDs your key can use. If a task needs a larger window than the model you're on, you can try a model from another family with the same key and code. Check each model's context window before you switch. There's no subscription; you pay per use.
- Create an API key in the TeamoRouter console. It starts with
sk-teamo-. - Top up from $5. The top-up dialog takes cards. You can also click Pay with crypto and pay with USDT, USDC or ETH from any exchange app — the checkout shows which networks your amount supports (BEP20 works from $5). Step by step: Pay with USDT.
- Use it with the OpenAI SDK:
from openai import OpenAI
client = OpenAI(
api_key="sk-teamo-...",
base_url="https://api.teamorouter.com/v1",
)
resp = client.responses.create(model="gpt-6-sol", input="Hello")
print(resp.output_text)
Claude models use the Anthropic protocol (/v1/messages) with the same key; see the API integration guide.
FAQ
Does retrying help? No. The same request has the same size. Change the input or the output reservation first.
Why does it fail when my input is smaller than the window?
Output and reasoning tokens count too. Lower max_output_tokens, or leave room for the answer.
What's the difference between this and "Prompt is too long" in Claude Code? They are the same idea on different APIs: the request has more tokens than the model's window. The Claude version is covered in Claude Code "Prompt is too long".