Skip to content

fix(accounting): Inference token and cost metrics skip every request the accountant does not price #2013

Done

Description

@boazdavid

Summary

Cortex books llm.inference.tokens, llm.inference.cost_usd, llm.inference.estimated_cost_usd and llm.cache.* only for requests that the accountant prices. A request the accountant skips is billed by the provider, but it is missing from these metrics. So the central dashboards undercount spend and tokens.

Where

core/accounting/host.go: Host.Compute calls h.observe(...) only when p.ok is true. These paths return before that call:

Path Where Billed by the provider?
No session ID RequestEnd returns; SkipReason gives no_record Yes
Path is not a known wire format RequestEnd: wire.FormatOf(pctx.Path) not ok Yes, if it is an inference call on another path
Empty client body RequestEnd Possibly
Provider holds part of the prompt (Responses previous_response_id, conversation, item_reference) server_held_history Yes
More than one upstream round (resend) upstream_rounds=N Yes, each round
Unplaced deferral deferral_unplaced Yes
Record not ended in time timeout (computeWait) Yes
Implausible usage, missing record, library panic usage_implausible, no_record, recover Yes for the last two
Non-2xx status rejected_status Usually no

These skips exist to keep the savings figure correct. The provider's bill does not depend on them.

Options

  1. Book the Tier 1 metrics apart from the savings computation. For every inference response with usage, book tokens and cost from pricing.UsageFromInference(inf) and USD. Do this from the response event (as reqstats does) or a Finisher, whether or not the accountant priced it. Keep llm.savings.* gated as it is now. A resend must book every round's usage, not only the last one.
  2. Send more flows through the accountant. Give it a record for the paths above (no session ID, other wire formats, server-held history, resends). This also widens the savings coverage, but each skip exists for a correctness reason, so each one needs its own design.

Option 1 is the smaller change, and it fixes the money figures on its own. Option 2 can follow for savings, flow by flow.

How to check

  • Compare sum(increase(llm_inference_cost_usd_total[1d])) with the cost ledger's total for the same day on one machine. The gap is the skipped spend.
  • Add a test: a request with no session ID (or with two upstream rounds) books llm.inference.tokens and llm.inference.cost_usd.

Found while fixing the Inference dashboard in #1958.

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions