Money
Estimating and controlling token spend for coding agents
Why coding agents use so many tokens, how to estimate the cost of a task, and the controls that reduce spend: context limits, prompt caching, model routing and per-task budgets.
By the BYOK Tools editorial team · 22 November 2026 · 8 min read
Coding agents are the most expensive BYOK workload most developers run. A single task can involve dozens of model calls, each carrying a large context. The costs are predictable once you know where the tokens go.
Where the tokens go
Each agent turn is a separate API request. The request typically contains:
- A system prompt with tool definitions and instructions, often several thousand tokens.
- Repository context: file contents, search results, diffs and directory listings.
- Conversation history: every previous turn, tool call and tool result in the session.
- The new instruction or tool output.
The model then returns output tokens: reasoning, code edits and tool calls. Because chat APIs are stateless, the history is re-sent on every turn. Input tokens therefore grow with each step, and on long tasks input usually accounts for most of the bill even though output tokens cost more per token.
Estimating the cost of a task
A rough estimate needs four numbers:
- Average input tokens per turn. Watch the agent's token counter, or read the usage field in API responses, over a few typical tasks.
- Average output tokens per turn.
- Turns per task. Small fixes may take a handful; multi-file features can take many more.
- Your model's input and output prices per million tokens, from the provider's pricing page.
Cost per task is roughly turns multiplied by (input tokens × input price + output tokens × output price), divided by one million. Apply the cached-input rate to the share of input that hits the cache. The cost calculator handles the arithmetic.
Controls that reduce spend
Limit context. Use ignore files so the agent skips build output, dependencies, lockfiles and generated code. Point the agent at specific files where you can. Start a new session for each unrelated task so history does not carry over.
Use prompt caching. Agent requests share a long, stable prefix: system prompt, tool definitions and earlier turns. Providers bill cached input at a reduced rate. Anthropic uses explicit cache breakpoints, while OpenAI and Google apply caching automatically for long prompts on supported models, with Google also offering explicit context caching. Check that your tool enables caching for your provider and confirm it in the usage data: Anthropic reports cache reads and writes as separate token counts, and OpenAI reports cached tokens within prompt token details.
Route by task. Not every step needs a frontier model. Use a smaller, cheaper model for file search, summarising, commit messages and simple edits, and reserve the strongest model for planning and difficult changes. Several agents support separate models for different roles. OpenRouter or a self-hosted gateway makes switching models a configuration change rather than a new account.
Set per-task budgets. Use the tool's budget or maximum-turn setting if it has one. Combine it with a per-key spend limit at the provider, so a runaway loop stops at a known cost.
Compact long sessions. Some agents summarise earlier history to shrink the context. This lowers input tokens, at some risk of losing detail.
Handle rate limits. Long runs can trigger HTTP 429 responses. Tools that respect the retry-after header recover cleanly. Persistent limits usually mean your account tier needs to rise with spend history.
Monitoring
Review spend per key in the provider dashboard each week while you calibrate. Use one key per tool so costs are attributable. If a tool reports cost per task, compare it with the dashboard figures to confirm the counters are accurate.
Our picks
These coding agents show token usage or cost per task and support multiple providers.
Browse every coding tool or command-line tool, and see the best BYOK coding agents.
Editorial independence: rankings are based on our own testing and research. Some outbound links may in future be affiliate links; that never affects scores. See our editorial policy.
Get the BYOK Brief
New listings, price changes and model releases. Fortnightly.