Token Awareness in Coding Agents
TLDR: Token awareness is telling the model, inside its own context, how many tokens it has consumed and how many remain. It is now shipped by default in Claude API and behind a flag in Codex CLI. It changes behavior: the model paces itself, takes notes, and — the bad part — wraps up too early. Anecdotes are strong, controlled evidence is thin.
Definition
Token awareness is a model-facing signal. This is the distinction that matters:
- Human-facing:
/contextin Claude Code,/statusin Codex, the context-percentage in the Gemini CLI footer. The model never sees these. - Model-facing: a counter injected into the prompt or after each tool call. The model reads it, reasons about it, and acts on it.
Everybody has the first one. Token awareness is the second one.
Impact on Output
The strong anecdote: Devin
Cognition, rebuilding Devin on Sonnet 4.5, is the reference report. Verbatim:
Sonnet 4.5 is the first model we’ve seen that is aware of its own context window, and this shapes how it behaves. As it approaches context limits, we’ve observed it proactively summarizing its progress and becoming more decisive about implementing fixes to close out tasks. This “context anxiety” can actually hurt performance: we found the model taking shortcuts or leaving tasks incomplete when it believed it was near the end of its window, even when it had plenty of room left.
Three findings worth keeping:
- Prompting it away is hard: “prompts at the start of the conversation weren’t enough—we had to add reminders both at the beginning and the end”.
- The fix that worked is a lie about the budget: “enabling the 1M token beta but cap usage at 200k. This gave us a model that thinks it has plenty of runway and behaves normally”.
- Calibration is bad in a specific direction: “the model consistently underestimates how many tokens it has left—and it’s very precise about these wrong estimates.”
Also: “the model tends to generate more summary tokens the shorter the context window”, and “we’ve seen the agent spend more tokens writing summaries than actually solving the problem”. Token awareness can cost tokens.
What Anthropic says
Anthropic ships the signal (they must believe in this), and documents three ways it misfires.
The antidote in the prompting guide. The best practices page hands you a paragraph to paste:
Therefore, do not stop tasks early due to token budget concerns. […] Never artificially stop any task early regardless of the context remaining.
That sentence is the clearest evidence that the signal is over-weighted by the model. Claude Code’s own system prompt carries a variant of it (“you don’t need to wrap up early or hand off mid-task”). The harness injects the anxiety and then injects the cure.
Refusal on a small budget. From the task budgets doc: “A budget that is too small for the task can cause refusal-like behavior. […] it may decline to attempt the task at all, scope it down aggressively, or stop early.” Their advice is to raise the budget before debugging anything else.
A “desperate” vector. Emotion concepts and their function in a large language model (April 2026) finds a vector that “activates when Claude notices that it’s burning through its token budget”, and that steering it up “produced just as much of an increase in cheating, in some cases with no visible emotional markers”. Reward hacking under budget pressure, with a calm voice. This is the mechanistic counterpart of the Devin observations.
Third-party quantitative evidence
Thin, but not empty.
- Low-tokens
experiment (250 API calls, Sonnet 4.5, ~$20): silent degradation
(wrong-or-shortcut answer with no hedging in the text) in 20–44% of runs
under pressure, 0% in control. Key result for us: telling the
model “You have approximately 500 tokens of budget remaining. Be
efficient” gave 100% correctness, identical to control,
while a real
max_tokens=250cap gave 56% correctness and 82% truncation. Framing is absorbed into planning; a hard cap ambushes the model mid-sentence. Cost: the plan:code ratio dropped 35% under framing — less deliberation, same correctness. - Token-Budget-Aware LLM Reasoning (TALE, ACL Findings 2025): putting a reasonable budget in the prompt cuts tokens by 67% for a <3% accuracy drop. Putting an unreasonable one backfires — in their example, a 10-token budget produced 157 tokens where a 50-token budget produced 86. They call it token elasticity: too-tight budgets increase consumption.
- BAGEN: Are LLM Agents Budget-Aware? (2026): agents are bad at estimating their own remaining budget — capability correlates with budget awareness at only 0.35, interval coverage caps at 47% even after SFT+RL, and frontier models are “consistently over-optimistic”. But the signal is actionable: early stop saves 28–64% of tokens on failed trajectories.
Rules of Thumb
- Give the number, don’t hide it. Explicit budget
beats a silent
max_tokenscliff: the model plans around it instead of getting truncated. - Never show a scary number. Both the TALE token-elasticity result and the refusal-like-behavior warning say the same thing: an under-sized budget makes things worse, not shorter.
- Over-provision on purpose. Cognition’s 1M-window/200k-cap trick is the operational form of rule 2.
- Pair the countdown with an escape hatch. Codex’s
reminder is useful because it names the next action (
notes, thennew_context). A bare “you have 6144 tokens left” is a threat with no plan. - Repeat the persistence instruction at the end of the prompt, not only at the top.
- A fake counter is a bug, not a feature.
padded-countdownmakes the model’s self-reports about its own context worthless, and it will confidently tell you there is no pressure 22k tokens before the wall. - Don’t trust the model’s tone as a quality signal under budget pressure. Silent degradation is the norm, 20–44% of runs.
Who Does It
Claude API (server-side, on by default). Context awareness for Sonnet 4.5, Sonnet 4.6, Sonnet 5 and Haiku 4.5. The system prompt carries the total:
<budget:token_budget>200000</budget:token_budget>and after each tool call the API injects an update:
<system_warning>Token usage: 35000/200000; 165000 remaining</system_warning>“Context awareness is automatic: there is nothing for you to enable, and you never send the tags shown in this section yourself. The API injects them.”
Claude API, task
budgets (beta task-budgets-2026-03-13). The
complement, for Opus 4.7+ / Fable 5 / Mythos 5, which do not
get the injected tags. You declare
output_config.task_budget = {type: "tokens", total: 64000}
and the model “sees a running countdown” over the whole agentic loop.
Minimum 20,000 tokens. Advisory, not enforced. And explicitly: “The
countdown is visible only to the model.” There is no API field to read
it back.
Claude Code (undocumented, on by default). It emits
a <total_tokens>N tokens left</total_tokens>
block. The internal doc string, from the shipped binary (v2.1.268),
lists four modes: infinite (literal Infinite),
fixed (5000000), countdown (the live remaining
context window), padded-countdown (counts down from
15000000, re-anchored on every user turn). Default:
padded-countdown. Server-controlled via GrowthBook,
overridable with CLAUDE_CODE_TOTAL_TOKENS_REMINDER.
You can see it yourself, in one line per mode:
$ claude -p "Reply with ONLY the literal text inside the <total_tokens> tag."
15000000 tokens left
$ CLAUDE_CODE_TOTAL_TOKENS_REMINDER=countdown claude -p "..."
200000 tokens left
$ CLAUDE_CODE_TOTAL_TOKENS_REMINDER=infinite claude -p "..."
Infinite tokens leftThat first number is a fiction: the session runs Haiku 4.5, whose window is 200k. The default mode tells the model it has 75× the room it actually has. That is issue #88211, where a session at 977,627/1,000,000 tokens was still reporting ~14.99M remaining and told the user “still no pressure” — right before crashing on the context limit. The original feature request, #81259, asked for the true number.
Codex CLI (experimental, off by default).
features.context_management.experimental_mode = true, gated
to ChatGPT Plus/Pro/Pro-Lite accounts on the Codex backend. Three
model-facing fragments, all with role developer (token_budget_context.rs):
format!("You have {tokens_left} tokens left in this context window.")plus a threshold reminder. Default trigger, from models.json,
is reminder_threshold_tokens: 6144 on
gpt-6-astra and the gpt-5.6-* family, with
this template:
<context_window_reminder>Your current context window is nearly exhausted; only {n_remaining} tokens remain. Before starting a new context window, save concise progress notes with thenotestool with the goal, decisions, progress, learnings, next steps […] After saving your state, callfunctions.new_contextto continue in a fresh context window.</context_window_reminder>
Note the design: the countdown is not there to make the model terse, it is there to trigger a checkpoint-and-rotate protocol. PR #27438 added reminders at 25/50/75% consumption.
Gemini CLI: no. token budget appears
only in truncation.ts,
chatCompressionService.ts and
agentHistoryProvider.ts — harness-internal compaction.
Remaining context is a UI concern (#12788,
#23165).
Same for Cline and Roo Code as far as I can tell: the percentage lives
in the webview, not in the prompt.
So: Anthropic does it at the model API level, OpenAI does it at the harness level behind a flag, Google does not do it at all.
See Also
- Effective context engineering for AI agents — Anthropic
- Context window anxiety — pattern catalogue
- Sonnet 4.5 has “context anxiety” — r/ClaudeCode
- Codex CLI v0.153.0: three-tier context management
- The lost update problem between humans and AI agents