In this categoryTroubleshooting ยท 27
- How to Fix an OpenAI 401 'Invalid API Key' ErrorStart
- How to Fix OpenAI 429 Rate Limit Errors With Backoff
- How to Fix Anthropic Claude API 401 Authentication Errors
- How to Fix Google Gemini 'API Key Not Valid' Errors
- How to Fix an API Key That Loads as Undefined
- How to Handle Anthropic 529 'Overloaded' Errors
- How to Fix Rate Limit Errors from an AI API
- How to Fix CORS Errors When Calling an AI API From the Browser
- How to Fix 'Model Not Found' and Deprecated Model Errors
- How to Rotate a Leaked API Key Without Downtime
- How to Fix SSL Certificate Errors When Calling AI APIs
- How to count tokens before sending a prompt to Claude
- How to Fix 'Context Length Exceeded' Token Limit Errors
- How to fix a context length exceeded error in the Claude API
- How to fix a Claude response that gets cut off mid-sentence
- How to reduce Claude hallucinations by grounding answers in your documents
- How to keep a long Claude conversation under the context limit
- How to stop Claude from calling tools when it should not
- How to handle a Claude Fable 5 refusal with a fallback model
- How to choose the right Claude model for cost and quality
- How to cut Claude API costs with prompt caching
- How to halve Claude costs for bulk jobs with the Batch API
- How to cap spend on a Claude agent with a task budget
- How to Debug an MCP Server That Will Not Connect
- How to Ask an Agent to Explain a Bug Before Fixing It
- How to Roll Back a Bad Deploy Quickly
- How to Fix Cursor Not Indexing Your Codebase
How to Fix 'Context Length Exceeded' Token Limit Errors
Resolve maximum context length errors by counting tokens, trimming input, and chunking long documents.
A context_length_exceeded error means your prompt plus the requested output is larger than the model's context window. Every model has a fixed ceiling measured in tokens, not characters. The fix is to measure how many tokens you are sending, trim what you can, and split anything too big into chunks.
- Your model's context window size, from the provider docs
- A token counting library such as tiktoken
- The long input that triggered the error
Step 1: Read what the error tells you
The message usually states the model maximum and how many tokens you tried to send. That gap is exactly how much you need to cut, plus headroom for the response.
Step 2: Count tokens before you send
Stop guessing. Count tokens locally so you can reject or trim oversized input before it ever hits the API. A rough rule is about four characters per token in English, but a real counter is exact.
import tiktoken
enc = tiktoken.encoding_for_model("gpt-4o")
text = open("doc.txt").read()
n = len(enc.encode(text))
print(f"{n} tokens")
if n > 120000:
print("Too big, must chunk")Step 3: Trim what you do not need
Before chunking, remove fat. Drop old chat history, strip boilerplate, and reserve room for the answer by leaving the max_tokens output budget out of your input total.
Step 4: Chunk long documents
If the input is genuinely larger than the window, split it into overlapping chunks, process each, then combine the results. Overlap a few hundred tokens between chunks so you do not cut a sentence or idea in half.
def chunk(tokens, size=20000, overlap=500):
out = []
start = 0
while start < len(tokens):
out.append(tokens[start:start + size])
start += size - overlap
return outResult
After counting tokens and splitting a 200-page PDF into overlapping 20k-token chunks, a summarizer that always failed now processes the whole document and merges the chunk summaries into one clean output.
Watch related tutorials
2:14
23:41
12:38
14:09
17:53
15:00New guides in your inbox
Fresh step-by-step how-to guides as we publish them. One email a week, no more.