Topic 3 of 5 · 5 min read
Calculating cost per successful task
Goal: Calculate cost per successful task from token usage logs that include retries, cache reads and failed runs.
Cost per successful task is all the money spent on a feature in a period, divided by the number of tasks that actually worked. Cost per call looks smaller and is easier to compute, and it is the wrong number to budget against.
Check yourself · 1 of 3
A feature spent 9 dollars over 1 000 tasks, and 800 of them succeeded. What is the cost per successful task?
Why it matters
Users pay for outcomes. A task that fails after three retries still costs three calls and produces nothing. If you divide by calls, retries and failures make the price per call look fine while cost per delivered result rises.
The pieces
For each call, tokens are billed in categories, each with its own price per million tokens (see your provider's pricing page):
- Input tokens, at the base input price.
- Cached input tokens. A cache write often costs more than a normal input token, and a cache read costs much less.
- Output tokens, usually priced several times higher than input.
- Reasoning tokens, produced by thinking models and typically billed as output even when you do not see them.
Call cost = sum of (tokens in each category times its price). Task cost = the sum over every call in that task: retries, tool-loop steps, a judge or guardrail call, and an embedding or retrieval call if it is billed.
How to calculate it
- Log usage from the API response for every call, with a task id, a feature name and the model.
- Group by task id and sum cost.
- Mark each task succeeded or failed by your own check: schema valid, test passed, user accepted.
- Divide the total spend of all tasks, including failed ones, by the number of successful tasks.
Before and after
A feature ran 1 000 tasks. Calls cost 9.00 dollars in total, and 800 tasks succeeded.
Weak: 9.00 dollars over 1 400 calls is 0.64 cents per call. Looks cheap.
Better: 9.00 dollars over 800 successes is 1.13 cents per successful task. If the ceiling is 1 cent, the feature is over budget, and the 200 failed tasks (and their retries) are the place to look.
What goes wrong
- Ignoring cache and reasoning tokens, which can be a large part of the bill.
- Leaving out failed runs, which are billed all the same.
- Mixing models in one price. Compute each call at its own model's price.
- Using list prices from memory. Prices change; read them from a config you maintain.
Examples
A usage log rolled up
Per-million-token prices assumed for this model: input 3.00 dollars, cached read 0.30, output 15.00.
| Task | Input tok | Cached read tok | Output tok | Result |
|---|---|---|---|---|
| t1 | 2 000 | 6 000 | 500 | success |
| t2 | 2 000 | 6 000 | 450 | failed, retried as t3 |
| t3 | 2 000 | 6 000 | 480 | success |
Cost of t1: 2 000 x 3 + 6 000 x 0.30 + 500 x 15, all over a million = 0.006 + 0.0018 + 0.0075 = 1.53 cents. t2 is 1.47 cents and t3 1.50 cents. Total 4.50 cents for two successes: 2.25 cents per successful task, not 1.5 cents per call.
Computing it in a script
PRICE = {"in": 3.00, "cache_read": 0.30, "out": 15.00} # dollars per 1M tokens
def call_cost(u):
return (u["in"] * PRICE["in"]
+ u["cache_read"] * PRICE["cache_read"]
+ u["out"] * PRICE["out"]) / 1_000_000
total = sum(call_cost(u) for u in all_calls) # failed runs included
per_success = total / successful_tasks
The numerator contains every call; the denominator counts only successes.
Key points
- Cost per successful task equals total spend, including failed runs and retries, divided by the number of tasks that succeeded.
- Each call is priced by category: input, cached input reads and writes, output, and reasoning tokens, each with its own rate.
- Log token usage from every API response against a task id so retries and tool-loop steps roll up into one task cost.
- Define success with a check you control, such as valid schema, passing test or user acceptance, before dividing.
- Keep model prices in a maintained config, since they change and differ by model.
Common mistakes
- Dividing total spend by the number of API calls, which makes retries look cheap.
- Excluding failed tasks from the numerator because they produced no result.
- Treating cached and uncached input tokens as one price, or forgetting reasoning tokens bill as output.
- Hard-coding prices in the code once and never updating them after the provider changes them.
Check yourself · 2 of 3
Which tokens are usually billed as output tokens even though the user never sees them?
Check yourself · 3 of 3
One task made a first call that failed schema validation, then a second call that passed. Each call cost 2 cents. What should the log record as that task's cost?
Practice items, not counted towards your mastery.
Start here
- Reducing latency (Claude docs) (opens in a new tab)DocsFree
Names the levers: model choice, output length, prompt length and streaming, with guidance on each.
- Latency optimization (OpenAI API docs) (opens in a new tab)DocsFree
Seven principles for cutting latency, including fewer output tokens, fewer requests and parallel calls.
- LLM inference benchmarking metrics (NVIDIA NIM docs) (opens in a new tab)DocsFree
Defines time to first token, inter-token latency, end-to-end latency and throughput precisely.
More for this topic
- Claude API pricing (opens in a new tab)DocsFree
Per-million-token prices for input, output, cache writes and cache reads, which every cost formula starts from.
- Prompt caching (Claude docs) (opens in a new tab)DocsFree
How cache writes and reads are priced and what makes a prompt prefix cacheable.