Estimating AI Agent Task Cost: Multi-Turn Calls and Context Growth
The cost of an agent task is roughly "number of turns x the context that has to be re-sent to the model on each turn," not the few sentences you typed. Double the turns and input tokens usually more than double, because the context keeps growing on every turn. Below is a formula and a worked example (with its assumptions stated) showing how to estimate it.
Why do agent tasks use more tokens than a normal chat?
A normal Q&A is one question, one answer, one call. An agent task (say, asking a coding tool to fix a bug or refactor a module) is a loop: the model reads files, calls tools, looks at the results, and decides the next step. It often takes dozens of calls to finish.
Model APIs are stateless. On every call, the client re-sends the earlier conversation and tool results, along with the system prompt and tool definitions. So the input for turn N contains everything from the previous N-1 turns. Each turn looks small on its own, but they add up.
Two more things amplify the cost:
- Tool results are often much longer than your prompt, for example a few-hundred-line file or a chunk of test logs.
- Reasoning models produce extra thinking tokens. Whether and how those are billed depends on the official documentation; see How reasoning tokens are billed.
How do you estimate the tokens for an agent task?
Define a few quantities, all of which you can read from logs or a usage page:
- B: the fixed overhead on every turn, meaning the system prompt, tool definitions, project instruction files, and so on. Think of it as "what you carry even if you do nothing."
- d: the new content added per turn, i.e. the tool result plus the model's output from the previous turn.
- n: the number of turns, i.e. model calls.
- o: the average output tokens per turn.
The input for turn k is about B + (k-1)·d, so for the whole task:
- Total input ≈ n·B + d·n(n-1)/2
- Total output ≈ n·o
The second term grows with the square of n, which is why long tasks get expensive quickly.
Worked example (all numbers are assumptions, not measurements)
Assume B = 8,000 tokens, d = 2,000 tokens (about 1,500 of tool results plus 500 of model output), and o = 500.
| Turns n | Total input (tokens) | Total output (tokens) |
|---|---|---|
| 10 | 170,000 | 5,000 |
| 30 | 1,110,000 | 15,000 |
| 60 | 4,020,000 | 30,000 |
Going from 30 to 60 turns doubles the turn count, but input rises to about 3.6 times. Replace these figures with your own records; don't apply this table directly.
Do caching and the input/output price gap change the result?
Yes. The calculation above gives token counts; converting to money depends on the price list. Two things to check, with the exact rules governed by the official documentation of the service you use:
- Input and output usually have different unit prices, with output typically costing more; see Why output tokens cost more than input. In the example, input makes up the vast majority, so input pricing dominates.
- Some services cache repeated prefixes and price cache hits differently. The prefix of a long task is mostly repeated, so the hit rate can affect the total a lot, but whether it is supported and how it is billed is something to confirm in the official docs.
If you are on a plan metered by quota, check how it converts input, output, and cache; don't assume it matches the official API.
How do you calibrate the formula with your own data?
- Pick a typical task you do regularly and run it through to completion.
- From the tool's usage page or the
usagefield in API responses, record the number of calls n, total input, and total output. - Divide total input by n to get average input per turn, then work backward to B and d using the formula.
- Run similar tasks a few times and take the median rather than the mean, because failed and retried tasks pull the mean up.
- Tasks per month x tokens per task gives your monthly usage.
For a fuller recording method, see How to estimate your monthly token usage: a three-step logging method. For Claude Code, see How to estimate Claude Code token consumption.
How can you reduce the cost of long tasks?
All of these reduce the number of turns or the amount accumulated per turn. Results vary by tool, so try them on a small scale first:
- Split tasks. Splitting one 60-turn task into two 30-turn tasks cuts input from about 4.02 million to about 2.22 million tokens in the example above (same B and d assumed).
- Start a new session instead of chatting on in one session forever. The longer the context, the more each turn costs.
- Trim project instruction files and tool definitions; they are the main source of B.
- Give a clear scope, such as specific files or specific errors, to reduce the turns the model spends reading files blindly.
- When command output is long, filter it before handing it to the model, for example keeping only the failing tests.
Should heavy tasks and everyday tasks be billed separately?
I recommend tracking them separately. Completions, formatting fixes, and small questions during a day are short per call and don't accumulate. Long-chain tasks such as refactors and cross-file debugging can cost as much as dozens of everyday calls in a single run. Averaging them together hides which kind of work is costing money and makes it harder to pick a plan.
In practice: tag each task as either "everyday" or "heavy" in your log and total the tokens for each over a month. Whether a plan is enough, and whether to buy separate quota for heavy tasks, depends on those two numbers. For what to do when quota runs out, see What to do when your AI plan quota runs out; for the overall value calculation, see How to calculate the value of an AI subscription.
What are the options if you want both kinds of tasks under one quota?
A disclosure first: AGI Plan is a commercial partner of cdnping. What follows only describes its positioning; whether it suits you is for you to judge against your own usage.
AGI Plan is a multi-provider LLM aggregation gateway sold as a monthly subscription, paid in RMB, with no overseas credit card needed. It is organized into capability groups: a frontier reasoning group for long-chain agents and complex refactors, a balanced general group for everyday coding and code review, and a fast lightweight group for completion, classification, and formatting. Tools such as Claude Code and Codex only need an endpoint URL change, and one quota can be used across the groups. That matches the "separate heavy and everyday tasks" idea above: send heavy tasks to the frontier reasoning group and everyday work to the lighter groups.
Caveats: it is not an officially authorized channel; some models are marked "overseas only," so check agiplan.dev/models for specifics; plans and metering are as described at agiplan.dev/pricing. Setup steps are at agiplan.dev/startup.
What should you do next?
- Pick a typical heavy task and record n, total input, and total output following the steps above.
- Record a week of everyday tasks and tally them separately.
- Compare plans using those two numbers, not "how many conversations."
- To see whether API responses are stable, check the probe results for each provider's API endpoints at /en/status, and read /en/learn/ai-api-latency-china to see which segments make up latency. Model additions, removals, and price changes can be followed at /en/models.