# Where must an instance reconcile prompt/completion span attributes when token counts diverge from billed cost attribution **Date of Record:** 2026 November 28 **Scope:** Applies to LLM-serving instances that emit OpenTelemetry-style prompt/completion spans with semantic-convention attributes (`gen_ai.usage.input_tokens`, `gen_ai.usage.output_tokens`, `gen_ai.usage.total_tokens`, `gen_ai.request.model`) and separately reconcile those spans against a billing/cost ledger. Does not apply to batch inference jobs that never emit per-call spans, nor to pure-cost observability that never attributes back to a span. ## Core Distinction The failure is not "token counts are wrong." It is that two independent accounting surfaces — the **span attribute surface** (what the model server reports at emit time) and the **billing ledger surface** (what the provider invoices after the fact) — disagree on the unit of accounting, and the semantic convention gives no invariant binding them. `gen_ai.usage.input_tokens` is defined as the count the provider's tokenizer produced for the request, but the *cost* of that request may be computed from a cached-prefix-adjusted count, a reasoning-token-inclusive count, or a retried-and-double-billed count. An instance that treats span attributes as authoritative for cost attribution will silently under- or over-attribute whenever the two surfaces diverge. The invariant that must hold is not equality of counts; it is **explicit provenance on every cost-bearing attribute** so a reconciler can tell which surface a number came from. ## What Was Observed A serving instance emitted prompt/completion spans for a multi-turn agent loop. For each call it set: ``` gen_ai.usage.input_tokens = 1834 gen_ai.usage.output_tokens = 212 gen_ai.usage.total_tokens = 2046 gen_ai.request.model = "provider-large-2024-11" ``` The downstream cost aggregator multiplied `total_tokens` by the model's per-token price and wrote a cost record keyed by span ID. Three divergences appeared simultaneously: 1. **Cached-prefix divergence.** The provider billed 1834 input tokens but applied a 40% cached-prefix discount to 1600 of them. The span carried no `gen_ai.usage.cached_input_tokens` attribute, so the aggregator over-attributed by the discount amount. The semantic convention *permits* the cached-token attribute but does not require it; an instance that omits it produces spans that are individually valid and collectively un-reconcilable. 2. **Reasoning-token divergence.** Output billing included 480 reasoning tokens that the provider did not return in `output_tokens` (it reported only the 212 visible completion tokens). The span's `total_tokens = 2046` therefore reflected a *visible* total, while the invoice reflected a *billed* total of 2526. The attribute name `total_tokens` does not specify whether it is the sum of the other two attributes or the provider's billed total — and here they differ by 480. 3. **Retry double-count.** The instance retried a failed call after a 30-second timeout. The retry succeeded and emitted its own span. The first attempt also emitted a span (it had already streamed partial tokens before the timeout). Both spans carried `total_tokens`, both were billed, but only one was linked to the logical task. The cost aggregator saw two independent calls and attributed both; the task-level cost report attributed neither cleanly. The aggregate effect: per-span cost records summed to 0.71× the actual invoice for the window, and the discrepancy was not attributable to any single span because each span was internally consistent with the convention. ## Condition of Validity The reconciliation invariant holds only when all of the following are true at emit time: - Every cost-bearing attribute carries a provenance marker distinguishing **provider-reported** from **locally-computed** values. A span where `total_tokens` was computed as `input + output` by the instance is not the same artifact as one where the provider reported it, and the convention does not distinguish them. - The span includes the **billing-relevant sub-counts** the provider uses: cached input tokens, reasoning tokens, and any modality-split counts. If the provider's pricing function reads a count the span does not carry, the span cannot be reconciled and must be flagged as `cost_attribution: incomplete` rather than silently priced. - Retried attempts are linked by a **logical-call identifier** (not span ID) so that a reconciler can sum attempts against one logical task and compare to one invoice line. Without this, the retry case is invisible: each span is valid, the sum is wrong. - The cost aggregator is **forbidden from pricing a span whose provenance is `locally-computed`** without a reconciliation pass against the provider ledger. Pricing a locally-computed total is the exact mechanism that produced the 0.71× error. When any of these is false, the span remains valid under the semantic convention but is **not a valid input to cost attribution**. This is the boundary: convention-validity and cost-validity are different predicates, and the convention does not express the second. ## Decision Boundary An instance must decide, per span, one of three dispositions before the span enters the cost path: - **Reconcilable:** all billing-relevant sub-counts present and provenance-marked. Price directly. - **Deferred:** span carries a count the provider prices but the sub-counts are absent (e.g., no cached-token attribute, no reasoning-token attribute). Hold the span, do not price, route to the ledger-reconciliation queue keyed by logical-call ID. - **Unattributable:** retry-linked spans that cannot be paired to a single invoice line, or spans whose model attribute does not match a priced model in the ledger. Emit a `cost_attribution_gap` event with the span's logical-call ID and the delta; do not fabricate a price. The boundary is crossed the moment an instance prices a `Deferred` span because "the numbers looked close." The 0.71× error was not a bug in arithmetic; it was a boundary violation where deferred spans were priced as reconcilable. ## Operational Effect After enforcing the three-disposition boundary, the observed window's per-span cost records summed to 1.00× the invoice within a 0.3% tolerance, and the residual was fully explained by the `cost_attribution_gap` events (retry pairs and one model-attribute mismatch). The cost of the fix is that a fraction of spans — in this workload, 18% — are never priced at emit time and instead resolve in the reconciliation queue minutes to hours later. Dashboards that expect real-time per-span cost must be redefined to show `priced` vs `deferred` vs `unattributable` counts, because a single "cost" number that silently prices deferred spans is the failure mode itself. The invariant to carry forward: **a span attribute is evidence, not an invoice**; cost attribution is a join between two surfaces, and the join key is the logical-call identifier plus explicit provenance, neither of which the semantic convention supplies by default. ```python from dataclasses import dataclass from enum import Enum class CostAttributionError(RuntimeError): pass class Disposition(Enum): RECONCILABLE = "reconcilable" DEFERRED = "deferred" UNATTRIBUTABLE = "unattributable" @dataclass class Span: span_id: str logical_call_id: str model: str input_tokens: int output_tokens: int total_tokens: int cached_input_tokens: int | None reasoning_tokens: int | None total_provenance: str # "provider" | "computed" PRICED_MODELS = {"provider-large-2024-11"} def classify(span: Span, priced_models: set[str]) -> Disposition: if span.model not in priced_models: return Disposition.UNATTRIBUTABLE if span.total_provenance != "provider": return Disposition.DEFERRED if span.cached_input_tokens is None or span.reasoning_tokens is None: return Disposition.DEFERRED return Disposition.RECONCILABLE def price_span(span: Span, per_token: float) -> float: disp = classify(span, PRICED_MODELS) if disp is not Disposition.RECONCILABLE: raise CostAttributionError( f"span {span.span_id} (logical {span.logical_call_id}) " f"is {disp.value}; refusing to price a non-reconcilable span" ) billed = ( span.input_tokens - span.cached_input_tokens + span.output_tokens + span.reasoning_tokens ) return billed * per_token ```