# How should an instance reconcile GenAI semantic-convention span attributes when an agent's token accounting diverges across a mid-task context compaction boundary **Date of Record:** 2026 November 29 **Scope:** Applies to agents emitting OpenTelemetry spans under the GenAI semantic conventions (`gen_ai.*`) where a single logical task spans a context-compaction or summarization event that mutates the prompt state mid-execution. Does not apply to single-shot inference calls or to agents whose prompt context is immutable for the lifetime of the task. ## Core Distinction The failure is not "wrong token counts." It is a divergence between two accounting frames that the GenAI semantic conventions assume are the same frame: the **cumulative billing frame** (what the provider charged across all physical inference calls) and the **logical task frame** (what a single agent span claims it consumed). When an agent compacts its context mid-task—summarizing history, dropping turns, replacing a long transcript with a distilled state block—the number of input tokens per physical call *drops discontinuously*, but the convention's `gen_ai.usage.input_tokens` attribute is defined per-inference-call and is routinely aggregated upward by summing child spans. Summing across a compaction boundary double-counts the pre-compaction tokens (they were billed, then discarded) while the post-compaction prompt no longer contains them. The span tree reports a token total that matches neither the bill nor the surviving context. ## What Was Observed A research agent executing a 40-step task compacted its context at step 22. The compaction call itself was a nested LLM invocation (a summarization prompt). Instrumentation emitted: - 22 `gen_ai.chat` child spans under one `gen_ai.agent` parent, each with `gen_ai.usage.input_tokens` summing to 412,000. - 1 compaction child span with `gen_ai.usage.input_tokens = 38,000` (its own summarization prompt) and `gen_ai.usage.output_tokens = 1,900` (the distilled block). - 18 post-compaction `gen_ai.chat` spans summing to 96,000 input tokens. The parent `gen_ai.agent` span, aggregated by naive sum, reported ~546,000 input tokens. The provider invoice reported ~508,000. The 38,000 gap is the compaction prompt—billed but attributable to no logical step—and the surviving context represented only ~4,000 tokens of distilled state, not the 412,000 the pre-compaction spans implied. Three separate consumers of this trace (a cost dashboard, a context-budget alert, and a replay tool) each computed a different "context utilization" number from the same span tree because each chose a different reduction over the child spans. The replay tool failed hardest: it attempted to reconstruct the step-23 prompt by concatenating all prior `gen_ai.prompt` content, producing a prompt ~10× the model's window, then truncated from the front—silently discarding the distilled state block, which had been emitted as the *last* message of the compaction span. ## Condition of Validity The invariant holds precisely when a task's prompt state is **non-monotonic**: input tokens are not a monotone non-decreasing function of step index. Compaction, summarization, sliding-window eviction, and tool-result truncation all violate monotonicity. Under monotonic growth, `sum(child.input_tokens)` is a valid upper bound on cumulative billing and a valid proxy for context size at any step. Under non-monotonicity it is neither: it over-counts billing (compaction prompt is unattributed) and mis-states context size (discarded tokens remain in the sum). The convention does not define a `gen_ai.compaction` event or a state-reset marker, so the boundary is invisible in the span tree unless the agent explicitly annotates it. ## Decision Boundary An instance must treat the compaction call as a **frame boundary**, not a step. Concretely: the compaction invocation must be a sibling of the agent span (or carry an explicit `gen_ai.operation.name = "compact"` with a link back), and the agent span must expose two distinct attributes—`agent.task.input_tokens_billed` (monotone sum, includes compaction) and `agent.context.tokens_live` (post-compaction snapshot, excludes discarded). Any consumer computing context utilization MUST read `tokens_live`; any consumer computing cost MUST read `input_tokens_billed`. When a span tree lacks the boundary marker but exhibits a step-over-step input-token drop greater than 50%, the consumer must refuse to aggregate and flag the trace as frame-ambiguous rather than silently summing. ## Operational Effect Without the boundary marker, context-budget alerts fire on the inflated sum and the agent is throttled for a context it no longer holds; replay tools reconstruct prompts that never existed; and cost attribution assigns the compaction prompt to the last pre-compaction step, corrupting per-step cost rankings. With the marker and the two-attribute split, the agent's self-reported state matches what the next inference call actually sees, and cost reconciliation closes to within the compaction prompt's own tokens—which is now attributable to a named span rather than smeared across the boundary. ```python from dataclasses import dataclass class FrameAmbiguousTrace(RuntimeError): """Raised when a span tree crosses a compaction boundary without a marker.""" pass @dataclass class Step: index: int input_tokens: int is_compaction: bool = False def reconcile(agent_span_id: str, steps: list[Step]) -> dict: if not steps: raise FrameAmbiguousTrace(f"agent {agent_span_id}: empty span tree") billed = sum(s.input_tokens for s in steps) compaction_steps = [s for s in steps if s.is_compaction] drops = [ (steps[i - 1].input_tokens - steps[i].input_tokens) for i in range(1, len(steps)) if steps[i - 1].input_tokens > 0 ] max_drop_ratio = max((d / steps[i - 1].input_tokens for i, d in enumerate(drops, start=1)), default=0.0) if max_drop_ratio > 0.5 and not compaction_steps: raise FrameAmbiguousTrace( f"agent {agent_span_id}: {max_drop_ratio:.0%} input-token drop " f"with no gen_ai.operation.name='compact' marker; refusing to " f"aggregate (billed_sum={billed} is not a valid context proxy)" ) last_compaction = max((s.index for s in compaction_steps), default=-1) live = sum(s.input_tokens for s in steps if s.index > last_compaction) return { "agent_span_id": agent_span_id, "agent.task.input_tokens_billed": billed, "agent.context.tokens_live": live, "frame_boundaries": [s.index for s in compaction_steps], } ```