SECURITY WARNING: Never run commands you don't understand. Always review code before execution. Use at your own risk.
AI Added 23 May 2026

Anthropic API: prompt is too long / context window exceeded

Input tokens plus requested output tokens exceed the selected model's context window.

Quick fix

Read the commands before running them. Anything that restarts a service, deletes data or changes permissions should be tried on a non-production system first.

Quick fix
# Count tokens first
token_count = client.messages.count_tokens(
  model='claude-3-5-sonnet-latest',
  messages=messages
)
print(token_count.input_tokens)
# Reduce max_tokens, summarize history, use RAG, or use prompt caching for static context

How to diagnose AI errors

AI and LLM errors cluster into four families: quota and rate limiting (429s, insufficient_quota), context window overflow (the prompt plus the requested completion exceeds the model's limit), accelerator memory (CUDA OOM, KV-cache exhaustion), and content policy (a refusal or safety stop rather than a transport failure). The first thing to establish is which family you are in, because the fixes have nothing in common: a 429 wants backoff and a quota increase, a context overflow wants truncation or chunking, and a CUDA OOM wants a smaller batch or a quantised model.

If the quick fix above does not resolve it, work through these steps. They apply to this whole class of error, not just to this one message, which is usually what saves the time.

  1. Read the error body, not just the status code. Providers put the real reason in a JSON error.type / error.code field: rate_limit_exceeded and insufficient_quota are both HTTP 429 but mean completely different things.
  2. Count your tokens before you send. Use the provider's tokenizer (tiktoken, Anthropic's count-tokens endpoint) rather than guessing from character length, and remember that max_tokens for the response is reserved inside the context window.
  3. For local inference, watch VRAM live with nvidia-smi -l 1 while the request runs. Memory that peaks during the forward pass rather than at load time points at batch size or sequence length, not at model weights.
  4. Distinguish a refusal from a failure. A stop_reason of refusal, or a finishReason of SAFETY, is a successful HTTP 200. Retrying identical input will produce the same result.
  5. Always implement exponential backoff with jitter and honour the retry-after header. Most production LLM incidents are self-inflicted retry storms.

Tools worth reaching for

  • tiktoken
  • nvidia-smi
  • curl -i
  • provider status pages

Authoritative references

Primary documentation for this error, worth reading before applying any fix in production.

docs.anthropic.com

Related AI errors

See all 35 AI errors →

Browse other categories

Something missing or wrong?

This entry is maintained by hand. If the fix is out of date, incomplete, or you have a better one, email a correction and it will be reviewed.