Answers for LLM inference engineers

Context length exceeded: what vLLM, SGLang, llama.cpp, Ollama, TGI and Dynamo return, and what openai-python and LiteLLM do with it

Most current engines reject a prompt that does not fit with HTTP 400, which openai-python raises as BadRequestError without retrying. The exceptions are the ones that cause trouble. Ollama and llama.cpp before build b6207 (2025-08-19) cut the prompt and answer 200. vLLM since v0.32.0 shortens an oversized max_tokens without an error. SGLang up to v0.5.9 puts the error inside a 200 stream. TGI answers 422. Any engine that answers 500 makes openai-python send the request three times. LiteLLM raises ContextWindowExceededError only when the message contains one of its known phrases, so several real messages come through as a plain BadRequestError. overflow_check.py sends four over-long requests to your server and shows which case you have.

Published 2026-11-25 · Engine source read on 2026-11-25 · Clients tested: openai-python 3.59.0, LiteLLM 1.111.0 · Tool version 1.0.0 (Python 3.8+, standard library only)

What does each engine return when the prompt is too long?

The status code and message depend on the engine and its version. The two right-hand columns are measured: each engine's exact response was replayed to the clients (see method).

Engine, versionsPrompt longer than the contextPrompt fits, prompt + max_tokens does notopenai-pythonLiteLLM ContextWindowExceededError?
llama.cpp b7481+ (2025-12-19)400, type exceed_context_size_error, “request (N tokens) exceeds the available context size (M tokens), try increasing it”, plus n_prompt_tokens and n_ctx. Streaming requests get the same 400.200; generation stops at the context with finish_reason: "length"BadRequestError, 1 requestyes
llama.cpp b6380–b7480400, exceed_context_size_error, “the request exceeds the available context size. try increasing the context size or enable context shift”200BadRequestErroryes
llama.cpp b6207–b6379400, invalid_request_error, same message (no token counts)200BadRequestErroryes
llama.cpp up to b6205, default settings200. The prompt is cut in half and processed (context shift was on by default). Measured: 2,109 tokens sent, usage.prompt_tokens 1,085.200; the context shifts and generation continuesno exceptionno error to classify
vLLM v0.32.0+ (2026-10-18)400, BadRequestError, “This model's maximum context length is M tokens. However, your prompt contains N input tokens, which exceeds this limit even without any output tokens.”200; max_tokens is lowered to what fits (PR #57035). Only max_tokens larger than the whole context is a 400.BadRequestErroryes
vLLM v0.18.0–v0.31.0400, “This model's maximum context length is M tokens. However, you requested K output tokens and your prompt contains N input tokens, for a total of T tokens.”400, same messageBadRequestErroryes
vLLM v0.10.1–v0.17.1400, “This model's maximum context length is M tokens. However, your request has N input tokens.”400, “'max_tokens' or 'max_completion_tokens' is too large: K. This model's maximum context length is M tokens and your request has N input tokens (K > M - N).”BadRequestErroryes
vLLM up to v0.10.0400, flat body {"object":"error","message":...}, “This model's maximum context length is M tokens. However, you requested N tokens ...”400, “... However, you requested T tokens (N in the messages, K in the completion).”BadRequestErroryes
SGLang v0.5.10+ (2026-04-05)400, flat body, “The input (N tokens) is longer than the model's context length (M tokens).” Streaming requests also get 400.400, “Requested token count exceeds the model's maximum context length of M tokens.” (since v0.4.1.post7)BadRequestErroryes
SGLang up to v0.5.9, stream: true200, then one SSE event {"object":"error","message":...} (non-streaming requests get 400)sameno exception: one chunk with object: "error" and choices: nullno. openai/: MidStreamFallbackError; hosted_vllm/: an empty chunk with finish_reason: "stop"
Ollama, /v1/chat/completions (all versions)200. Oldest messages are dropped (logged only at debug level); a single message still too long is cut in the middle, keeping the first num_keep tokens (default 4) and the end (“truncating input prompt” warning in the server log).200no exceptionno error to classify
Ollama v0.30.9+ with context shift off400, invalid_request_error, “the prompt is longer than the context length currently available to the model; ...”—BadRequestErrorno (plain BadRequestError, also with ollama_chat/)
TGI 3.x (Text Generation Inference)422, body {"error": "Input validation error: `inputs` tokens + `max_new_tokens` must be <= M. ...", "error_type": "validation"}UnprocessableEntityError with message “Error code: 422” onlyonly with hosted_vllm/; openai/ gives BadRequestError and loses the text
NVIDIA Dynamo v1.0.0+ frontend400, “This model's maximum context length is M tokens. However, your messages resulted in N tokens.”400, “... your request would use N prompt tokens plus K ...”BadRequestErroryes
Any engine that answers 500 (Dynamo up to v1.6.1 with TensorRT-LLM over max_num_tokens)500InternalServerError after 3 requests (2 retries, 1.2 s)no (InternalServerError; with openai/ also 3 requests)

