Skip to content
The AI Agent Factory

37.8 Build the contribution report with Claude Code

Approved by
Panaversity ·
5 min read

In everyday life. A shop's till report is only useful if it matches what is in the bank. The owner checks the two at the end of every month.

What you will learn. What Claude Code builds for the monthly report, how you check it, and the mistakes that make cost reports wrong.

Why it matters. A report with a cost counted twice, or in the wrong line, leads to the wrong decision with full confidence.

The spec

You write spec-report.md:

## Monthly contribution statement, per worker
- Source: the month's usage rows, joined by worker, tenant and month.
  Each row has a kind: model, runtime, process, review, other or learning.
- Lines: revenue; doing the work (models, runtime, processes, review,
  other); Contribution I; learning; Contribution II; margin.
- Each usage row is counted once: two rows with the same call ID are one
  call. A row with no kind fails the report.
- A row from the dreaming or evaluation Workflow is learning, whatever its
  kind says. The report names any row filed under the wrong kind.
- Money is stored and added as integer nano-dollars (billionths of a
  dollar), never as floats.
- Billed amounts, not list prices: where a provider bill is available,
  the month's model rows, for every worker, are compared with it, and the
  difference is shown.
- Also: cost per accepted assignment, verified autonomy rate, review
  minutes (from Chapter 36's autonomy report).

## Failure tests
1. A model call recorded twice (a retry) is counted once.
2. A dreaming run appears under learning, never under doing the work.
3. Summing 1,000,000 calls of $0.0000005 gives exactly $0.50.
4. A month's model total differs from the provider bill by more than 2%:
   the report shows a warning, not a silent total.

The three tools

Step 1. Build src/statement.ts from spec-report.md. Write the four failure tests first. Then build src/sensitivity.ts, which takes a worker's measured statement and runs model.ts with one assumption changed at a time: review rate, model mix and context tier. Then src/price.ts, which computes the price floor at 50% and 70% margins. Write docs/contribution-report.md for both workers.

What to check.

  • Open the usage table and find one assignment by its correlation ID. Do its rows add up to the cost shown for it?
  • Compare the month's model total with the AI Gateway and provider bills. Is the difference shown?
  • Read the sensitivity runner's inputs. Are they your measured numbers, with model.ts used only for the formulas?

Where Claude Code usually goes wrong here

A cost counted twice. A retried model call writes two usage rows, or a Workflow step writes its cost and its parent assignment writes it again. Catch it: failure test 1, and a check that the sum of assignment costs equals the sum of usage rows.

Learning under doing the work. The dreaming Workflow uses the same helper as ordinary work to record usage, with kind: "model". Contribution I then looks worse in the months the worker learns. Catch it: failure test 2. Search for every place usage is written, and check its kind.

One worker against the whole bill. Claude Code compares one worker's model rows with the provider's invoice. The invoice covers every worker, so the report shows a large, false difference. Catch it: reconcile the month across all workers, separately from each worker's statement.

List prices instead of billed amounts. Claude Code multiplies tokens by the prices in model.ts. Real bills include caching discounts, off-peak rates, free allowances and rounding. Catch it: failure test 4. The list-price number is an estimate; the bill is the fact.

Floating-point money. 0.1 + 0.2 is not 0.3 in JavaScript. Over a million tiny model charges, the errors add up. Catch it: failure test 3. Store money as integer nano-dollars and convert only to display it. Micro-dollars are not small enough: $0.0000005 is half a micro-dollar.

The invariant. A cost report is right only when each cost is counted once, in its right line, in exact arithmetic, and it agrees with the bill.

Check yourself. For the month, the report's model rows for one provider, across every worker, add up to $4.10. That provider's invoice for the month says $3.40. Which do you trust, and what do you look for? (The invoice. Look for cached tokens charged at full price in the report, retries counted twice, or calls priced at peak rates that were billed off-peak.)

Owner
Panaversity