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.
What context cost control gives you
Section titled “What context cost control gives you”- 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
/clearcosts 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.
/usageshows the session’s token counts and an estimated cost at list price, aPrompt 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./contextdraws 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.
/statusshows the current session configuration and token usage./usageshows account usage against your plan’s five-hour and weekly windows, and lets you spend a usage-limit reset.- For scripted runs,
codex exec --jsonprints the event stream, including usage, as JSONL you can store per run.
- Open the usage view in your Cursor account dashboard and compare token use by model across a few days of normal work.
- Cursor’s plan prices, included usage and Auto-mode billing could not be re-verified for this page (cursor.com was unreachable on 2026-09-26), so read the numbers in your own dashboard rather than any published table. Cursor token management covers the Cursor-specific controls.
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.
| Prompt | What the agent does first | Risk |
|---|---|---|
| “Something is wrong with login, here is the whole auth directory” | Reads everything pasted, then searches for more | High 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 hypothesis | May 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.
Keep the always-loaded context small
Section titled “Keep the always-loaded context small”Whatever loads at session start is paid for on every turn of every session. Three things tend to grow there unnoticed.
-
Trim the instruction file. Keep
CLAUDE.mdorAGENTS.mdto commands, conventions and the constraints the agent keeps getting wrong. Anthropic recommends keepingCLAUDE.mdunder 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. -
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.
-
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:
/mcpin Claude Code, and in Codex setenabled = false(or list only the tools you want inenabled_tools) under[mcp_servers.NAME]inconfig.toml. Where a CLI such asghdoes the job, prefer it: it adds no tool listing at all. Reducing MCP token cost goes deeper.
Clear, compact or delegate between tasks
Section titled “Clear, compact or delegate between tasks”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).
/clearstarts a new conversation with empty context and costs nothing. Use/renamefirst if you want to find the old session with/resume./compact Focus on the failing test and the files changedkeeps 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: haikuin the subagent’s frontmatter; otherwise it may inherit your session’s model. See scoped subagents. - A
PreToolUsehook 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.
/newstarts a new chat in the same session;/compactsummarizes the conversation to stay under the context limit./sidestarts a side conversation in an ephemeral fork, which suits a quick question that should not land in the main thread.- To continue earlier work without re-explaining the project, resume it:
codex resume --lastfrom the shell (it reopens the most recent session in the TUI), orcodex exec resume --last "Now add rate limiting to the endpoints you created"from a script. - Keep
AGENTS.mdlayered: a short root file, andsrc/api/AGENTS.mdorsrc/web/AGENTS.mdfor area rules.
- Start a new chat for each task instead of continuing a debugging chat into a feature.
@-mention the files the task needs rather than letting the agent search the whole project.- Use subagents for exploration so the search results stay out of the main chat, and keep Rules short and scoped the same way as an instruction file.
Tune effort before you switch model
Section titled “Tune effort before you switch model”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.
| Job | Claude Code | Codex |
|---|---|---|
| Hard agentic coding (the default) | Claude Opus 5.5 at its default medium effort; raise with /effort high only when a task needs it | GPT-6 Astra, the bundled default (effort low by default) |
| Everyday work | Opus 5.5 at default effort, or Claude Sonnet 5 when cost matters | GPT-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 deliberately | Claude Fable 5.1 (/model fable), never a default, may bill to usage credits | GPT-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.
Batch related changes into one request
Section titled “Batch related changes into one request”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.
Cap spend in headless runs and CI
Section titled “Cap spend in headless runs and CI”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 -paccepts--max-budget-usd, which stops a print-mode run at a dollar amount (for exampleclaude -p --max-budget-usd 2 --model sonnet "Summarize this diff for the PR description"). The flag works only with--print. - Codex:
codex exechas no budget flag in 0.157.1 (checked 2026-09-26). Bound it with the model (-m gpt-6-lunafor 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:
- Fix the task set. Pick five to ten recent tasks of one class with known-good outcomes.
- 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.
- Compare usage and pass rate together from
/usage,/statusor the Cursor dashboard, and watch the rework rate on merged changes for two weeks. - 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.
When context cost optimization backfires
Section titled “When context cost optimization backfires”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.