Skip to content

Context cost optimization for coding agents

Context cost optimization for coding agents means cutting the tokens an agent processes per accepted change, not cutting how much you use the agent. The biggest levers: measure usage first, scope each task tightly, keep always-loaded context small, clear between tasks, and tune effort before switching model. A cheaper session only counts if tests still prove the result.

It is the third week of the month. You have hit your Claude Code limit twice this week, while a colleague on the same plan still has headroom and ships as much. The difference is rarely how often they use the agent: it is that their sessions carry less dead weight. This page is for developers who want their plan or API budget to last, and for tech leads who need a spend baseline for the team without trading away quality.

  • A way to see where the tokens in one session went, in each of the three tools.
  • Six copy-paste prompts that scope, batch, delegate, audit and A/B-test agent work for cost.
  • A model-and-effort routing table that follows the vendors’ own defaults.
  • Spend caps for headless and CI runs, and a cost-per-accepted-change measure that shows whether savings cost you quality.

Where does the money go in an agent session?

Section titled “Where does the money go in an agent session?”

An agent session is not one request. Every turn resends the system prompt, your CLAUDE.md or AGENTS.md, the tool list, and the whole conversation so far, including every file the agent read and every command output it saw. Each tool call adds another request carrying those results. So cost grows with the size of the context multiplied by the number of turns, and a one-line question late in a long session still pays for everything above it.

Four mechanics decide how much of that you pay for:

  • Prompt caching. Repeated context is billed at the much lower cache-read rate while the cache is warm. Anthropic’s cost documentation (read September 2026) gives Claude Code a one-hour cache lifetime on a subscription (five minutes once you are drawing on usage credits) and five minutes by default on an API key or cloud provider, so the first message after a longer break reprocesses the full context.
  • Thinking tokens. Reasoning is billed as output. Effort level controls how much of it the model spends.
  • Subagents and parallel agents. Each one sends its own requests on top of yours. Anthropic’s documentation puts agent teams (an experimental feature, off by default) at roughly seven times the tokens of a standard session when teammates run in plan mode.
  • Compaction. Summarizing a long conversation is itself a large request. Starting fresh with /clear costs nothing.

How that turns into money depends on how you pay. On a subscription (Claude Pro, Max, Team or Enterprise seats; ChatGPT plans that include Codex), usage draws down rolling five-hour and weekly allowances, and waste means hitting the limit sooner. On an API key or bring-your-own-key setup, every token is a line on the invoice. Per-token prices change often, so this site keeps them in one place: the model comparison hub. For scale, Anthropic reports an enterprise average of “around $13 per developer per active day and $150-250 per developer per month” for Claude Code (Claude Code cost documentation, checked 2026-09-26). Plan prices and plan fit are compared in pricing analysis.

Measure a baseline before you cut anything

Section titled “Measure a baseline before you cut anything”

Optimize what you can see. Run two or three representative tasks as you normally would, then read the usage view before you change a habit.

  • /usage shows the session’s token counts and an estimated cost at list price, a Prompt cache (main) line with the share of input served from cache and the number of misses, and, on Pro, Max, Team and Enterprise plans, a breakdown that attributes recent usage to skills, subagents, plugins and individual MCP servers.
  • /context draws the current context as a grid and flags context-heavy tools and memory files.
  • A status line can show context usage continuously; see context windows.

Scope the task before the agent starts exploring

Section titled “Scope the task before the agent starts exploring”

Unfocused exploration is the largest avoidable cost. “Fix the auth bug” sends the agent reading every file that looks related. Naming the file, the symptom and your hypothesis gives it a starting point and a stopping point.

PromptWhat the agent does firstRisk
“Something is wrong with login, here is the whole auth directory”Reads everything pasted, then searches for moreHigh cost, ambiguous goal
“Fix the intermittent TokenExpiredError in src/auth/login.ts; I suspect clock skew between token.ts and verification”Reads two files, tests the hypothesisMay still ask for a dependency; that is fine

Four rules do most of the work: name the files, state a hypothesis even if you are unsure, define done (which test must pass), and batch genuinely related changes into one request.

Whatever loads at session start is paid for on every turn of every session. Three things tend to grow there unnoticed.

  1. Trim the instruction file. Keep CLAUDE.md or AGENTS.md to commands, conventions and the constraints the agent keeps getting wrong. Anthropic recommends keeping CLAUDE.md under 200 lines. Put area-specific rules in nested files that load only when the agent works in that directory. A short, accurate file also saves tokens the agent would otherwise spend rediscovering your build and test commands. Pruning context files shows how to prove each line earns its place.

  2. Move workflow instructions into skills. A skill loads its body only when invoked; at startup the agent sees its name and description, which the Agent Skills specification puts at about 100 tokens (agentskills specification, checked 2026-09-26). Your PR-review checklist or migration procedure costs almost nothing on the days you do not use it.

  3. Disconnect MCP servers you are not using. Claude Code and Codex both defer MCP tool definitions through tool search, so an idle server costs far less than it used to, but its name, instructions and any tool results you pull still enter context. Turn off what you do not need: /mcp in Claude Code, and in Codex set enabled = false (or list only the tools you want in enabled_tools) under [mcp_servers.NAME] in config.toml. Where a CLI such as gh does the job, prefer it: it adds no tool listing at all. Reducing MCP token cost goes deeper.

