400 "This model's maximum context length is N tokens" (context_length_exceeded): why max_tokens counts, and how to fix it
The message says "reduce the length of the messages", so people trim their prompt, and the error persists, because the check is prompt plus max_tokens. A 10K-token prompt with max_tokens: 65536 on a 65K model fails, and the message about messages does not say why. The second version of the error, from vLLM and hosts built on it, spells it out: you requested 65535 output tokens and your prompt contains at least 62466 input tokens. Agent tools are the usual source: several set a fixed large max_tokens for every request, and tool outputs grow until the sum tips over. Once you know which side of the sum is the problem, the fix is short.
Last verified September 21, 2026 against OpenAI docs: error codes, GitHub: 680 issues quoting the message since January 2026, vllm-project/vllm: rejects requests when max_tokens exceeds available context · 5 min read
| Provider | OpenAI, and every OpenAI-format host: vLLM, DeepSeek, Together, Groq, Inference APIs. The wording is shared |
| HTTP status | 400 Bad Request, code context_length_exceeded |
| Message | This model's maximum context length is 128000 tokens. However, your messages resulted in 130412 tokens. Please reduce the length of the messages., or a variant naming max_tokens |
| Cause | Prompt tokens plus the requested max_tokens exceed the window. Half the reports are a large max_tokens on a small prompt, not a long conversation |
| How common | 680 GitHub issues since January 2026 |
| Can you wait it out? | No. Same request, same answer |
- Check
max_tokensfirst. It must be at most window minus prompt. Many tools hard-code 32K or 64K; lower it, or leave it unset and let the server pick. - Then the prompt. Drop or summarise old turns, truncate tool output, do not paste whole files.
- Or change the window. Same API format, 1M-token models: fix 3.
What it looks like
{
"error": {
"message": "This model's maximum context length is 128000 tokens. However, your messages resulted in 130412 tokens. Please reduce the length of the messages.",
"type": "invalid_request_error",
"param": "messages",
"code": "context_length_exceeded"
}
}The param is messages even when max_tokens is the real cause, which is the trap. On our endpoint the same overflow is reported as:
{
"error": {
"message": "The request does not fit the model's context window of 131,072 tokens (prompt tokens plus max_tokens). Shorten the conversation, lower max_tokens, or use a model with a larger window: DeepSeek V4 and GLM 5.3 models here take 1,048,576 tokens. See GET /v1/models for each model's context_length.",
"type": "invalid_request_error",
"param": null,
"code": "context_length_exceeded",
"docs_url": "https://inferenceapis.com/docs/errors#context_length_exceeded"
}
}The window size is in the message so you can decide whether to trim or to move, and GET /v1/models lists context_length for every model without a key.
Fix 1: max_tokens is part of the sum
Set max_tokens to what you need for the answer, not to the model's maximum. If you do not know, omit it; servers default to what fits. In agent frameworks look for a per-model output setting; the reports from opencode, Kilo Code and Zed in the threads above all trace to a static output budget colliding with a grown prompt.
Fix 2: budget the prompt before sending
Count tokens client-side and drop the oldest turns until the request fits, keeping the system prompt. Tool outputs are the usual growth: cap them at a few thousand tokens each before they go back into the conversation.
import tiktoken
enc = tiktoken.get_encoding("o200k_base") # close enough for budgeting on any model
def fit(messages, window, max_tokens, margin=2000):
"""Drop the oldest non-system turns until prompt + max_tokens fits the window."""
budget = window - max_tokens - margin
system = [m for m in messages if m["role"] == "system"]
rest = [m for m in messages if m["role"] != "system"]
count = lambda ms: sum(len(enc.encode(str(m["content"]))) + 4 for m in ms)
while rest and count(system + rest) > budget:
rest.pop(0)
return system + restFix 3: a model with a larger window, same code
If the work needs the context, the window is the fix. These models take 1M tokens on the OpenAI format, on the Anthropic format (for Claude Code) and on the Responses API (for Codex):
| Model | Context window | Per 1M tokens in / out |
|---|---|---|
deepseek-ai/DeepSeek-V4-Flash | 1,048,576 | $0.19 / $0.38 |
deepseek-ai/DeepSeek-V4.1-Flash | 1,048,576 | $0.40 / $1.60 |
deepseek-ai/DeepSeek-V4-Pro | 1,048,576 | $1.60 / $3.40 |
zai-org/GLM-5.3, zai-org/GLM-5.3-Flash | 1,048,575 | $1.82 / $5.72 · $0.20 / $0.66 |
MiniMaxAI/MiniMax-M3 | 524,288 | $0.39 / $1.56 |
moonshotai/Kimi-K2.7-Code, Qwen/Qwen3-VL-235B-A22B-Instruct | 262,144 | $0.89 / $4.42 · $0.26 / $1.15 |
openai/gpt-oss-120b, meta-llama/Llama-3.3-70B-Instruct-Turbo | 131,072 | $0.20 / $0.80 · $0.20 / $0.50 |
Long prompts are billed as input. On DeepSeek V4 Flash a 500K-token prompt costs about ten cents, and repeated prefixes bill at the cached-input rate of $0.04 per million, so a large fixed context reused across turns is cheap after the first call. Speed and tool-calling checks per model are on the model pages.
Frequently asked questions
Why does the error say "reduce the length of the messages" when my prompt is short?
Because the check is prompt plus max_tokens and the message template only mentions messages. Lower max_tokens and the same prompt goes through.
Do reasoning tokens count against the window?
Yes, as output. A reasoning model with a large thinking budget needs more room than its visible answer suggests. On our reasoning models reasoning_effort: "none" removes that.
Is 1M context slower?
Time to first token grows with the prompt; a full 1M prompt takes noticeably longer than a 10K one on any host. Cached input, which applies automatically here to repeated prefixes, removes most of that on later turns.
Where Inference APIs fits
Fix 3 is what we sell: five models with a 1,048,576-token window on the same OpenAI wire format, so the fix for a 128K overflow can be a model name. Our own overflow error names the window and this page. US GPUs, zero retention, prepaid; a new account starts with $1 of credit.
Something changed or wrong? Tell us and we will re-verify the entry.