Dates are the PyPI release dates (vLLM, SGLang) or the tag dates (llama.cpp, Ollama, Dynamo). Versions in this table were found by reading every release tag of each engine; the first version is the first tag that has the code.

“the request exceeds the available context size, try increasing it” (llama.cpp)

The number in the message is the context of one slot, not the -c you passed. llama-server checks the prompt against the slot it runs in. With the default settings (build 12733), all four slots share one unified KV cache and each can use the whole -c. If you pass -np 4 yourself, the cache is split: with -c 2048 -np 4 we got “request (576 tokens) exceeds the available context size (512 tokens)”. Use a larger -c, fewer slots, or --kv-unified.

vLLM: “This model's maximum context length is ... tokens” and the v0.32.0 change

Since v0.32.0, vLLM no longer counts max_tokens against the context when it checks the prompt. A request whose prompt fits but whose max_tokens does not is accepted, and the output is cut at the context with finish_reason: "length". Before v0.32.0 the same request was a 400. Code that relied on that 400 to detect “too long” now gets a truncated answer instead (#57035, in v0.32.0).

Ollama: no error, the prompt is cut

On /v1/chat/completions Ollama does not reject an over-long prompt. It drops the oldest messages until the rest fits and, if the last message alone is too long, cuts it in the middle. The answer is a normal 200. This is the usual reason a model “forgets” the start of a long document. The context size is the model's num_ctx or OLLAMA_CONTEXT_LENGTH (default 4k, 32k or 256k depending on VRAM), not the model's maximum.

SGLang: the error inside a 200 stream (up to v0.5.9)

Up to v0.5.9, SGLang validated the length after the stream had started, so a streaming request got 200 and then a single event data: {"object":"error","message":"The input (N tokens) is longer than the model's context length (M tokens).", ...}. openai-python does not raise on it: it yields one chunk whose object is "error" and whose choices is None, so code that reads chunk.choices[0] fails with a TypeError far from the cause. v0.5.10 reads the first chunk before sending headers and returns a proper 400. Non-streaming requests got a 400 in both versions. SGLang can truncate instead with --allow-auto-truncate.

What do TGI and Dynamo return?

Does openai-python retry it, and does LiteLLM raise ContextWindowExceededError?

openai-python retries 408, 409, 429 and every 5xx (twice by default, with backoff), or whatever the x-should-retry response header says. It never retries 400 or 422. A context overflow reported as 500 is therefore sent three times and fails three times. LiteLLM decides ContextWindowExceededError by searching the error text for fixed phrases (is_error_str_context_window_exceeded); the status code alone does not make it a context error.

Phrase in the message (LiteLLM 1.111.0)Engine that writes it
this model's maximum context length isvLLM (all versions), Dynamo
is longer than the model's context length, requested token count exceeds the model's maximum context lengthSGLang
exceeds the available context sizellama.cpp (both wordings)
`inputs` tokens + `max_new_tokens` must beTGI (but see the 422 note: only when the text reaches LiteLLM)
not in the listOllama “the prompt is longer than the context length”; llama.cpp “is larger than the max context size”; vLLM “cannot be greater than max_model_len”, “max_tokens must be at least 1”

If you catch overflows yourself, catch openai.BadRequestError and openai.UnprocessableEntityError, and look at the status code and the body (e.body), not only the message. For llama.cpp, e.body["n_ctx"] gives the limit directly.

Check your own server: overflow_check.py

The script reads the context length from the server (vLLM and SGLang /v1/models, llama.cpp /props) or takes --ctx, measures how many tokens a repeated word costs, and sends four requests. For each it prints the status, error type and message, the first SSE event of the streaming request, and for a 200 the usage counts, which reveal silent truncation. It says whether openai-python would retry and whether LiteLLM's phrase check matches. With --clients it also calls the installed openai-python and LiteLLM and prints what they raised.

curl -O https://erimaki-kobo.github.io/en/context-overflow-error-status-check/overflow_check.py
python overflow_check.py --base-url http://localhost:8000/v1 --model MODEL
python overflow_check.py --base-url http://localhost:11434/v1 --model llama3.2 --ctx 4096   # Ollama: pass num_ctx
pip install openai litellm && python overflow_check.py --base-url ... --model ... --clients --json out.json

Output against llama.cpp build b6205 with default settings (CPU, a tiny random-weight model):

context 2048 (/props n_ctx (per slot)); 'the ' = 4.000 tokens, template overhead ~49 tokens

[prompt_over] sent ~2109 prompt tokens, max_tokens=16
  verdict                accepted with HTTP 200: the prompt was truncated without an error
  status                 200
  prompt_tokens          1085
  completion_tokens      16
  finish_reason          length
  clients                {'openai_python': 'no exception', 'litellm_openai': 'no exception', 'litellm_hosted_vllm': 'no exception'}

The same request against the current build:

[prompt_over] sent ~2112 prompt tokens, max_tokens=16
  verdict                HTTP 400
  error_type             exceed_context_size_error
  message                request (2112 tokens) exceeds the available context size (2048 tokens), try increasing it
  error_extra            {'n_prompt_tokens': 2112, 'n_ctx': 2048}
  openai_python_retries  False
  litellm_message_check  True
  clients                {'openai_python': 'BadRequestError', 'litellm_openai': 'ContextWindowExceededError', 'litellm_hosted_vllm': 'ContextWindowExceededError'}

The four requests use a few thousand prompt tokens in total; on a GPU server that takes seconds. Point it at a test instance if a 400 on your production server pages someone.

How this was measured

FAQ

Should a context overflow be HTTP 400 or 500?
400 (or another 4xx). It is a property of the request, so retrying cannot help, and SDKs such as openai-python retry every 5xx automatically. LiteLLM's phrase list also contains OpenAI's own error code context_length_exceeded.
Why does llama-server say 512 tokens when I started it with -c 2048?
You set -np 4, which splits the KV cache into four slots of 512. Without -np (or with --kv-unified) each slot can use the full context.
Why doesn't Ollama return an error for a long prompt?
It truncates by default: old messages are dropped and an over-long message is cut in the middle. Raise num_ctx or OLLAMA_CONTEXT_LENGTH, and check usage.prompt_tokens against what you sent.
Why did my vLLM request stop returning 400 after upgrading?
From v0.32.0 a prompt that fits is accepted even if max_tokens does not; the output stops at the context with finish_reason: "length". Check finish_reason instead of waiting for a 400.
Why doesn't LiteLLM raise ContextWindowExceededError for my server?
LiteLLM looks for fixed phrases in the error text. Ollama's message and some vLLM and llama.cpp messages are not in its list, a 200 with truncation has no error at all, and a 422 through the openai/ provider loses the text.

Limitations

Sources