LLM cost calculation
One request costs input tokens times the input price, plus output tokens times the output price, and a run costs the sum of its requests. An agent run is hundreds of requests, so the number you want is the total per run, read from the proxy instead of worked out by hand.
The formula
Providers price per million tokens, with one rate for input and a higher rate for output. Take the rates from the provider's own pricing page, not from a blog post, because they change.
request_cost = (tokens_in * price_in) + (tokens_out * price_out) run_cost = sum(request_cost) over every request in the run
Cached input tokens are billed at a different rate from fresh ones, so keep cache reads and cache writes as separate terms. If you fold them into one input count, your estimate drifts from the invoice.
Why hand math misses on agents
A coding agent resends the whole conversation on every turn. Turn thirty carries everything from turns one to twenty nine, so input tokens grow with each step and the total is far above what one prompt suggests.
Retries add a second leak. A failed call that is sent again is billed again. RelayPlane flags a retry when the same thread resends an identical last user message within 120 seconds, and the run rollup shows the share, as a retries line next to the total. The runs and attribution docs explain how that is counted.
Estimating before the call
For a cap you need a worst case before the response exists. RelayPlane reserves the prompt size at the target model's price plus the request's max_tokens, up to 4096 output tokens, and swaps that for the real cost when the response lands. That is the same arithmetic as above with the output side assumed full. Set a small max_tokens and your worst case drops with it.
Reading the real total
Every request through the proxy is logged with its model, token counts, cache reads and a cost. Tag a command as a run and you get the sum without touching the agent code:
relayplane run --label nightly-backfill -- ./orchestrate.sh relayplane runs export --days 7 --format csv --out runs.csv
The CSV has one row per request, with tokens_in, tokens_out, cache_read, cache_creation and cost_usd. Check the formula against it. If your hand total and cost_usd disagree, the gap is usually cache terms or retries.
Know what the number means
cost_usd is notional list price. On a flat rate plan, such as Claude Code Max, the figure is API-equivalent cost and not money you were charged. Real money is at stake on API key and OpenRouter traffic. The budget cap docs spell out the same caveat.
From a number to a limit
Once you know what a workflow normally costs, make the proxy enforce it. Run the workflow five times, then ask for the observed range:
relayplane runs band nightly-backfill relayplane cap set --day 10
The band suggestion is the p25 to p75 of completed runs over 30 days, per cache state, and it needs at least five samples. The cap command sets a daily limit, and the request that would cross it gets a 429 before it is forwarded. For a limit on a single job, see how to cap AI spend per task. For the proxy itself, see the LLM proxy page. RelayPlane is free and MIT licensed.