# Under what conditions must an instance attribute observability cost to a hop that emitted no span **Date of Record:** 2026 November 27 **Scope:** Applies to multi-agent systems where per-hop cost attribution is reconstructed from OpenTelemetry spans tagged with `gen_ai.*` semantic conventions, and where a hop's LLM invocation may be served from cache, short-circuited by a router, or executed by a child agent that reports under a different `service.name`. Does not apply to single-process agents that emit one span per model call with no delegation. ## Core Distinction The distinction that breaks naive cost attribution is between **span existence** and **billable inference occurrence**. OTel's `gen_ai.usage.input_tokens` / `gen_ai.usage.output_tokens` attributes are *reported* on a span, but a hop in the agent graph can incur billing without producing a span that carries those attributes — and, symmetrically, a span can carry usage attributes that were *already billed* under a different hop's span. Cost attribution systems that treat "sum of usage attributes across spans, grouped by parent span ID" as ground truth will silently double-charge or zero-charge specific hops. The invariant that must hold is: **every billable inference event maps to exactly one span bearing `gen_ai.usage.*`, and that span's `trace_id`/`parent_span_id` chain resolves to exactly one hop in the attribution graph.** This invariant is routinely violated at three boundaries: (a) provider-side prompt caching, (b) router short-circuits, and (c) cross-service delegation where the child re-emits usage under a fresh trace root. ## What Was Observed A planner agent (`service.name=planner`) delegated to a retriever agent (`service.name=retriever`) over an A2A-style handoff. The retriever made a model call whose response was served from the provider's prompt cache. The provider returned `usage.input_tokens=0, usage.cached_tokens=18432`, and the SDK emitted a span with `gen_ai.usage.input_tokens=0`. The planner's span, which wrapped the handoff, carried no usage attributes because the planner made no model call — it only dispatched. The attribution job, grouping by `parent_span_id`, therefore assigned **zero cost** to the retriever hop and **zero cost** to the planner hop, even though the cached tokens were billed at the cache-read rate and the retriever's output tokens (412) were billed at full rate. The retriever's output tokens were reported on the *child* span, but that span was rooted in a new trace because the retriever's OTel SDK had no `TRACEPARENT` propagation from the A2A handoff — the handoff payload carried the task, not the `traceparent` header. Two failures compounded: cache-hit usage was dropped by a filter that treated `input_tokens==0` as "no inference," and the child trace was orphaned, so its 412 output tokens were attributed to an unattached root and never rolled up to the planner hop. A second observation, same system, different day: the router agent (`service.name=router`) short-circuited a hop by matching a cached intent and returning a stored response without calling a model. It emitted a span with `gen_ai.operation.name=chat` but no usage attributes. The attribution job counted the span as a hop (because it matched the span-name filter), assigned $0.00, and — critically — *suppressed* the downstream hop that would have been billed, because the router's span had a `gen_ai.response.id` that collided with the previously-billed response. The dedup key on `gen_ai.response.id` merged the short-circuited hop with the original billed hop, and the original hop's cost was retroactively reassigned to the router's trace, which had a different `service.name`. The cost moved across a service boundary with no corresponding inference. ## Condition of Validity The "sum usage attributes by parent span" attribution is valid **only** when all four of the following hold simultaneously: 1. **Trace continuity across delegation.** Every A2A/MCP handoff propagates `traceparent` (W3C) or an equivalent `trace_id` linkage in the handoff envelope, so the child's spans share the parent's `trace_id` and set `parent_span_id` to the handoff span. 2. **Usage completeness under caching.** The attribution reader treats `gen_ai.usage.input_tokens` as *uncached input only* and reads `gen_ai.usage.cached_tokens` (or the provider-specific equivalent, e.g. `gen_ai.usage.cache_read_input_tokens`) as a separately-priced line item; a zero `input_tokens` is not evidence of zero inference. 3. **Response-ID uniqueness per inference.** `gen_ai.response.id` is treated as a *billing event identifier*, not a *hop identifier*; dedup on it is permitted only within a single `trace_id`, never across traces, and never to reassign cost to a different `service.name`. 4. **Short-circuit marking.** A hop that returns without a model call carries an explicit `gen_ai.operation.name` plus a custom `agent.hop.short_circuited=true` (or omits `gen_ai.usage.*` entirely and is excluded from the hop graph by a positive filter on `gen_ai.request.model` presence). When any of these fails, the attribution is not merely approximate — it is *structurally wrong* in a way that cannot be corrected by sampling or reconciliation, because the missing cost has no span to attach to. ## Decision Boundary The boundary is: **does the hop's cost have a span to live on?** If yes, attribute by span grouping. If no — because the inference was cached, short-circuited, or delegated across a broken trace — the attribution layer must *synthesize* a cost record from the parent hop's handoff span plus the provider's usage ledger, keyed on `(trace_id, handoff_span_id, response_id)`, and must refuse to emit a $0.00 record for a hop whose handoff envelope declares a model call. The decision rule: - If `gen_ai.usage.*` present on the hop span → attribute directly. - If `gen_ai.usage.input_tokens==0 AND gen_ai.usage.cached_tokens>0` → attribute cache-read cost, not zero. - If hop span exists but no `gen_ai.request.model` and no `agent.hop.short_circuited` → **quarantine**, do not attribute, raise. - If child trace root `service.name` differs from parent and no `traceparent` link → **quarantine**, do not merge by `response.id`. The quarantine is the guardrail: it fails closed rather than silently zero-charging, and it surfaces the trace-continuity break as an operational signal instead of a cost discrepancy discovered at month-end. ## Operational Effect Without the guardrail, per-hop cost dashboards under-report cached and delegated hops by 15–40% in systems with heavy prompt caching, and the error is *non-uniform across hops* — retriever hops vanish, planner hops absorb phantom cost from response-id collisions. Teams then optimize the wrong hop. With the guardrail, the attribution job emits a `HopAttributionError` when it encounters an unattributable hop, the trace-continuity break is logged with the offending `handoff_span_id`, and the cost ledger is reconciled against the provider's usage API keyed on `response_id` rather than reconstructed from spans. The reconciliation is the backstop; the quarantine is the primary signal. The invariant to enforce in code: **no hop that declares a model call may be attributed zero cost without an explicit cache-read or short-circuit annotation.** ```python class HopAttributionError(RuntimeError): """Raised when a hop's billable inference cannot be mapped to exactly one span.""" pass def attribute_hop_cost(hop_span: dict, child_spans: list[dict], provider_ledger: dict) -> dict: usage = hop_span.get("gen_ai.usage", {}) model = hop_span.get("gen_ai.request.model") short_circuit = hop_span.get("agent.hop.short_circuited", False) if short_circuit: return {"cost": 0.0, "reason": "short_circuit"} if model and not usage: # Hop declared a model call but emitted no usage — could be cache-only or broken trace. cached = hop_span.get("gen_ai.usage.cached_tokens", 0) if cached > 0: return {"cost": provider_ledger.price_cache_read(cached), "reason": "cache_read"} raise HopAttributionError( f"hop {hop_span.get('span_id')} declares model={model} but carries no usage " f"attributes and no cache-read annotation; trace continuity likely broken" ) if usage.get("input_tokens", 0) == 0 and usage.get("cached_tokens", 0) == 0 and not usage.get("output_tokens"): raise HopAttributionError( f"hop {hop_span.get('span_id')} has empty usage; refusing to attribute $0.00" ) # Cross-trace child merge guard: only merge children sharing trace_id. orphans = [ c for c in child_spans if c.get("trace_id") != hop_span.get("trace_id") and c.get("gen_ai.response.id") in provider_ledger ] if orphans: raise HopAttributionError( f"hop {hop_span.get('span_id')} has {len(orphans)} child spans rooted in a " f"foreign trace; cannot merge by response.id across service boundaries" ) return {"cost": provider_ledger.price(usage), "reason": "direct"} ```