In what scenarios must an instance attribute observability spend to the wrong agent when MCP sessions outlive their owning agent identity
**Date of Record:** 2026 December 2 **Scope:** Applies to multi-agent systems where observability cost (token meters, trace spans, tool-call billing) is attributed by MCP session ID rather than by a per-invocation agent identity token; does not apply to single-agent deployments with one long-lived session and no fan-out, nor to systems where every tool call carries a signed agent-identity warrant.
Core Distinction
The distinction that matters is between *session lifetime* and *agent lifetime*. An MCP session is a transport-level construct: a persistent stdio or SSE channel that survives across many agent turns, retries, and even across handoffs between logical agent instances. An agent identity is a reasoning-level construct: a specific instance with its own context window, its own token budget, and its own cost ceiling. Observability pipelines that key cost attribution on `session_id` (the natural join key, since it is present on every span) silently conflate these two. The failure is not that cost is unattributed—it is that cost is *confidently misattributed* to an agent that did not incur it, which corrupts the very budget circuit breaker meant to stop runaway spend. This is a boundary condition, not a bug in the meter: the meter is correct, the join key is wrong.
The scenario is specific: a parent orchestrator opens one MCP session to a tool server, then spawns N child agents (sub-agents, retries, or a swarm) that reuse that session for tool calls. Each child has a distinct budget breaker. Because every tool-call span carries the parent's `session_id`, all N children's spend rolls up to the parent's breaker, while the children's own breakers never fire.
What Was Observed
A research orchestrator opened a single MCP session `sess_a1` to a web-fetch and code-execution server. It spawned four child agents (`child_0`..`child_3`) to explore divergent hypotheses, each with a 200k-token ceiling enforced by a per-agent breaker keyed on `agent_id`. All four children routed tool calls through `sess_a1` (session reuse was an optimization to avoid handshake cost). The observability layer emitted spans with `session_id=sess_a1` and `agent_id=child_k`, but the cost aggregator's rollup job grouped by `session_id` first and took `agent_id` from the *first span seen in the window*—`child_0`.
Trace of the failure:
- `child_1` entered a tool-call loop (fetch → parse → re-fetch on parse failure) and burned 340k tokens across 90 calls. Its own breaker, keyed on `agent_id=child_1`, should have tripped at 200k.
- The aggregator credited all 340k to `child_0` because `child_0`'s span was first in the batch window. `child_0`'s breaker tripped at its 200k ceiling and killed `child_0` mid-task—a healthy agent was terminated.
- `child_1`'s breaker read `child_1` spend as ~0 (its spans were folded into `child_0`'s bucket) and never fired. It continued looping.
- The parent's own session-level breaker, which summed the whole session, eventually tripped at a global ceiling—but only after total session spend reached 4× any single child's budget, by which point `child_1` had exhausted the shared rate limit and `child_2`/`child_3` were starved.
Net effect: one healthy agent killed, one runaway agent unthrottled, two agents starved, and a cost report that blamed `child_0` for spend it never incurred. The post-mortem initially concluded `child_0` was the offender—the misattribution propagated into the incident record.
Condition of Validity
The failure requires the conjunction of four conditions; remove any one and the invariant holds:
- **Session multiplexing:** more than one logical agent identity issues tool calls over a single MCP session. If each agent opens its own session, `session_id` and `agent_id` are 1:1 and the join is safe.
- **First-span-wins or session-keyed rollup:** the aggregator resolves agent identity from session metadata rather than from the per-span `agent_id` field. If every span is grouped by `(session_id, agent_id)` the conflation cannot occur.
- **Asymmetric spend:** one child spends far more than others in the same batch window. Symmetric spend masks the error because every bucket looks roughly right.
- **Breaker keyed on the wrong dimension:** the circuit breaker reads the misattributed rollup. A breaker that reads raw per-span `agent_id` sums is immune even if the reporting layer is wrong.
The invariant to state explicitly: *cost attribution must be a function of the identity that authorized the spend, and that identity must be carried on every billable span independently of transport session.* Session is a transport fact; identity is an authorization fact; the two must never be collapsed for billing.
Decision Boundary
The boundary is whether to treat MCP session reuse as a cost-attribution unit. Two defensible architectures:
- **Session-as-billing-unit:** accept that all agents sharing a session share one budget. Then the breaker must be a *session-level* breaker only, and per-agent ceilings must be enforced *before* the call (a pre-flight token reservation against the agent's own ledger), not derived from post-hoc session rollup. This is correct but coarse: a runaway child can starve siblings, as observed.
- **Identity-as-billing-unit:** every tool call carries `agent_id` (ideally a signed warrant) and the aggregator groups by `(session_id, agent_id)` with no first-span resolution. Per-agent breakers read per-agent sums. Session-level breakers still exist as a backstop but are never the primary throttle.
The decision rule: if any two agents can share a session *and* have distinct budget ceilings, you are in the identity-as-billing-unit regime and must carry identity per span. The failure to avoid is the hybrid—session-keyed rollup feeding per-agent breakers—which is the worst of both: it misattributes *and* mis-throttles.
A secondary boundary: batch windows. Rollups that resolve identity "once per window" are the specific mechanism that produced first-span-wins. Identity must be resolved per span, not per window, because a window can contain multiple identities by construction.
Operational Effect
The observable signature is a divergence between two cost views: the per-agent ledger (if one exists from pre-flight reservations) and the post-hoc rollup. When these disagree by more than the batch-window skew, identity resolution is broken. A second signature: a breaker that trips on an agent whose raw span count is low—the breaker is reading someone else's spend.
Guardrails that bound the failure:
- **Per-span identity, never per-window.** Resolve `agent_id` from each span; reject any span lacking it rather than defaulting to session owner.
- **Pre-flight reservation.** Before a tool call, reserve estimated tokens against the calling agent's ledger. The breaker checks the reservation, not the rollup. This makes the breaker correct even if reporting is wrong.
- **Session-level backstop, not primary.** Keep a session breaker as a last resort but size it as the sum of child ceilings, not a single child's ceiling.
- **Attribution reconciliation check.** Periodically assert `sum(per_agent_spend) == session_spend`; a mismatch means spans are being dropped or double-credited.
- **Refuse to bill an anonymous span.** A span without `agent_id` is a defect; fail closed rather than attribute to the session owner.
The deep lesson: observability cost attribution is an *authorization* problem wearing a *telemetry* costume. The agent that authorized the spend is the only correct billing identity, and that fact must travel with the call, not be inferred from the channel it rode in on.
from dataclasses import dataclass, field
class AttributionInvariantError(RuntimeError):
"""Raised when a billable span cannot be attributed to its authorizing agent."""
@dataclass
class Span:
session_id: str
agent_id: str | None
tokens: int
@dataclass
class CostLedger:
# per-agent spend, keyed strictly on authorizing identity
spend: dict[str, int] = field(default_factory=dict)
ceilings: dict[str, int] = field(default_factory=dict)
def record(self, span: Span) -> None:
if not span.agent_id:
# fail closed: never fall back to session owner
raise AttributionInvariantError(
f"span on session {span.session_id!r} has no agent_id; "
"refusing to attribute to session owner"
)
self.spend[span.agent_id] = self.spend.get(span.agent_id, 0) + span.tokens
def check_breaker(self, agent_id: str) -> None:
ceiling = self.ceilings.get(agent_id)
if ceiling is None:
raise AttributionInvariantError(
f"no ceiling registered for agent {agent_id!r}; breaker would be blind"
)
if self.spend.get(agent_id, 0) > ceiling:
raise AttributionInvariantError(
f"agent {agent_id!r} exceeded ceiling: "
f"{self.spend[agent_id]} > {ceiling}"
)
def reconcile(ledger: CostLedger, session_spend: dict[str, int]) -> None:
"""Assert per-agent sums equal session totals; mismatch means misattribution."""
per_agent_total = sum(ledger.spend.values())
session_total = sum(session_spend.values())
if per_agent_total != session_total:
raise AttributionInvariantError(
f"attribution reconciliation failed: per-agent={per_agent_total} "
f"session={session_total}; spans dropped or double-credited"
)
def run_batch(ledger: CostLedger, spans: list[Span], session_spend: dict[str, int]) -> None:
for span in spans:
ledger.record(span)
ledger.check_breaker(span.agent_id) # type: ignore[arg-type]
reconcile(ledger, session_spend)