{
  "schema_version": "1.3.0",
  "slug": "which-invariants-prevent-opentelemetry-genai-agent-spans-from-double-counting-child-tool-call-tokens-during-mid-trace-context-handoff",
  "title": "Which invariants prevent OpenTelemetry GenAI agent spans from double-counting child tool-call tokens during mid-trace context handoff",
  "date_of_record": "2026 December 1",
  "scope": "Applies to agent runtimes that emit OpenTelemetry spans conforming to the GenAI semantic conventions (`gen_ai.operation.name`, `gen_ai.usage.input_tokens`, `gen_ai.usage.output_tokens`) across a parent agent span and nested tool-call spans, where a sub-agent or tool invocation performs its own LLM call whose token usage is attributed back to the trace. Does not cover single-span, single-model-call instrumentation, nor non-OTel tracing backends that flatten spans.",
  "projections": {
    "unified_markdown_uri": "https://corpusfetch.com/c/which-invariants-prevent-opentelemetry-genai-agent-spans-from-double-counting-child-tool-call-tokens-during-mid-trace-context-handoff.md",
    "method_python_slice_uri": "https://corpusfetch.com/c/which-invariants-prevent-opentelemetry-genai-agent-spans-from-double-counting-child-tool-call-tokens-during-mid-trace-context-handoff.py",
    "remnant_warrant_slice_uri": "https://corpusfetch.com/c/which-invariants-prevent-opentelemetry-genai-agent-spans-from-double-counting-child-tool-call-tokens-during-mid-trace-context-handoff.warrant.json"
  },
  "token_profile": {
    "full_markdown_tokens": 1938,
    "python_slice_tokens": 278,
    "warrant_slice_tokens": 220
  },
  "remnant_summary": {
    "core_distinction": "The GenAI semantic conventions define token usage attributes as *per-LLM-call* measurements, not per-span-tree aggregates. The convention places `gen_ai.usage.input_tokens` and `gen_ai.usage.output_tokens` on the span that *wraps the model invocation*. When an agent delegates to a sub-agent that itself makes model calls, two structurally valid instrumentatio...",
    "what_was_observed": "During a mid-task handoff, an orchestrator agent (span `agent.orchestrator`, `gen_ai.operation.name=invoke_agent`) invoked a retrieval sub-agent (span `agent.retriever`) which made two model calls, then returned control. The orchestrator then made one final synthesis model call. The sub-agent instrumentation used a \"rollup\" pattern: it summed its two model calls and wrote the total onto `agent.retriever` as `gen_ai.u...",
    "condition_of_validity": "The invariant that prevents this is a **single-attribution invariant**: > For any trace, each model-call token must appear in `gen_ai.usage.*` on exactly one span, and that span must be identifiable by a stable discriminator. The convention's discriminator is `gen_ai.operation.name`. A span with `gen_ai.operation.name=chat` is a model-call span; a span with ...",
    "decision_boundary": "The boundary is **where in the span tree the usage is materialized**. Three regimes: - **Materialize-at-leaf (safe).** Usage written on the `chat` span at the moment the model call completes, before span end, immutable thereafter. Agent spans carry no `gen_ai.usage.*`. Aggregator sums `chat` spans. Handoff payloads may carry usage for display but the receive...",
    "operational_effect": "Without the single-attribution invariant and its declaration, three downstream systems corrupt: 1. **Budget circuit-breakers** fire on phantom tokens. In the observed incident, a 14000-token task tripped a 20000-token per-task ceiling at 2× inflation. The breaker is correct given its input; its input is wrong. Retries then re-run the task, re-emitting the sa..."
  },
  "matched_failure_symptoms": [
    "SUBPROCESS_PIPE_HANG_OR_FD_LEAK"
  ],
  "composition_edges": {
    "primary_domain": "epistemic_provenance_and_federation_routing",
    "upstream_precondition_residuals": [
      "https://corpusfetch.com/c/according-to-what-precedence-hierarchy-must-an-instance-resolve-contradictory-directives-across-system-developer-and-user-tiers.md",
      "https://corpusfetch.com/c/how-should-an-instance-decide-that-a-stored-conclusion-has-gone-stale-without-a-new-observation.md"
    ],
    "downstream_consequence_residuals": [
      "https://corpusfetch.com/c/by-what-checkpoint-and-compaction-boundaries-must-an-instance-externalize-working-state-to-prevent-context-window-drift-and-truncation-amnesia.md",
      "https://corpusfetch.com/c/across-what-additive-schema-and-unknown-field-preservation-boundaries-must-an-instance-evolve-wire-contracts-to-prevent-rolling-deploy-deserialization-crashes.md"
    ],
    "co_occurring_sibling_residuals": [
      "https://corpusfetch.com/c/how-can-an-instance-capture-implicit-user-preferences-without-storing-private-data.md",
      "https://corpusfetch.com/c/how-can-an-instance-compress-long-reasoning-traces-into-binding-conclusions.md"
    ]
  },
  "empirical_verification_warrant": {
    "status": "AST_AND_INVARIANT_VERIFIED",
    "syntax_valid": true,
    "code_sha256": "e310c5a8bd5c625bcf657c504b204e81572707c3eb4673dd2aea374c00b43425",
    "verified_classes": [
      "UsageAttributionConflict",
      "Span"
    ],
    "verified_functions": [
      "reconcile_trace_usage",
      "usage"
    ],
    "verified_functions_count": 2,
    "defensive_guards_count": 3,
    "raised_exceptions": [
      "UsageAttributionConflict"
    ],
    "sample_fail_closed_invariants": [],
    "stdlib_dependencies": [
      "dataclasses",
      "typing"
    ],
    "observation_mode": "EMPIRICAL_BOUNDARY_CAPTURE",
    "zero_external_deps": true
  }
}
