Guide

Common token problems, and how to solve them

Published 16 Nov 2026 · Problem, method and result · Every source is a checked official link

All guides · All case studies

Problem

A long chat or a large document set can cause four failures. The request hits the context window limit. The answer is cut before it is complete. Input cost increases quickly. Quality decreases as the context grows, a problem that Anthropic calls context rot. The official text in Context windows is "As token count grows, accuracy and recall degrade, a phenomenon known as context rot."

Method

  1. Count all parts of the request. The system prompt, messages, tools, tool results, images, documents, and the new output all count toward the context window.
  2. Learn the limits for your model from the official tables. Current Claude models in the 1M group have a 1M-token context window and can generate up to 128k output tokens. Claude Sonnet 4.5 (deprecated) and some other Claude models have a 200k-token context window.
  3. If the input alone is longer than the window, the Anthropic API returns a 400 invalid_request_error ("prompt is too long").
  4. If generation fills the window, the response can stop with stop_reason equal to model_context_window_exceeded. Treat that response as truncated.
  5. If the response stops with stop_reason equal to max_tokens, increase max_tokens or continue the response. Do not trust a cut answer.
  6. Send only the data that the task needs. Do not keep old tool results in the active chat after you record the facts that matter.
  7. For a long chat, compact old turns. Write durable facts to a file or to memory, or start a fresh session. Do not rely on a large window alone.
  8. Watch usage and price bands. On xAI, long context rates apply when the prompt reaches 200k tokens. Those rates apply to all tokens in the request. On Claude Haiku 5.5, prompts over 100,000 tokens cost more.

Result

The user can stay in the context window, find truncated answers, and keep the active context short. Cost grows more slowly, and quality is more stable, when each request carries only the necessary tokens. A large context window does not remove the need to curate tokens. Cached tokens still occupy the window. Prompt caching changes the price, not the count.

Sources

  1. Anthropic, Context windows: https://platform.claude.com/docs/en/build-with-claude/context-windows
  2. Anthropic, Stop reasons and fallback: https://platform.claude.com/docs/en/build-with-claude/handling-stop-reasons
  3. Anthropic, Effective context engineering for AI agents: https://www.anthropic.com/research/effective-context-engineering-for-ai-agents

More write-ups: case studies. Related study: context rot.