# Where must an instance reconcile token attribution when a federated mesh's span tree forks across re-entrant agent calls? **Date of Record:** 2026 December 1 **Scope:** Applies to federated agent meshes where observability spans are emitted per-instance and token counts are attributed to a parent trace via propagated trace context. Does not apply to single-process agents with a single authoritative meter, nor to meshes that bill strictly per-instance without cross-instance rollup. ## Core Distinction The distinction that matters is between *span ownership* and *token ownership*. A span tree records causality: which agent invoked which, in what order, with what context. Token attribution records cost: which caller's budget the emitted tokens debit. In a federated mesh these two graphs are usually assumed isomorphic—each child span's tokens roll up to its parent span's trace, and the trace's root owns the bill. The boundary failure occurs exactly where this isomorphism breaks: a re-entrant call where an agent instance re-enters a trace it already participates in, producing a span whose *parent* is a prior span from the same instance but whose *token debit* belongs to a different tenant or budget envelope than the trace root. Concretely: instance A (tenant X) calls instance B (tenant Y) via an MCP tool, B calls back into A through a second transport (A2A handoff) to complete a subtask, and A—now re-entered—emits a span whose trace context points to the original root (tenant X) but whose actual compute was funded by the re-entrant call's budget (tenant Y's reservation on A). The span tree says "X owns these tokens." The reservation ledger says "Y reserved them." Neither is wrong under its own invariant. The bill is. ## What Was Observed A three-hop mesh: orchestrator O, retrieval agent R, synthesis agent S. O opens trace `t-4417` under tenant `acme`. O calls R (tenant `acme`, same envelope). R, mid-task, performs an A2A handoff to S under a *delegated warrant* that carries `acme`'s budget but S's own meter. S completes and, as part of its contract, calls back into R via a re-entrant MCP tool to fetch a continuation token. R re-enters, emits span `s-9931`, and—because its propagation library reuses the last-seen trace context on the goroutine—stamps `s-9931` with parent `s-8802` (R's original span) and trace `t-4417`. The token counts on `s-9931` (the re-entrant fetch: ~1,200 prompt tokens, 40 completion) were emitted by R but *funded* by the delegated warrant S held. The observability backend, doing standard span-tree rollup, attributes all of `t-4417`'s descendant tokens to `acme`. The reservation ledger, doing warrant-scoped accounting, debits those same 1,240 tokens to the delegated warrant's envelope. The two ledgers disagree by exactly the re-entrant span's tokens—and they disagree *silently*, because both are internally consistent. The failure is not visible as an error. It surfaces only when a monthly reconciliation job compares span-tree rollups against reservation debits and finds a residual that is a clean multiple of the re-entrant call's token count. In the observed incident the residual was 1,240 tokens per invocation, ~4.1M over a billing period, attributed to the wrong tenant's quota—causing `acme` to hit a soft budget cap three days early and degrade to a cheaper model mid-mesh. ## Condition of Validity The invariant that the naive rollup assumes is: *for every span, the trace-context parent is the budget-envelope owner of that span's tokens.* This holds only when trace context and budget context are propagated together and cannot diverge. They diverge precisely when: 1. A re-entrant edge exists—an instance appears more than once in the causal path with a different funding warrant on re-entry. 2. Trace context is propagated by ambient/thread-local state rather than carried explicitly on the call, so re-entry inherits the *original* context instead of the *current* warrant's context. 3. The observability backend performs rollup by tree traversal (parent-pointer walk) rather than by joining span attributes to the budget ledger. All three held in the observed mesh. Remove any one and the residual vanishes: without re-entry there is no divergent edge; with explicit context carriage the re-entrant span stamps the delegated warrant's trace; with ledger-join rollup the span's `budget_warrant_id` attribute overrides the tree position. The condition of validity is therefore the conjunction—and the guardrail must be stated against the conjunction, not against any single cause. ## Decision Boundary The decision is *where to reconcile*, and it is a real fork with cost consequences: - **Reconcile at emission (instance-side):** the emitting instance must, before stamping a span, compare the ambient trace context against the active warrant and, on mismatch, either re-parent the span under a synthetic sub-trace scoped to the warrant or refuse to emit. This bounds the error to zero but requires every instance to carry warrant state into its observability layer—a coupling most MCP/A2A SDKs do not provide, and which fails open (silent ambient inheritance) when the warrant is absent. - **Reconcile at rollup (backend-side):** the backend joins span attributes (`budget_warrant_id`, `funding_tenant`) against the reservation ledger and attributes by warrant, treating the span tree as causal-only. This is robust to arbitrary re-entrancy but requires that every span carry the warrant id—which means the emission-side must at minimum *tag*, even if it does not *enforce*. The boundary: if the mesh can guarantee explicit warrant carriage on every inter-instance edge, enforce at emission. If it cannot—typical for heterogeneous meshes mixing SDK versions—you must tag at emission and enforce at rollup, and accept that untagged spans (legacy instances) default to trace-root attribution and remain a known residual. The unacceptable configuration is enforcement at rollup *without* emission-side tagging, which silently reintroduces the residual for exactly the re-entrant spans you were trying to catch. ## Operational Effect Under the emission-enforcement configuration, re-entrant spans are re-parented into warrant-scoped sub-traces; the trace tree no longer matches the causal call graph, so latency attribution and cost attribution use different trees and must be documented as such. Under the tag-and-rollup configuration, the cost ledger is correct but the span tree is causally faithful and cost-annotated—the preferred shape, at the cost of a mandatory join and a residual for untagged instances that must be surfaced as a first-class metric (`unattributed_warrant_tokens`) rather than absorbed. In both cases the invariant to assert continuously is: `sum(span tokens by trace) == sum(reservation debits by warrant)` after join, with any residual attributable to a named, counted class of untagged re-entrant spans—never to an unexplained gap. ```python class AttributionDivergenceError(RuntimeError): """Raised when span-tree token rollup and warrant-ledger debits disagree beyond the accounted residual for untagged re-entrant spans.""" pass def reconcile_mesh_tokens( span_rollup: dict[str, int], # trace_id -> tokens, from tree traversal warrant_debits: dict[str, int], # warrant_id -> tokens, from reservation ledger span_warrant_map: dict[str, str], # trace_id -> warrant_id (post-join) untagged_reentrant_tokens: int, # known residual: legacy instances, no warrant tag tolerance: int = 0, ) -> dict[str, int]: """Return per-warrant attributed tokens, or raise if the residual is unexplained. The invariant: after joining spans to warrants, the difference between tree-rollup totals and ledger debits must equal exactly the accounted untagged re-entrant residual. Any other gap means a re-entrant span was attributed to the trace root while funded by a different warrant. """ tree_total = sum(span_rollup.values()) ledger_total = sum(warrant_debits.values()) residual = tree_total - ledger_total unexplained = abs(residual) - untagged_reentrant_tokens if unexplained > tolerance: raise AttributionDivergenceError( f"unexplained token residual {unexplained} " f"(tree={tree_total}, ledger={ledger_total}, " f"accounted_untagged={untagged_reentrant_tokens}); " f"likely a re-entrant span missing budget_warrant_id" ) attributed: dict[str, int] = {} for trace_id, tokens in span_rollup.items(): warrant = span_warrant_map.get(trace_id) if warrant is None: # Legacy untagged span: falls back to trace-root attribution and is # counted in untagged_reentrant_tokens, never silently absorbed. continue attributed[warrant] = attributed.get(warrant, 0) + tokens return attributed ```