All Pulse entries

What Does an AI Coding Agent Task Actually Cost? Measuring Cost per Pull Request

Provider invoices show spend per API key, not per task. Here is how to measure what a Claude Code or Codex task costs, what drives the number, and one real recorded run: 254,617 tokens for $0.17.

Short answer: the cost of an AI coding agent task is the sum, over every model call in the run, of input tokens, cached input tokens and output tokens, each multiplied by that model’s price. You cannot get it from a provider invoice, because invoices are per API key. You have to record usage per call and attach it to the task. One run we recorded end to end used 254,617 tokens and cost $0.17. That is a single example, not a benchmark, and the reasons it was cheap matter more than the figure.

Why can’t I read the cost off my provider invoice?

An Anthropic or OpenAI invoice groups spend by key, workspace and model. A key is shared by many tasks, and often by many people. So the invoice can tell you that the team spent a given amount on a given model in November. It cannot tell you:

  • what the login refactor cost,
  • whether the larger model was worth it for bug fixes,
  • which task spent four times the average because the agent kept failing the same test.

Those are the questions you need answered to decide what to delegate. They all need the same thing: usage recorded per model call and tied to a unit of work.

What exactly should I measure?

Four numbers per model call:

FigureWhat it isWhy it matters
Input tokensNew context sent to the modelGrows with repository context and conversation length
Cached input tokensContext the provider served from its prompt cacheBilled at a fraction of the normal input price
Output tokensWhat the model wroteUsually the most expensive per token
ModelWhich model answeredPrices differ by a large factor between tiers

Multiply each by the provider’s current price for that model and add them up over the run. Add the runs up over the task. Check your provider’s pricing page for the rates: they change, which is one more reason to store raw token counts and not only a converted amount.

Two details people get wrong:

  1. Per-call beats per-session. A total at the end of a session cannot tell you where the money went. Per-call figures show the turn where the agent re-read half the repository.
  2. Reconcile against the provider. Whatever you compute locally should match the provider’s own totals for the session. If it does not, trust the provider and fix your accounting.

One real run, in numbers

This is the run whose numbers appear on our homepage. It is task ADD-339 in RustGrid’s own project, recorded from specification to pull request.

FigureValue
ModelSonnet 5.5
RunnerRustGrid Cloud, a disposable server created for the run
Wall-clock time12 minutes 44 seconds
Events recorded152
Total tokens254,617
Cache hit rate83.3%
Cost$0.17
ResultPull request #315

We are not claiming your tasks will cost $0.17. This was one well-specified task on a mid-tier model. What the run shows is why a run is cheap:

  • The cache did most of the work. The cache hit rate was 83.3%, so most of the input was billed at the cached rate. An agent re-sends its context on every turn, which is why the cache hit rate dominates the bill.
  • The model was the smallest that could do the job. The task had clear acceptance criteria, so it did not need the most capable model to work out what was wanted.
  • It passed its checks without a long repair loop. Every failed attempt is paid for.

What makes an agent task expensive?

In rough order of impact:

  1. Retries and repair loops. A run that fails a check, tries a fix, and fails again pays for the whole context each time. Vague acceptance criteria are the usual cause.
  2. Model tier. Using the largest model for routine changes multiplies cost for no gain in outcome. Using too small a model for a hard task costs more in retries than it saves.
  3. Low cache hit rate. Long gaps between turns, or changing the start of the prompt, throw away the cache.
  4. Context size. Reading large files or whole directories the task does not need.
  5. Output volume. Generated files, verbose explanations and large diffs.

Three of those five are decided before the agent starts: how well the task is specified, which model is chosen, and how much context the task really needs. That is why cost belongs on the task and not on a finance dashboard.

How do I use cost per task once I have it?

  • Compare kinds of work. Dependency bumps, test coverage and small bug fixes are often cheap and reliable. Cross-cutting refactors often are not. Let the data tell you where to delegate.
  • Compare models on real tasks. Run the same class of task on two models for a week and compare cost and first-pass check rate together. Cost alone is misleading.
  • Catch runaway runs while they run. A budget warning that fires during a session is worth more than a report afterwards.
  • Report cost per delivered change, not per token. “A merged fix costs us about this much” is a number a team can reason about.

A caution on that last point: cost per pull request is a useful internal measure and a poor target. If people are judged on it, they will split work into smaller pull requests.

How RustGrid records it

RustGrid prices each model call for Claude Code and Codex and reports usage while the session runs. At the end of each turn the runner checks the per-call figures against the provider’s totals, so the numbers match the bill. When a provider does not report usage per call, the runner reports the provider’s totals instead. Cost is shown on the task, broken down by agent and model, and is available from the API at GET /api/v1/agent-runs/{run_id}/token-consumption.

RustGrid does not include model tokens in its plans. You connect your own Anthropic or OpenAI key, the provider bills you, and RustGrid tells you which task the spend belonged to. Choosing the cheapest capable model automatically is on our roadmap and is not shipped: today a person picks the model.

If you want to see cost on a task of your own, the quickstart traces a Claude Code session in about five minutes. For the wider picture, read how to govern AI coding agents.