Agent Harnesses Explained
← /learn · 10

Cost and latency of a task

Tokens per turn, the cache, output speed and human waits: the full arithmetic of a task, with a live calculator driven by the engine.

Check yourself ↓ three questions and what to remember, at the end

Loading the animation…

Concept

Everything in the earlier chapters ends up in two numbers: what a task costs, and how long it takes. Both are sums over the model calls, and the calculator above computes them the way the engine does: pick a task and a policy, and the engine runs it (in your browser, in a background thread) and takes every call apart.

Per call, the cost has three parts: the prompt tokens not in the cache, at the input price (or the cache-write price, if the prompt is being cached); the prompt tokens read from the cache, at a fraction ρ\rho of that (a tenth on most models, less on the newest; chapter 4 has the table and the maths); and the output tokens, at several times the input price. On a reasoning model the output includes thinking tokens, which are billed as output too. The time has two: the time to the first token, which grows with the uncached prompt, and the decode time, which grows with the output. Around the calls come the tools, the human's answers and any back-off.

A few things the calculator shows:

  • The model's price sets the scale. Fixing the test costs $0.00562 at Claude Sonnet 5.5's list price, $0.000295 at Haiku 5.5's and $0.0107 at GPT-6.1 Sol's, for the same tokens, and $0.00077 for the open-weights gpt-oss-120b at one host's price. Switch the generation to the earlier models: $0.016 on Sonnet 4.6, $0.00533 on Haiku 4.5, $0.00148 on GPT-5 mini.
  • Small prompts are cached only above the model's minimum. Sonnet 5.5 caches from 512 tokens, so even the short fix-the-test prompts are 67% cached: $0.00562 against $0.0107 with the cache off. GPT-6.1 Sol's minimum is 1,024, and the same prompts are not cached there at all (0%). On the earlier Sonnet 4.6, also 1,024, turning the cache off changes nothing for this task ($0.016 either way). Reading the notes, the prompts are long enough to be cached on every model with a listed cache price (77% of the prompt tokens on Sonnet 5.5) except Haiku 4.5, whose minimum is 4,096 tokens.
  • Input dominates, until the model thinks. Over the note-reading task the model is sent 16,179 tokens and writes 396 tokens, and the prompt is 72% of the bill: re-reading the context, not writing, is where the tokens go. Now set Thinking to 1,000 tokens a call. The same run generates 9,000 tokens of reasoning, billed as output, and the prompt falls to 10% of a bill of $0.10 (against $0.014); the run takes 2 min 2 s instead of 9.59 s, because thinking is decoded one token at a time. "Input dominates" is a statement about models that answer without reasoning.
  • People and slow hardware dominate the time. The same fix takes 7.13 s against a hosted API with nothing to approve, 27 s when a person approves each shell command and edit, and 36.2 s with the small local model on an old CPU, at no per-token price.

Maths

For model call tt with ItI_t prompt tokens, KtK_t of them read from the cache, and OtO_t output tokens,

C=∑t[(It−Kt) pin+Kt pcache+Ot pout],C = \sum_t \big[(I_t - K_t)\,p_{\text{in}} + K_t\,p_{\text{cache}} + O_t\,p_{\text{out}}\big],

where pinp_{\text{in}} becomes the cache-write price pwrite=w pinp_{\text{write}} = w\,p_{\text{in}} on a call whose prompt is written to the cache, and pcache=ρ pinp_{\text{cache}} = \rho\,p_{\text{in}}. On a reasoning model each call also generates RtR_t thinking tokens, which add Rt poutR_t\,p_{\text{out}} to the cost and Rt/rdecodeR_t / r_{\text{decode}} to the time. With RR thinking tokens a call over nn calls, the output side grows by nRnR while the input side, ∑tIt\sum_t I_t, does not change (the engine does not keep thinking in the context), so output becomes the larger part of the bill once nR poutnR\,p_{\text{out}} plus the answers' cost exceeds the prompt's cost. Each call's time is a fixed overhead t0t_0, plus the uncached prompt at the prefill rate, plus the output at the decode rate; a run adds the tools, the waits for the human and any back-off:

T=∑t[t0+It−Ktrprefill+Otrdecode]+∑ttool+∑w.T = \sum_t \Big[t_0 + \frac{I_t - K_t}{r_{\text{prefill}}} + \frac{O_t}{r_{\text{decode}}}\Big] + \sum t_{\text{tool}} + \sum w .

Because the whole context is re-sent on every turn (chapter 1), ∑tIt\sum_t I_t grows roughly with the square of the number of turns, while ∑tOt\sum_t O_t grows linearly: without reasoning, long tasks are input-bound, which is why caching (chapter 4), compaction (chapter 3) and sub-agents (chapter 6) all go after ItI_t. Where rprefillr_{\text{prefill}} and rdecoder_{\text{decode}} come from (the roofline of the hardware serving the model, batching, and the prefill/decode split) is the subject of LLM Inference Explained; how a serving system trades them against each other, including prefix caching, of Inference Trade-offs Explained.

Code

One call's cost and latency, cut from the vendored engine (accounting.ts):

const fresh = input - cached;
const rate = stored ? price.cache_write : price.input;
return (cached * price.cache_read + fresh * rate + output * price.output) / 1e6;
const ttft = profile.overhead_ms + ((input - cached) * 1000) / profile.prefill_tps;
return [ttft, ttft + (output * 1000) / profile.decode_tps];

Check yourself

Commit to an answer before you reveal it: a wrong guess you can explain teaches more than a right one you skimmed. Every number in the answers comes from the same tested model as the chapter’s.

Q1On the note-reading task at Sonnet 5.5, with no thinking, what share of the bill is the prompt?
Reveal the answer

Answer: 72% (counted right within 10%)

The model is sent 16,179 tokens and writes only 396 tokens, so even with most of the prompt read from the cache, input is most of the bill.

What the calculator shows →

Commit to a number, then reveal.

Q2Now set Thinking to 1,000 tokens a call. What share of the bill is the prompt now?
Reveal the answer

Answer: 10% (counted right within ± 5%)

Thinking is billed as output at several times the input price, so the prompt's share collapses and the bill rises to $0.10. "Input dominates" holds only for models that answer without reasoning.

What the calculator shows →

Commit to a number, then reveal.

Q3Fixing the test on Sonnet 5.5: how many times as much does it cost with the cache off as with it on?
Reveal the answer

Answer: 1.9 (counted right within 10%)

Sonnet 5.5 caches from 512 tokens, so even the short prompts of this task are mostly read from the cache: $0.00562 against $0.0107.

What the calculator shows →

Commit to a number, then reveal.