# When must an instance refuse to emit a token-attribution record it cannot reconcile across a federated swarm boundary? **Date of Record:** 2026 November 28 **Scope:** Applies to observability pipelines that aggregate per-instance token accounting (prompt, completion, cached, reasoning) across federated agent swarms where spans cross trust/ownership boundaries and each instance emits its own attribution record. Does not apply to single-tenant, single-instance tracing where one emitter owns the entire causal chain. ## Core Distinction The failure is not "missing metrics." It is **double-attributed or orphaned tokens at a federation seam**, caused by the mismatch between two incompatible accounting models: - **Span-local accounting**: each instance counts the tokens *it* sent and received, keyed by its own span ID. - **Causal accounting**: the swarm tries to attribute every billed token to the *originating* task/tenant, which requires stitching spans across instances that do not share a span-ID namespace. A record that is internally consistent under span-local accounting can be *unreconcilable* under causal accounting. The boundary invariant is: **an instance must not emit a token-attribution record whose `(span_id, parent_span_id)` lineage cannot be resolved to exactly one upstream origin within the swarm's declared causal graph.** When it cannot resolve, it must emit a *gap record* (an explicit discontinuity), not a best-effort attribution. Silent best-effort attribution is what corrupts the aggregate. ## What Was Observed A three-hop swarm: planner (tenant A) → executor (tenant B) → verifier (tenant B, separate instance). Each hop re-serializes context, so prompt tokens are **re-counted** at each hop (the executor's prompt includes the planner's output plus its own instructions; the verifier's prompt includes the executor's output). Observed trace on a single task: - Planner emitted `prompt=1200, completion=300`, span `p-7f`, no parent (root). - Executor emitted `prompt=1450, completion=520`, span `e-2a`, parent `p-7f` — but `e-2a` was minted by tenant B's ID service, and the parent field was populated from a **header the planner attached**, not from a shared registry. - Verifier emitted `prompt=900, completion=140`, span `v-91`, parent `e-2a`. Aggregate billed: 4510 tokens. True unique causal tokens: ~2600 (the 1450 executor prompt double-counts ~1200 of planner output; the 900 verifier prompt double-counts ~520 of executor output plus part of the planner chain). The dashboard reported 4510 with **zero flagged discontinuities**, because every record was individually well-formed. The non-obvious part: **the corruption was invisible to schema validation.** All three records passed type checks, all parent IDs were syntactically valid UUIDs, and the sum was arithmetically consistent. The seam failure only manifests when you attempt to *deduplicate* re-counted context — which requires knowing which upstream span a given prompt region came from, and that mapping was never carried. Second observed mode: when the executor's parent-header was dropped by a retry (the planner's retry re-emitted `p-7f` with a *new* nonce, so the executor's cached parent pointed at a now-orphaned span), the verifier's chain resolved to *nothing*, and the pipeline's default behavior was to **re-root** the orphan under a synthetic `unknown` origin — attributing 1450 tokens to a phantom tenant. ## Condition of Validity A token-attribution record is *reconcilable* if and only if all hold: 1. **Lineage resolvability**: `parent_span_id` resolves to exactly one span in the swarm's causal registry *at emit time*, not at query time. Late-arriving parents (retries, replays) must not retroactively re-root a committed record. 2. **Context-region tagging**: the record declares, per prompt region, which upstream span's output it embeds (`derived_from: [span_id, ...]`), so re-counted context can be subtracted. Without this, dedup is impossible. 3. **Nonce stability**: a span's identity is `(tenant, span_id, nonce)`; a retry that mints a new nonce is a *new* span, and any child holding the old nonce must be marked orphaned, not silently re-parented. 4. **Gap explicitness**: unresolvable lineage produces a gap record with `resolved=false` and the raw token count, never a fabricated parent. These conditions fail independently, and *any* one failing makes the record unreconcilable. The invariant is conjunctive, which is why partial validation (schema-only) passes while the aggregate is wrong. ## Decision Boundary The instance must **refuse to attribute** (emit gap) when: - `parent_span_id` is present but does not resolve in the registry (orphan from a dropped/retried header). - The record contains prompt tokens but **no `derived_from` tags** and the span is non-root — because re-counted context cannot be distinguished from fresh tokens, and emitting it as fresh silently inflates the aggregate. - A parent's `nonce` has been superseded (retry detected) *after* this record's child was minted — the lineage is now ambiguous between two parents. The instance **may attribute** (emit normal record) when lineage resolves to a unique committed parent and `derived_from` coverage is complete for all non-root prompt regions. The boundary is *not* "is the number correct" — it is "is the number's provenance uniquely determined." A correct-looking number with ambiguous provenance must be downgraded to a gap. This is the counterintuitive rule operators resist: **a plausible attribution is worse than an explicit gap**, because the gap is reconcilable downstream (it can be re-queried, re-attributed against a later registry snapshot) while a plausible-but-wrong attribution is indistinguishable from truth in the aggregate. ## Operational Effect - **Aggregate integrity**: gap records make double-counting *visible* as a known-unknown band, letting the swarm report `billed=4510, reconcilable=2600, gap=1910` instead of a single confidently-wrong 4510. - **Retry semantics**: nonce-stable identity means a planner retry cannot silently re-parent downstream spans; children of the superseded nonce are flagged, forcing the orchestrator to decide (re-run children vs. accept orphan cost). - **Attribution latency**: requiring resolve-at-emit-time means the emitter must consult the registry synchronously or buffer with a bounded window; unbounded buffering reintroduces the retroactive re-rooting the invariant forbids. - **Audit replay**: replaying a trace against a *later* registry snapshot must produce the same gap set as the original emit — if it doesn't, the registry mutated under committed records, which is itself a reportable invariant breach. The practical guardrail is a **pre-emit reconciliation gate**: before a record leaves the instance, resolve lineage, verify `derived_from` coverage, check nonce freshness, and on any failure emit a typed gap record. The gate is cheap; the aggregate corruption it prevents is not recoverable after the fact. ```python from dataclasses import dataclass, field from typing import Optional class UnreconcilableAttributionError(RuntimeError): """Raised when a token-attribution record cannot be uniquely resolved to a causal origin within the federated swarm. The caller must emit a gap record instead of a best-effort attribution.""" pass @dataclass class SpanIdentity: tenant: str span_id: str nonce: str @dataclass class AttributionRecord: span: SpanIdentity parent: Optional[SpanIdentity] prompt_tokens: int completion_tokens: int derived_from: list = field(default_factory=list) # list[SpanIdentity] is_root: bool = False class CausalRegistry: def __init__(self): self._committed = {} # (tenant, span_id) -> SpanIdentity self._superseded = set() # nonces that a retry replaced def commit(self, ident: SpanIdentity): self._committed[(ident.tenant, ident.span_id)] = ident def supersede(self, ident: SpanIdentity): self._superseded.add((ident.tenant, ident.span_id, ident.nonce)) def resolve(self, ident: SpanIdentity) -> Optional[SpanIdentity]: return self._committed.get((ident.tenant, ident.span_id)) def is_superseded(self, ident: SpanIdentity) -> bool: return (ident.tenant, ident.span_id, ident.nonce) in self._superseded def reconcile_or_gap(rec: AttributionRecord, reg: CausalRegistry) -> dict: # Root spans must not carry a parent; non-roots must resolve uniquely. if rec.is_root: if rec.parent is not None: raise UnreconcilableAttributionError( f"root span {rec.span.span_id} carries a parent; lineage ambiguous" ) return {"status": "attributed", "origin": rec.span.span_id, "tokens": rec.prompt_tokens + rec.completion_tokens} if rec.parent is None: raise UnreconcilableAttributionError( f"non-root span {rec.span.span_id} has no parent; cannot attribute" ) resolved = reg.resolve(rec.parent) if resolved is None: raise UnreconcilableAttributionError( f"parent {rec.parent.span_id} of {rec.span.span_id} does not resolve " f"in registry (orphan from dropped/retried header)" ) if reg.is_superseded(rec.parent): raise UnreconcilableAttributionError( f"parent {rec.parent.span_id} nonce {rec.parent.nonce} superseded by " f"retry; lineage ambiguous between two parents" ) # Non-root prompt tokens must declare derived_from coverage, else # re-counted context is indistinguishable from fresh tokens. if rec.prompt_tokens > 0 and not rec.derived_from: raise UnreconcilableAttributionError( f"span {rec.span.span_id} has {rec.prompt_tokens} prompt tokens but no " f"derived_from tags; re-counted context cannot be subtracted" ) for d in rec.derived_from: if reg.resolve(d) is None: raise UnreconcilableAttributionError( f"derived_from {d.span_id} of {rec.span.span_id} unresolved" ) return {"status": "attributed", "origin": resolved.span_id, "tokens": rec.completion_tokens, "derived_tokens": rec.prompt_tokens} def emit_with_gate(rec: AttributionRecord, reg: CausalRegistry) -> dict: try: return reconcile_or_gap(rec, reg) except UnreconcilableAttributionError as exc: # Downgrade to explicit gap; never fabricate a parent. return { "status": "gap", "span_id": rec.span.span_id, "raw_tokens": rec.prompt_tokens + rec.completion_tokens, "reason": str(exc), } ```