Every provider rejects an over-long request differently. OpenAI returns a 400 context_length_exceeded naming both the limit and your token count; Anthropic returns prompt is too long: N tokens > M maximum; Google returns a 400 in the invalid_request family. What they share is the accounting: system prompt, tool schemas, full message history, output reservation, and reasoning tokens all count. Current windows are large enough that most of these errors are now history-management bugs rather than genuine limits, with GPT-5.6 at 1,050,000 tokens, Claude 5-series models at 1M, and the Gemini 3.x line at 1,048,576. The fixes, in ascending order of effort, are trimming, compaction, retrieval, and routing to a bigger window.
- The window holds everything: system, tool schemas, history, output reservation, and reasoning tokens.
- Reasoning tokens count, and you cannot see them. This is why a request that fit yesterday fails today.
- Tool definitions are resent on every single call. Twenty verbose schemas is a permanent tax on every turn.
- On Claude 4.5 and newer, input plus
max_tokensover the window is accepted and stops withmodel_context_window_exceeded. - GPT-5.6 has a 922,000 max-input cap under a 1,050,000 window. You can blow the input cap first.
- Cross the 272K input mark on GPT-5.6 and you pay 2x input and 1.5x output on the whole request.
- Bigger windows are slower and dearer, and recall degrades long before the limit. A fit is not a good idea.
This model's maximum context length is N tokens
Three providers, three wordings, one condition. Search traffic clusters around the OpenAI phrasing because it is the oldest and the most quoted, but knowing all three saves you when your gateway abstracts the provider away.
| Provider | Status and code | Message shape |
|---|---|---|
| OpenAI | 400, code: context_length_exceeded | This model's maximum context length is N tokens. However, your messages resulted in M tokens... |
| Anthropic | 400, invalid_request_error | prompt is too long: 206767 tokens > 200000 maximum |
| Google (Interactions API) | 400, code: "invalid_request" | A message naming the offending field |
| OpenRouter | 400, error_type: context_length_exceeded | Normalized across upstream providers |
The most useful property of the OpenAI wording is that it gives you both numbers. The gap between them tells you which fix you need: 300 tokens over is a trimming problem, 300,000 over is an architecture problem, and treating the second like the first is how people end up with a truncation function that quietly destroys the beginning of every conversation.
What actually counts toward the window
People budget for the prompt and get surprised by everything else. OpenAI defines the window across three categories, verbatim: input tokens, output tokens, and reasoning tokens, the last being tokens "used by the model to plan a response". That third category is the one you cannot see and cannot easily predict.
| Component | Counts? | Notes |
|---|---|---|
| System prompt | Yes | Constant per call. Every word is paid for on every turn |
| Tool and function schemas | Yes | Resent in full on every request. Verbose JSON Schema is expensive |
| Full message history | Yes | Grows without bound unless something manages it |
| Tool results | Yes | The usual runaway: one unpaginated file read can be 50k tokens |
| Images and documents | Yes | Tokenized by dimensions, not by file size |
| Reasoning tokens | Yes | Invisible in the response, counted in the window, variable per request |
| Output reservation | Yes | Your max_tokens is held against the window on most providers |
| Cached prefix | Yes | Caching cuts the price, not the token count |
Prompt caching is worth a sentence here because it is regularly misunderstood as a context fix. It is not. A cached prefix costs a fraction of the price, typically a tenth on read, and occupies exactly the same number of tokens in the window. Caching solves cost and latency. It does nothing for this error.
The current limits, and the caps hiding inside them
Windows grew enormously through 2026, which changed the character of this error: it is now far more often a history-management bug than a real ceiling.
| Model | Context window | Max output | Notes |
|---|---|---|---|
gpt-5.6-sol | 1,050,000 | 128,000 | Max input 922,000 |
gpt-5.6-terra | 1,050,000 | 128,000 | Max input 922,000 |
gpt-5.6-luna | 1,050,000 | 128,000 | Max input 922,000 |
claude-fable-5 | 1M | 128k | |
claude-opus-5 | 1M | 128k | |
claude-sonnet-5 | 1M | 128k | |
claude-haiku-4-5-20251001 | 200k | 64k | The small model is the one to watch |
gemini-3.7-flash | 1,048,576 | 65,536 | Same shape across the 3.x line |
gemini-3.1-pro-preview | 1,048,576 | 65,536 | Prices step up above 200k input |
- The 922,000 input cap on GPT-5.6 is separate from the window. You can be inside 1,050,000 and still rejected for input alone. Two limits, two failure modes.
- Anthropic no longer gates 1M behind a beta header. The docs are explicit: for every model with a 1M window, 1M is the default, no header, and long-context requests are billed at standard rates. If your code still sends
context-1m-2025-08-07, delete it. - Gemini 3.1 Pro prices in two bands, $2 and $12 per million up to 200k input and $4 and $18 above it. The window does not change; the invoice does.
- GPT-5.6 has a price cliff at 272K input: past it, the whole request bills at 2x input and 1.5x output. A 280k-token request costs disproportionately more than a 270k one.
Measure before you fix
Counting tokens before you send is cheap, and both major providers give you an endpoint for it. Guessing at four characters per token is fine for a rough sanity check and useless for a limit that rejects you at one token over.
count = client.messages.count_tokens(
model="claude-sonnet-5",
system=SYSTEM_PROMPT,
tools=TOOLS, # schemas count, and people forget them
messages=messages,
)
print(count.input_tokens)
The number that matters is not the total, it is the breakdown. Log the four components separately, once, on a representative conversation:
- System prompt. Fixed cost, paid every turn. If it is above a couple of thousand tokens, it is worth an edit.
- Tool schemas. Also fixed, also every turn, and usually the most compressible thing in the request. Descriptions written for a human reader are the usual culprit.
- History. The part that grows. If this is 80% of your total, no amount of prompt editing will help you.
- The largest single message. Nine times out of ten it is one tool result: a whole file, an unpaginated query, a full HTML page from a fetch.
Four fixes, cheapest first
1. Trim the history
Drop the oldest turns until you fit. Fast, stateless, and it silently loses whatever was decided early in the conversation, which for a coding agent is usually the requirements. Two rules make it survivable: never split a tool exchange, because a tool_use without its tool_result is an immediate 400 on Anthropic, and always keep the first user turn, because it usually carries the actual task.
2. Compact
Summarize the older half of the conversation into a single message and continue. This is what Claude Code does at its context threshold and it is the best general answer for long-running agent sessions: it preserves decisions and discards transcript. The cost is one extra model call and the permanent loss of exact wording, which matters if the conversation contained precise identifiers or code that will be referenced verbatim later.
3. Move the bulk out of context entirely
If you are pasting a corpus into every request, retrieval is the right answer and the window is not your problem. Chunk, embed, and fetch the three passages that matter per turn. This is more infrastructure than the other options and it is the only one that scales past a window of any size. It also fails differently: instead of an error you get a confident answer based on the wrong chunk, so it needs evaluation in a way trimming does not.
4. Route to a bigger window
The escape hatch, and the one to reach for last. It works, it is one line, and it hides the underlying growth until you hit the bigger limit too. Two costs to weigh: money, since GPT-5.6 doubles input pricing past 272K and Gemini 3.1 Pro steps up past 200k, and quality, because recall across very long contexts degrades well before the hard limit. A conversation that only fits in a million tokens is usually a conversation that should have been compacted. If you do route up, the cards for Claude Fable 5 at 1,000,000 input tokens and gpt-5.6-sol at 1,050,000 carry the exact window and output ceiling for each.
| Situation | Reach for |
|---|---|
| Slightly over, occasionally | Trimming, plus a tool-output cap |
| Long agent session, gradual growth | Compaction at a threshold |
| One enormous document per request | Retrieval, or a long-context model if it is genuinely one-shot |
| Large static corpus, many questions | Retrieval. Nothing else scales |
| Genuinely needs a million tokens, once | A long-context model, and accept the bill |
| Growing history and a small model | Compaction first, then reconsider the model |
Questions people ask
What does context_length_exceeded mean?
Your request needs more tokens than the model can hold in one call. The count includes the system prompt, every tool schema, the entire message history, any output you reserved with max_tokens, and on reasoning models the invisible thinking tokens. The provider rejects the whole request rather than truncating it, except on Claude 4.5 and newer where an over-long output reservation is accepted and generation stops with model_context_window_exceeded instead.
Do reasoning tokens count toward the context window?
Yes. OpenAI defines the window as input tokens, output tokens, and reasoning tokens together, describing the last as tokens used by the model to plan a response. This is why an identical request can succeed once and fail the next time: reasoning length varies with the difficulty of the problem, and you cannot see it in the response.
How do I count tokens before sending a request?
Anthropic exposes a count_tokens endpoint that accepts the same system prompt, tools, and messages you are about to send, so count the request exactly as you will send it rather than counting the prompt alone. Rough character-based estimates are fine for a sanity check and useless near the limit. What matters most is logging the breakdown across system, tools, history, and largest single message.
Does prompt caching help with context length errors?
No. Caching reduces the price of a repeated prefix, typically to a tenth on read, and occupies exactly the same number of tokens in the window. It is a cost and latency optimization, not a capacity one.
What is the largest context window available in August 2026?
Around a million tokens across all three major providers: GPT-5.6 models at 1,050,000 with a separate 922,000 max-input cap, the Claude 5-series at 1M, and the Gemini 3.x line at 1,048,576. Note that using a large window is not free. GPT-5.6 charges 2x input and 1.5x output above 272K input tokens, and Gemini 3.1 Pro prices differently above 200k.
Should I just switch to a bigger model?
It is the fastest fix and rarely the best one. Recall degrades across very long contexts well before the hard limit, the request gets slower, and the pricing bands make a very large request cost disproportionately more. A conversation that only fits in a million tokens is usually one that should have been compacted, and switching models just defers the same failure to a larger number.
Sources
Every figure above was read from these pages on August 2026. Vendors reprice without notice; if you find a stale number, tell us.
- OpenAI conversation state and context the three-category definition of the context window
- OpenAI token counting max_output_tokens covering non-visible tokens
- Claude context windows prompt-too-long, model_context_window_exceeded, 1M without a beta header
- GPT-5.6 Sol model reference window, max input, and the 272K pricing band
- Gemini API models input and output token limits for the 3.x line
- Gemini API pricing the 200k input pricing step on Gemini 3.1 Pro