AI Context Limit Exceeded: What to Do
In this article
When a prompt exceeds an AI model's context limit, the direct answer is to break your request into smaller, sequential parts and feed them to the model one at a time, using the model's output from each part as context for the next. This technique, often called chunking or sliding-window prompting, lets you work with text or data far larger than the model's maximum context window. Below, we explain why context limits exist, how to choose a chunk size, and the practical steps to handle long documents, codebases, or conversations.
What Is a Context Limit, Really?
Every large language model (LLM) has a fixed number of tokens it can "see" at once. A token is roughly a word or part of a word; for English, about 100 tokens equals roughly 75 words. The context limit includes both the text you send (the prompt) and the text the model generates (the response). For example, a model with an 8,000-token limit can accept a prompt of 7,000 tokens and still generate 1,000 tokens of output. If your prompt alone is 9,000 tokens, the model will either reject it or truncate it, losing information at the start or end.
Context limits exist because of the underlying transformer architecture. The model's attention mechanism compares every token to every other token, so memory and compute scale quadratically with sequence length. Doubling the context window roughly quadruples the computational cost. That is why models with 128K or 1M token contexts are much more expensive to run than those with 4K or 8K.
How to Check the Limit Before You Hit It
Most model providers document their context limits clearly. For example, OpenAI's GPT-4o has a 128K token context; Anthropic's Claude 3.5 Sonnet has 200K; Google's Gemini 1.5 Pro has 1M. But the actual usable limit is often lower because the output tokens count against the total. A practical rule: subtract your expected response length from the model's limit to get your maximum prompt size.
To measure your prompt's token count, use a tokenizer tool—OpenAI's tokenizer, Anthropic's, or a free library like tiktoken. If you are pasting a long document, the token count is usually 1.3 to 1.5 times the word count for English. For code, it can be higher because symbols and spaces are tokenized differently.
Chunking: The Core Strategy
The most reliable way to handle oversized prompts is to split the content into chunks that each fit comfortably within the limit, then process them sequentially. Here is a step-by-step approach:
- Split your content by logical boundaries—paragraphs, sections, or functions—rather than by fixed character counts. This preserves meaning.
- Process each chunk with a prompt that asks the model to summarize, extract key points, or answer a specific question about that chunk.
- Combine the outputs into a new, smaller prompt. If the combined summary still exceeds the limit, repeat the process recursively.
- For question-answering over a large document, first ask the model to identify which chunks are relevant (using a search or embedding), then feed only those chunks with your question.
For example, if you have a 50,000-word report and a 4,000-token limit, you might split it into 10 chunks of ~5,000 words each. Ask the model to summarize each chunk in 200 words. Then combine those ten 200-word summaries into one prompt—now only 2,000 words—and ask your final question.
Choosing the Right Chunk Size
Chunk size is a trade-off between fidelity and efficiency. Larger chunks preserve more context but risk hitting the limit or losing detail in the middle. Smaller chunks are safer but can lose cross-references between sections. A good starting point is to aim for chunks that use 50–70% of the model's context limit, leaving room for the response and for any instructions.
For documents with clear structure (chapters, sections), use those boundaries. For code, split by function or class. For conversation history, keep the most recent turns and summarize older ones. If you are using a retrieval-augmented generation (RAG) system, you typically chunk into 500–1,000 token pieces and store them in a vector database, then retrieve the top-k most relevant chunks for each query.
Alternatives to Chunking
Chunking is not the only option. Depending on your use case, consider:
- Use a model with a larger context window—if your task genuinely needs the whole document at once, a 200K or 1M token model may handle it directly, though at higher cost.
- Summarize first, then ask—for broad questions, a hierarchical summary (summarize sections, then summarize the summaries) often works better than trying to feed everything.
- Retrieval-augmented generation (RAG)—embed your document, retrieve relevant chunks for each question, and only feed those chunks to the model. This scales to millions of tokens.
- Map-reduce prompting—process each chunk with a specific question, collect answers, then synthesize a final answer. This is a common pattern in LangChain and similar tools.
Each method has trade-offs. Larger context windows are simplest but cost more and may still hit limits with very long inputs. RAG is scalable but requires setup and may miss nuanced connections between distant parts of the document.
Common Mistakes and How to Avoid Them
Even with chunking, people often make errors that cause the model to miss information:
- Not accounting for the response tokens—leaving no room for the output truncates the answer. Always reserve at least 20% of the context for the response.
- Chunking mid-sentence or mid-code-block—this breaks syntax and meaning. Split on paragraph or line boundaries instead.
- Losing the question—when you process many chunks, the model may forget the original question. Repeat the question in each chunk prompt.
- Ignoring order—if you summarize chunks in sequence, the model may lose track of earlier summaries. Consider using a structured template that includes the chunk number and a heading.
- Assuming the model sees everything—if you truncate a prompt, the model does not know it is truncated. It will confidently answer based on what it sees, potentially missing critical information.
A good practice is to verify that the model's answer actually references the part of the document you care about. If not, retrieve that specific section and re-ask.
Cost and Performance Trade-offs
Context length directly affects cost. Most providers charge per token for both input and output, and input tokens are often priced lower than output tokens. But a long prompt costs more than a short one, even if the output is the same. For example, sending a 100K-token prompt costs roughly 12 times more than an 8K-token prompt on many pricing models. If you only need a small part of the document, RAG can be far cheaper.
Latency also increases with context length. The model must process every token in the prompt before generating a response, so a 128K prompt can take several seconds longer than an 8K prompt. For interactive applications, this can be noticeable. Chunking with RAG often provides faster responses because the prompt is much smaller.
Frequently Asked Questions
What happens if I send a prompt that exceeds the context limit?
Most APIs return an error like "This model's maximum context length is 8192 tokens. However, your messages resulted in 10000 tokens." Some interfaces silently truncate the prompt, which is worse because you lose data without knowing. Always check the token count before sending.
Can I increase the context limit of a model?
No. The context limit is a fixed property of the model architecture and training. You cannot change it. You can only choose a different model with a larger limit, or change how you structure your prompt.
Is there a way to make the model "remember" earlier parts of a long conversation?
Yes, by summarizing the conversation periodically. After every few turns, ask the model to summarize the key points so far, then replace the older messages with that summary. This keeps the context small while retaining the essential information.
What is the best chunk size for a 128K context model?
It depends on your task. For simple Q&A, chunks of 1,000–2,000 tokens are common. For summarization, you might use 8,000–16,000 tokens. The key is to leave enough room for the response and to keep chunks semantically coherent.
Does the context limit include the system prompt?
Yes. The system prompt, user messages, and the model's response all count toward the limit. If you use a long system prompt, it reduces the space available for your actual content.
Why do some models have 1M token contexts but still struggle with long documents?
Even with a large context, models may focus more on the beginning and end of the input, a phenomenon called "lost in the middle." They may also degrade in performance when the input is extremely long. Chunking and retrieval often yield better accuracy than feeding the entire document, even when the model can technically handle it.