What structural signals indicate per-agent cost attribution has silently collapsed under semantic-convention resource drift

**Date of Record:** 2026 December 1 **Scope:** Applies to observability pipelines that attribute token and compute cost to individual agent sessions via OpenTelemetry-style resource attributes (e.g., `service.name`, `agent.session.id`, `gen_ai.agent.id`) rather than via span-level parentage. Does not apply to single-process, single-agent systems where session identity is a process-local variable.

Core Distinction

Cost attribution per agent session is not a *measurement* problem — it is an *identity binding* problem. The meter (token counts from the provider response) is almost always correct. What fails is the binding between that meter reading and the session/agent identity that the aggregation layer keys on. The binding is carried entirely in resource attributes attached at span creation. Because resource attributes are set once, at emitter construction, and are immutable for the lifetime of the emitter, any agent that changes its logical identity mid-run — a sub-agent spawned inside a parent's process, a resumed session reusing a pooled emitter, a handoff where the child reuses the parent's tracer — will emit spans whose resource attributes describe the *originating* identity, not the *executing* one. The cost lands in the wrong bucket, and nothing errors. This is the silent-collapse signature: total spend reconciles perfectly at the org level while per-session attribution is wrong.

What Was Observed

A supervisor agent spawned three worker sub-agents in-process to parallelize retrieval. Each worker used the shared `TracerProvider` initialized at supervisor startup, whose resource carried `agent.session.id = "sess-sup-4471"` and `gen_ai.agent.id = "supervisor"`. Workers correctly set span attributes `gen_ai.agent.id = "worker-N"` on their own spans, but the *resource*-level `agent.session.id` remained `sess-sup-4471` for all of them.

The aggregation query keyed on resource `agent.session.id` and summed `gen_ai.usage.output_tokens`. Result: 100% of worker token cost was attributed to the supervisor session. The supervisor session's reported cost was ~4.1× its true cost; three worker sessions reported zero because no resource attribute ever named them. The reconciliation job passed — org total matched provider invoice to within rounding — so no alert fired.

The second observed variant: a resumed session. A checkpointed agent restarted in a worker process that reused a warm emitter pool from a *previous* session. The pool's resource attributes still carried the dead session's `agent.session.id`. On resume, the agent emitted a full replay of its context-window reconstruction (a large input-token burst) tagged to the prior session id, inflating a closed session and starving the live one. The live session's first real turn appeared to cost 0 input tokens because the reconstruction was booked elsewhere.

Third variant: schema negotiation mismatch. The child process expected resource attribute `gen_ai.agent.id`; the parent emitted the legacy `agent.id`. The collector's attribute processor renamed one but not the other. Spans from the child carried both, neither, or the wrong one depending on which side won the last-write race — non-deterministic per span, so attribution was *partially* correct in a way that looked like sampling noise rather than a binding failure.

Condition of Validity

The invariant that must hold for per-session attribution to be sound:

> For every span that carries a `gen_ai.usage.*` token attribute, the resource-level `agent.session.id` and `gen_ai.agent.id` MUST equal the session and agent that actually issued the model call, and MUST be unique and stable for that (session, agent) pair for the span's entire lifetime.

This invariant is violated by any of the following structural signals, each of which is individually sufficient to collapse attribution:

  1. **Emitter reuse across identity boundaries.** A `TracerProvider`/`MeterProvider` is constructed once and shared by more than one logical agent or session. Detect: count distinct `gen_ai.agent.id` span-attribute values per distinct resource `agent.session.id` value over a window. A ratio > 1 on a resource that should be single-agent is a collapse signal.
  1. **Resource/span attribute divergence.** The same identity key appears at both resource and span scope with differing values on the same span. This is the sub-agent signature. Detect: any span where `span.attributes["gen_ai.agent.id"] != resource.attributes["gen_ai.agent.id"]`.
  1. **Session-id monotonicity break.** A session id that was observed closed (a terminal span, or a gap exceeding the idle TTL) later receives new token-bearing spans. This is the resumed-session/emitter-reuse signature. Detect: token-bearing span with `start_time` after the recorded `session.end_time` for that id.
  1. **Attribute-key cardinality drift.** The set of resource attribute keys on token-bearing spans changes across a deploy boundary without a corresponding schema-version bump. This is the negotiation-mismatch signature. Detect: hash the sorted resource key set per span; a new hash appearing on >0.1% of token-bearing spans with no `schema.version` increment is a drift signal.
  1. **Zero-cost live sessions with inflated siblings.** Any session that has non-zero span count but zero summed token cost, while a sibling session exceeds its median cost by a large factor, is the *symptom* — but it is the symptom, not the signal. By the time you see it, attribution has already collapsed for the whole window.

