ReAct Pattern: Interleaving Reasoning and Action for LLM Agents › Context Length and Token Growth
Language AI HandbookAll chapters ReAct Pattern: Interleaving Reasoning and Action for LLM Agents › Context Length and Token GrowthOne section · Sections in this chapter7 / 14 ↓
Reading tools Definitions & inline links
Choose your expertise level to adjust how many terms are explained. Beginners see more tooltips, experts see fewer to maintain reading flow. Hover over underlined terms for instant definitions.
Article links
Make inline references clickable
One of the most practically important aspects of ReAct is that context grows with each iteration. The prompt at step contains:
where is the length of the system prompt (including few-shot examples), and each iteration adds tokens for a thought and action plus the resulting observation. For typical ReAct setups with roughly 350 tokens for the system prompt and roughly 280 tokens per iteration (about 80 for the thought, 40 for the action, and 160 for the observation), the context length at step is approximately:
This linear growth has real consequences. Modern models support context windows of 8k, 32k, 128k, or even 1M tokens, but most API calls are billed per token, and latency increases with context length. An agent running 20 iterations with verbose observations could easily consume 10,000 tokens per LLM call, and it makes 20 such calls, for a total of 200,000 tokens for a single user question. At typical API pricing, this adds up quickly.

The chart shows that even with a modest 4k context window, the agent starts hitting limits around iteration 13. With modern 128k context windows, this is less immediately problematic, but the cost and latency implications remain. Several strategies address this:
- Trajectory truncation: Drop the oldest few iterations when the context approaches a threshold. This loses some historical context but keeps the agent running.
- Observation summarization: Instead of storing the full observation, store a one-sentence summary. This is lossy but dramatically reduces token usage.
- Selective retention: Keep all thoughts and actions, but only keep observations when they contributed new information. Redundant or failed observations are discarded.
- External memory: For very long sessions, store the trajectory in a database and retrieve only the most relevant past steps using semantic similarity search.
The right strategy depends on the task. For short research queries (5-10 steps), the raw trajectory works fine. For complex research or coding tasks that might run dozens of iterations, some form of compression becomes necessary.