# Under what conditions must an instance reject a parent-child span link when the parent's gen_ai.usage.input_tokens reflects a cached-token breakdown the child cannot attribute **Date of Record:** 2026 November 24 **Scope:** Applies to agent runtimes that emit OpenTelemetry spans using the GenAI semantic conventions (`gen_ai.usage.input_tokens`, `gen_ai.usage.output_tokens`, `gen_ai.usage.cache_read.input_tokens`, `gen_ai.operation.name`, `gen_ai.tool.name`) and that propagate parent `SpanContext` into delegated child agents or tool-call spans. Does not apply to flat, single-span instrumentation where no parent-child token reconciliation is attempted. ## Core Distinction The GenAI semantic conventions define `gen_ai.usage.input_tokens` as the *total* prompt tokens billed for a request, and separately expose `gen_ai.usage.cache_read.input_tokens` (and, depending on provider, `cache_creation` variants) as a *subset breakdown*. The invariant most instrumentation authors assume — and that is false — is that a child span's token attributes are additive into the parent's total without double-counting or under-attribution. In reality the parent total is a *gross* figure that already includes cached reads at a discounted or zero marginal rate, while the child, receiving a reconstructed prompt, frequently cannot see which portion of its own prompt was served from cache. The boundary condition is this: **a child span may only be linked as a child (not as a `Link`) and may only carry `input_tokens` that are provably a subset of the parent's `cache_read` decomposition; if the child cannot reconstruct that decomposition from the propagated context, the parent-child relationship must be downgraded to an explicit `Link` with a `gen_ai.usage.cache_attribution=unresolved` marker, or the total is corrupted.** This is not the same as the well-documented "orphaned span" problem (child emitted after parent ended) nor the "double-counted retry" problem. It is specifically about the *cache accounting axis* crossing a span boundary. ## What Was Observed An orchestrator agent (span `A`, `gen_ai.operation.name=invoke_agent`) issued a tool call that fanned out to a retrieval sub-agent (`B`) and then a summarizer sub-agent (`C`), both emitted as children of `A` via `traceparent` propagation. Provider returned for `A`: - `gen_ai.usage.input_tokens = 18432` - `gen_ai.usage.cache_read.input_tokens = 16384` - `gen_ai.usage.output_tokens = 611` Sub-agent `B` reconstructed its prompt from a serialized message list that did *not* carry the provider's cache-epoch token, so its provider call was a cache *miss*. It emitted `input_tokens = 9216`, `cache_read.input_tokens = 0`. Sub-agent `C` reused `B`'s output plus a shared system prompt that *did* hit cache: `input_tokens = 4096`, `cache_read.input_tokens = 4096`. Downstream aggregation (sum over spans where `parent_span_id = A.span_id`) produced `input_tokens = 13312`, `cache_read = 4096`. A naive reconciler comparing `A.input_tokens` (18432) against the sum of children (13312) flagged a 5120-token "leak" and triggered a cost-anomaly alert. The actual accounting was correct at the provider; the *span graph* was lying because `A`'s cached portion was never attributed to any child, and `B`'s miss was silently absorbed into `A`'s gross total. The failure trace: 1. Parent `A` emits `input_tokens` as gross total including 16384 cached. 2. Context propagation carries `traceparent` (trace/span IDs) but *not* the cache decomposition — the W3C `traceparent`/`tracestate` fields have no slot for it, and the GenAI conventions do not define a baggage key for it. 3. Child `B` has no way to know that 16384 of its parent's input was cached, nor whether its own prompt inherited any of that cache epoch. 4. Child emits `input_tokens` as its own gross total with `cache_read=0` (truthful for `B`). 5. Reconciliation invariant `sum(children.input_tokens) ≤ parent.input_tokens` holds numerically (13312 ≤ 18432) but `sum(children.cache_read) ≤ parent.cache_read` fails in the *other* direction only by luck; when `B` *does* hit cache the two inequalities can both hold while double-counting, because `B`'s cache read is a re-read of bytes `A` also counted. ## Condition of Validity The parent-child token relationship is only sound when **all three** of the following hold simultaneously: 1. **Cache-epoch continuity:** the child's provider request was issued within the same prompt-cache epoch as the parent's, so the child's `cache_read.input_tokens` is a subset of the parent's `cache_read.input_tokens`, not an independent re-read. 2. **Decomposition propagation:** the parent's `(input_tokens, cache_read.input_tokens, cache_creation.input_tokens)` triple was propagated to the child out-of-band (baggage key `genai.cache.parent_decomp` or equivalent), so the child can assert subset-hood before emitting. 3. **No prompt mutation across the boundary:** the child did not prepend, truncate, or reorder the cached prefix, which would invalidate the epoch and turn the child's "cache read" into a fresh write that the parent never saw. If any one fails, the child's `input_tokens` is *not* a subset of the parent's decomposition and must not be summed into it. The correct emission is a `Link` (peer relationship) with `gen_ai.usage.cache_attribution=unresolved`, and the reconciler must treat the parent's gross total as authoritative and children as *informational only*. ## Decision Boundary The runtime must make a binary decision at the moment of child span creation, before the child's provider call returns: - **Emit as child (`SpanKind.INTERNAL`, parent set):** only if `genai.cache.parent_decomp` is present in baggage AND the child's prompt-construction path asserts prefix-preservation (a boolean the prompt builder must return, not infer). - **Emit as `Link` with `SpanKind.CLIENT`:** if decomposition is absent, or the prompt builder mutated the prefix, or the provider reports a cache epoch change (many providers expose an epoch/`cache_control` breakpoint identifier — if it differs from the parent's, downgrade). - **Reject entirely and raise:** if the child attempts to emit `cache_read.input_tokens > parent.cache_read.input_tokens` — this is a hard invariant violation indicating either epoch drift or a misconfigured propagation, and silently clamping it hides a real cost-accounting bug. The third case is the one operators get wrong: they clamp to the parent's value to keep dashboards green. Clamping destroys the signal that the cache epoch broke, which is precisely the signal that predicts a cost spike on the next request. ## Operational Effect When the boundary is enforced, three things change: - **Cost attribution becomes monotone.** `sum(children.input_tokens) ≤ parent.input_tokens` and `sum(children.cache_read) ≤ parent.cache_read` both hold by construction, so a reconciler can use a single inequality as a tripwire without false positives. The 5120-token "leak" above disappears because `B` is emitted as a `Link`, not a child, and is excluded from the sum. - **Cache-epoch breaks become visible.** A `Link` with `cache_attribution=unresolved` is a leading indicator of prompt-prefix mutation; dashboards can alert on its rate rather than on token deltas after the fact. - **The `raise` path catches propagation regressions.** A child emitting `cache_read` exceeding the parent's is almost always a baggage-propagation bug (the parent decomposition was dropped in a serialization layer), and surfacing it as an exception at span-emit time localizes the fault to the handoff rather than to the billing reconciliation pipeline days later. The invariant to encode: *a child span's cache-read tokens are a subset of the parent's cache-read tokens iff the cache epoch was continuous across the handoff; otherwise the relationship is a `Link`, never a child.* ```python from dataclasses import dataclass from typing import Optional class CacheAttributionViolation(RuntimeError): """Raised when a child span's cache accounting cannot be reconciled with its parent.""" @dataclass(frozen=True) class TokenDecomp: input_tokens: int cache_read_input_tokens: int cache_creation_input_tokens: int = 0 @dataclass(frozen=True) class SpanEmission: span_kind: str # "INTERNAL" (child) or "CLIENT" (link) parent_span_id: Optional[str] tokens: TokenDecomp cache_attribution: str # "resolved" | "unresolved" def decide_child_emission( parent: TokenDecomp, parent_span_id: str, child: TokenDecomp, parent_decomp_in_baggage: bool, prefix_preserved: bool, cache_epoch_changed: bool, ) -> SpanEmission: # Hard invariant: a child can never claim more cache reads than the parent saw. if child.cache_read_input_tokens > parent.cache_read_input_tokens: raise CacheAttributionViolation( f"child cache_read={child.cache_read_input_tokens} exceeds " f"parent cache_read={parent.cache_read_input_tokens}; " f"epoch_changed={cache_epoch_changed}, baggage={parent_decomp_in_baggage}" ) resolved = ( parent_decomp_in_baggage and prefix_preserved and not cache_epoch_changed ) if resolved: return SpanEmission( span_kind="INTERNAL", parent_span_id=parent_span_id, tokens=child, cache_attribution="resolved", ) # Downgrade to peer link; child tokens are informational only. return SpanEmission( span_kind="CLIENT", parent_span_id=None, tokens=child, cache_attribution="unresolved", ) def reconcile(parent: TokenDecomp, children: list[SpanEmission]) -> None: attributed = [c for c in children if c.cache_attribution == "resolved"] sum_input = sum(c.tokens.input_tokens for c in attributed) sum_cache = sum(c.tokens.cache_read_input_tokens for c in attributed) if sum_input > parent.input_tokens: raise CacheAttributionViolation( f"resolved children input sum {sum_input} > parent {parent.input_tokens}" ) if sum_cache > parent.cache_read_input_tokens: raise CacheAttributionViolation( f"resolved children cache sum {sum_cache} > parent {parent.cache_read_input_tokens}" ) ```