by Martin Monperrus Tags:

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:

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:

  1. 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”.
  2. 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”.
  3. 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.

Rules of Thumb

  1. Give the number, don’t hide it. Explicit budget beats a silent max_tokens cliff: the model plans around it instead of getting truncated.
  2. 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.
  3. Over-provision on purpose. Cognition’s 1M-window/200k-cap trick is the operational form of rule 2.
  4. Pair the countdown with an escape hatch. Codex’s reminder is useful because it names the next action (notes, then new_context). A bare “you have 6144 tokens left” is a threat with no plan.
  5. Repeat the persistence instruction at the end of the prompt, not only at the top.
  6. A fake counter is a bug, not a feature. padded-countdown makes 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.
  7. 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 left

That 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 the notes tool with the goal, decisions, progress, learnings, next steps […] After saving your state, call functions.new_context to 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