Coding agent cost control.
Operations guide · 1 July 2026
Coding agents (Claude Code, Cursor, Devin-style autonomous developers) solve one LLM cost problem — they optimize the task itself — and create a new one: they can loop forever. If your agent retries a failing test without finding the root cause, or if it cycles through identical edits trying to satisfy a contradictory requirement, the task can balloon from $0.10 to $5 in seconds. Stop that with termination signals and per-task budgets.
Why coding agents loop
Coding agents cost differently from chat agents. A chat interaction has one request and one response. A coding task has edit cycles: the agent reads code, makes a change, runs tests, checks the result, and tries again. If the tests keep failing, or if the agent thinks one more retry will succeed, it loops.
Three patterns create runaway loops:
- Test failures it cannot solve. A test fails for reasons outside the agent's control (flaky CI, missing environment variable, external API timeout). The agent retries the edit, the test still fails, and it retries again.
- Contradictory requirements. The task asks the agent to "reduce execution time" and "add detailed logging." The agent trades off: faster execution, then slower but more verbose, then back to faster. Each edit looks slightly different, so the agent thinks it is making progress.
- Edit-verify cycles without bounds. The agent has no hard stop. It keeps running tests and making small tweaks because "one more retry might do it."
Termination signals: the minimum guardrails
Coding agents need explicit, non-overrideable stop conditions. Here are the four that matter:
1. Max turns per task
A "turn" is one edit cycle: one code change plus one test run. Cap at 8–10 for most tasks. If the agent does not solve the problem in 10 edits, it is not going to solve it with 15. At the limit, the agent must either succeed, escalate to a human, or degrade to a simpler goal. Do not let it retry indefinitely.
2. Max tokens per task
Set a token budget independent of turn count. If each turn costs 10K tokens, a 10-turn task costs 100K tokens ($0.30 at Sonnet pricing). If you allow unlimited turns but cap total tokens, the agent exhausts the budget more predictably than it does turn limits. Use both: max turns catches runaway loops early; max tokens is the financial backstop.
3. No-progress detection
If the agent makes the exact same change twice, or if the diff is identical across multiple edits, it has looped. Detect this by comparing the hash of the code state after each turn. If the code state repeats, stop and fail the task. This is cheaper than waiting for the token budget.
4. Wall-clock timeout
Even with the above guardrails, a slow test suite can eat time. Set a hard clock limit (e.g., 5 minutes per task, 30 minutes for big refactors). When time is up, the agent stops, returns what it has, or escalates.
Cost-control levers specific to coding agents
Context compaction
Coding agents are greedy for context. They want to see the entire repo, full git history, and all test output to make the safest decision. But that eats tokens. Instead, feed only what the agent needs per turn:
- Show the changed files plus their direct imports, not the entire codebase.
- Show only the failing test names and their error messages, not the full test output.
- Show the last 3 commits, not the last 30.
- Show one error at a time, not ten.
This cuts context size by 50–70% per turn and reduces cost proportionally, with no loss of agent capability.
Cheaper model for mechanical steps
Not every edit needs the frontier model. If the agent is doing a straightforward change — fixing a formatting error, renaming a variable, adding a missing import — use a smaller model like Haiku. Reserve Claude 3.5 Sonnet for test interpretation, architectural decisions, and root-cause analysis.
Route to the cheaper model when the task is mechanical: the instructions are unambiguous, the required change is obvious from the test error, and there is little room for interpretation. This can cut per-task cost by 40–60% without quality loss.
Cache-friendly prompts
Coding agents send the same system prompt and context setup on every turn. Use prompt caching to lock in the expensive context (system instructions, codebase structure, testing framework docs) and pay once. Each subsequent turn reuses the cached prefix, cutting input token cost by 50–90%.
Structure the prompt as: (1) cached system instructions (agent role, code style rules), (2) cached repo overview and test framework docs, (3) non-cached current edit, (4) non-cached test result. This way you pay full price once and near-zero on retries.
Per-task token budgets
Instead of global token limits, set per-task budgets aligned to task type. Simple fix (one file, obvious solution): 20K tokens. Medium refactor (3–5 files, multiple test fixes): 80K tokens. Complex redesign (10+ files, architectural decision): 200K tokens. When the agent exceeds the budget for its task category, it fails and escalates. This makes cost predictable per workflow type.
Monitoring per-task cost
Instrument three things for every coding task:
- Estimated and actual cost. Compute estimated cost at task start (model + expected tokens). Log actual cost at finish. The delta reveals whether your token estimates are tight.
- Turn count and tokens per turn. Track how many edits it took and the average tokens per edit. If median turns trend up, your task scoping is getting looser.
- Success vs guardrail termination. Did the task complete within budget, or did it hit the max-turns limit? If >20% of tasks hit limits, the limits are too tight. If 0% hit limits, they are too loose.
Send this data to an observability tool (Langfuse, Helicone, or LiteLLM) and set up alerts: page if a single task exceeds $1, or if weekly median cost per task category (refactor vs bug fix) rises 20% month-over-month. Track cost by task type so you can catch regressions early.
The economics of task routing
A simple heuristic: for tasks that cost <$0.50 (simple bug fix, one-file change), use one agent with tight guardrails. For tasks that cost $0.50–2 (multi-file refactor), add a cheaper-model fallback. For tasks >$2 (major redesigns), route to a human with the agent as a helper, not the decision-maker. This keeps most tasks cheap while avoiding infinite loops.
Related
- Agent spend guardrails: budgets, retries, and loop control — the parent concept, covering all agents.
- Prompt caching: OpenAI vs Anthropic vs Bedrock — how to lock in expensive context for multi-turn tasks.
- Model routing without quality regressions — the mechanics of swapping models by task type.
- Reasoning tokens are the hidden line item on your AI bill — why some reasoning-heavy tasks cost more than expected.
- LLM cost per request — unit economics for AI features.