Stale context is paid for on every later turn. When one task ends, decide what the next one needs from it: nothing (clear), a summary (compact), or only a result (delegate).

  • /clear starts a new conversation with empty context and costs nothing. Use /rename first if you want to find the old session with /resume.
  • /compact Focus on the failing test and the files changed keeps a focused summary. It is a large request itself, so compact deliberately, not every few turns.
  • Delegate verbose work, such as running the full suite or reading logs, to a subagent so only its summary returns. Pin cheap subagents to a smaller model with model: haiku in the subagent’s frontmatter; otherwise it may inherit your session’s model. See scoped subagents.
  • A PreToolUse hook can rewrite test commands to print failures only, which turns tens of thousands of tokens of passing output into a few hundred. See hooks as guardrails.

The rule this site follows, taken from the vendors’ own guidance: start on the tool’s default model, tune effort before switching model, and switch model only when your own evals say so. Anthropic’s Claude Code documentation states that “Opus 5.5 at medium matches or exceeds Opus 5 at high”, which is why lowering effort on routine work is usually the first cost lever, not the model picker.

JobClaude CodeCodex
Hard agentic coding (the default)Claude Opus 5.5 at its default medium effort; raise with /effort high only when a task needs itGPT-6 Astra, the bundled default (effort low by default)
Everyday workOpus 5.5 at default effort, or Claude Sonnet 5 when cost mattersGPT-6 Sol
Cheap, high-volume work (classification, extraction, simple edits, subagent fan-out)Claude Haiku 4.5 (retirement not before 2026-10-15; no deprecation notice as of 2026-09-26)GPT-6 Luna
Longest-horizon, hardest reasoning, chosen deliberatelyClaude Fable 5.1 (/model fable), never a default, may bill to usage creditsGPT-6 Astra at a higher effort

Commands you need: in Claude Code, /model and /effort in a session, or claude --model sonnet --effort low at launch. In Codex, /model picks model and reasoning effort together; at launch use codex -m gpt-6-luna or codex -c model_reasoning_effort="low". Claude Code’s defaults above are those of the latest release channel from v2.1.280; the stable channel still had older defaults on 2026-09-26, so check /model rather than assuming. In Cursor, no default model or model price could be verified for this page; apply the same evaluation to whatever your plan offers. Current prices for every model are on the model comparison hub, and model routing turns this table into a team policy.

Separate requests each reload the base context: instructions, tool list and history. Three small prompts that touch the same files usually cost more than one that does all three, but only when the work is genuinely related. There is no fixed multiplier, so compare the usage view before and after.

Automation multiplies cost, because every pull request or schedule starts a new session and nobody watches it. Set a hard ceiling where the tool offers one and a cheaper model where quality allows.

  • Claude Code: claude -p accepts --max-budget-usd, which stops a print-mode run at a dollar amount (for example claude -p --max-budget-usd 2 --model sonnet "Summarize this diff for the PR description"). The flag works only with --print.
  • Codex: codex exec has no budget flag in 0.157.1 (checked 2026-09-26). Bound it with the model (-m gpt-6-luna for triage), a job timeout in CI, and the spend limits on the API key or workspace.
  • Every provider: set spending limits on API keys used by automation, so a runaway loop hits a ceiling instead of an invoice.

Run cheap, bounded jobs (PR descriptions, triage labels) on every pull request; run broad ones (whole-repository analysis) on a schedule, not per PR. Keep CI agents read-only unless the job must write, and load secrets from the CI secret store; CI/CD pipelines shows the secure setup.

How do you prove a cheaper setup did not cost quality?

Section titled “How do you prove a cheaper setup did not cost quality?”

Token count is the wrong target on its own. Track cost per accepted change: tokens or dollars spent on a task class, divided by the changes that merged and passed CI without a follow-up fix within two weeks. A setting that halves tokens but doubles rework has made things worse.

To check a change in model, effort or context without reading every diff:

  1. Fix the task set. Pick five to ten recent tasks of one class with known-good outcomes.
  2. Gate on machines, not impressions. A run counts as a pass only if the type check, lint and the relevant tests pass, and a review agent or continuous evals report no new findings.
  3. Compare usage and pass rate together from /usage, /status or the Cursor dashboard, and watch the rework rate on merged changes for two weeks.
  4. Record the decision. The tech lead signs off the new default for that task class and notes the evidence, so the next model release is re-tested against the same set.

The test stage of the lifecycle describes the verification loop these gates plug into.

The cheapest model gets the hard task. The result needs three rounds of fixes and costs more than the default would have. Recovery: route by task class using the table above, and let the A/B gate, not the price list, decide a downgrade.

Compaction drops a constraint. After /compact, the agent reintroduces an approach you rejected earlier. Recovery: put durable constraints in the instruction file, not the chat, and give /compact a focus instruction. See context windows for what survives a summary.

A long break makes the next message expensive. The cache has expired and the whole context is reprocessed. Recovery: clear or start a new session after a break when you do not need the history; on Claude Code Pro and Max plans, accept the offer to resume from a summary.

Subagent fan-out quietly multiplies usage. Parallel agents each carry their own context and requests. Recovery: check the attribution breakdown in /usage, pin subagents to a smaller model, and keep parallel tasks small and self-contained.

A headless job loops overnight. Recovery: --max-budget-usd on claude -p, CI timeouts on codex exec, and spend limits on every automation key.

Nobody on the team can see spend. Individuals cannot optimize what they cannot see. Recovery: share usage views in retros, and have the tech lead track cost per accepted change per task class. Cost governance covers organization-level attribution and budgets.

You optimize every prompt by hand. Ten minutes polishing a prompt to save a few thousand tokens loses more than it saves. Recovery: fix the few biggest drivers from your baseline (scope, instruction-file size, idle context, model and effort) and stop there. If you still hit plan limits on well-scoped work, the plan is too small: judge a plan by accepted output, time saved and rework, and upgrade when the limit costs more than the next tier.