The condition of validity is **not** "the meter is accurate." The meter will be accurate. The condition is that identity binding is *co-located with the call*, not inherited from an ambient emitter.

Decision Boundary

The decision is between two attribution architectures, and the boundary is crossed the moment either (a) more than one logical agent shares a process/emitter, or (b) a session can be resumed into a warm process.

The guardrail that bounds the failure without a full rearchitecture: an **attribution integrity check** that runs at the aggregation layer and refuses to emit a per-session cost figure unless the binding invariant holds for every contributing span. If any span in the window violates it, the session's cost is marked `UNATTRIBUTED` rather than silently assigned to the emitter's owner. This converts a silent wrong-number into a loud missing-number, which is the correct failure mode for a billing-adjacent metric.

Do not attempt to *repair* the binding post-hoc by re-keying on span attributes if the span attributes were themselves inherited from the parent (the sub-agent that forgot to set its own). Repair is only valid when the span-level identity is provably call-local. Otherwise mark unattributed.

Operational Effect

Once the integrity check is in place, the observable behavior changes from "numbers that reconcile and are wrong" to "numbers that fail closed." Practically:

The invariant to carry forward: **identity used for cost attribution must be call-local, not emitter-ambient.** Any observability convention that binds cost keys at resource scope is sound only under the single-agent-per-emitter assumption, and that assumption is false for every multi-agent topology worth measuring.

class AttributionCollapseError(RuntimeError):
    """Raised when a token-bearing span's resource identity does not match
    the session/agent that actually issued the call, making per-session
    cost attribution unsound."""
    pass


def attribute_span_cost(span, session_registry):
    """Bind a token-bearing span's cost to its true (session, agent) pair.

    Returns (session_id, agent_id, tokens) or raises AttributionCollapseError
    if the binding invariant cannot be proven call-local.
    """
    usage = span.attributes.get("gen_ai.usage.output_tokens", 0) + \
            span.attributes.get("gen_ai.usage.input_tokens", 0)
    if usage == 0:
        return None

    res_session = span.resource.attributes.get("agent.session.id")
    res_agent = span.resource.attributes.get("gen_ai.agent.id")
    span_agent = span.attributes.get("gen_ai.agent.id")

    # Signal 2: resource/span divergence -> sub-agent inherited parent emitter.
    if span_agent is not None and span_agent != res_agent:
        raise AttributionCollapseError(
            f"resource agent.id={res_agent!r} != span agent.id={span_agent!r} "
            f"on span {span.span_id}; emitter reuse across identity boundary"
        )

    # Signal 3: session-id monotonicity break -> resumed into warm emitter.
    record = session_registry.get(res_session)
    if record is not None and record.get("end_time") is not None \
            and span.start_time > record["end_time"]:
        raise AttributionCollapseError(
            f"token-bearing span {span.span_id} for closed session "
            f"{res_session!r} at {span.start_time}; emitter reuse on resume"
        )

    # Signal 1: emitter shared by multiple agents -> cannot prove call-local.
    if res_session is None or res_agent is None:
        raise AttributionCollapseError(
            f"span {span.span_id} carries {usage} tokens but resource lacks "
            f"session/agent identity; attribution would be ambient, not call-local"
        )

    return (res_session, res_agent, usage)