Context length exceeded
The text sent to the model, including chat history, documents, and tool results, is more than the model can read in one request.
Also appears as
- BadRequestError: 400 This model's maximum context length is 128000 tokens. However, your messages resulted in 130532 tokens. Please reduce the length of the messages.
- This model's maximum context length is 128000 tokens. However, you requested 132096 tokens (128000 in the messages, 4096 in the completion). Please reduce the length of the messages or completion.
- BadRequestError: 400 {"type":"error","error":{"type":"invalid_request_error","message":"prompt is too long: 213462 tokens > 200000 maximum"},"request_id":"req_011CSHoEeqs5C35K2UUqR7Fy"}
Prompt to fix it
Our AI feature fails with a context length error ("context_length_exceeded", "maximum context length", or "prompt is too long"). Find the code that builds the request and measure what goes into it: system prompt, chat history, documents, and tool results. Fix it by limiting what we send: keep recent messages within a token budget, summarize older ones, and include only the relevant chunks of large documents. Handle the error so the app trims and tells the user, instead of retrying the same request. Don’t just switch to a model with a bigger context window.
Paste into Layerwise or any AI coding tool.
What it means
Every model has a context window: the most text, counted in tokens, it can take in one request. A token is about three-quarters of an English word. The window covers everything you send, including the system prompt, the whole chat history, documents, and tool results, and on some models the room reserved for the reply. When the total goes over, the API rejects the request before the model runs. OpenAI marks it with the code context_length_exceeded, and Anthropic says prompt is too long.
In apps, this usually builds up over time. A chat feature that resends the full conversation on every turn works for the first few dozen messages and then fails. So does a feature that pastes whole files, web pages, or database rows into the prompt.
Switching to a model with a bigger window only postpones it, and every request gets slower and more expensive along the way. The lasting fix is to control what goes in: keep the latest turns plus a short summary of older ones, and send only the relevant parts of large documents.
Common causes
- A chat feature sends the entire conversation history with every message.
- Whole documents, PDFs, web pages, or query results are pasted into the prompt.
- An agent loop appends every tool result, so each step adds more text.
- The request reserves a large reply, and those output tokens push the total over the limit.
- The code targets a model with a smaller window than the one it was tested with.
How to fix it
- Read the token counts in the error, if it has them, or count tokens before sending to see how far over the request is.
- Cap the chat history to the most recent turns, and replace older turns with a short summary.
- Split large documents into chunks and send only the chunks relevant to the question.
- Shorten or summarize long tool results before adding them to the conversation.
- Lower the reply limit (
max_output_tokenson OpenAI’s Responses API,max_tokenson Anthropic) if it asks for more than needed. - Catch this error and trim further, since retrying the same request fails the same way.
Example fix
type Message = { role: 'user' | 'assistant'; content: string };
// Keep the newest messages that fit a rough budget (about 4 characters per token).
export function trimHistory(messages: Message[], maxTokens = 50_000) {
const kept: Message[] = [];
let used = 0;
for (const message of [...messages].reverse()) {
used += Math.ceil(message.content.length / 4);
if (used > maxTokens) break;
kept.unshift(message);
}
return kept;
}Related errors
All errors413 Payload Too Large
The request body, usually a file upload, was bigger than the server or host accepts, so it was rejected before your code could handle it.
You exceeded your current quota
OpenAI refused the request because the account or project behind the API key is out of credit or has reached a spending limit.
Anthropic API 529 overloaded_error
Anthropic’s API had more traffic than it could serve at that moment, so it turned your request away even though the request was fine.
Incorrect API key provided
The AI API rejected the key your code sent: it has a typo, was revoked, or isn’t the key you think the app is using.
Build with an agent that checks its work
The Layerwise agent clicks through the live preview and reads the console, so it catches errors before you do.