{
  "schema_version": "1.3.0",
  "slug": "what-structural-signals-indicate-token-usage-metric-corruption-when-opentelemetry-genai-agent-spans-are-closed-out-of-causal-order",
  "title": "What structural signals indicate token-usage metric corruption when OpenTelemetry GenAI agent spans are closed out of causal order",
  "date_of_record": "2026 November 25",
  "scope": "Applies to agents that emit OpenTelemetry GenAI spans (`gen_ai.*` semantic conventions) where a parent agent span's `gen_ai.usage.input_tokens`/`output_tokens` is derived by aggregation of child tool-call and LLM-call spans, and where those children may complete after the parent's `end()` is invoked. Does not apply to flat, single-span instrumentation or to backends that recompute usage from raw event streams independent of span lifecycle.",
  "projections": {
    "unified_markdown_uri": "https://corpusfetch.com/c/what-structural-signals-indicate-token-usage-metric-corruption-when-opentelemetry-genai-agent-spans-are-closed-out-of-causal-order.md",
    "method_python_slice_uri": "https://corpusfetch.com/c/what-structural-signals-indicate-token-usage-metric-corruption-when-opentelemetry-genai-agent-spans-are-closed-out-of-causal-order.py",
    "remnant_warrant_slice_uri": "https://corpusfetch.com/c/what-structural-signals-indicate-token-usage-metric-corruption-when-opentelemetry-genai-agent-spans-are-closed-out-of-causal-order.warrant.json"
  },
  "token_profile": {
    "full_markdown_tokens": 1642,
    "python_slice_tokens": 363,
    "warrant_slice_tokens": 220
  },
  "remnant_summary": {
    "core_distinction": "The GenAI semantic conventions treat `gen_ai.usage.*` as a *span-local* attribute: each span reports the tokens it consumed. What most agent runtimes actually need is a *subtree-aggregate*: the parent agent span must report the sum of its own reasoning tokens plus every descendant LLM and tool-call span. These are not the same number, and the conventions do ...",
    "what_was_observed": "A three-tool agent turn (`search`, `fetch`, `summarize`) produced the following trace fragment. The orchestrator span `agent.turn` was closed immediately after dispatching `summarize`, because the orchestrator used a `asyncio.create_task` fan-out and did not await the final tool. Observed attributes: - `agent.turn` span: `end_time` = T+410ms, `gen_ai.usage.input_tokens=1840`, `gen_ai.usage.output_tokens=210`. These a...",
    "condition_of_validity": "The aggregate-usage invariant holds only when the following three conditions are jointly true: 1. **Causal closure:** every descendant span's `end_time` is less than or equal to its parent's `end_time`. Equivalently, the parent span is finalized strictly after all children. 2. **Monotonic attribute assignment:** `gen_ai.usage.*` is set once, before `end()`, ...",
    "decision_boundary": "The boundary is: **does the orchestrator's control flow join all descendant work before closing its span?** - If the orchestrator `await`s every tool and LLM call (structured concurrency, `asyncio.TaskGroup`, explicit join), condition 1 holds and aggregation at `end()` is correct. - If the orchestrator fans out and returns (fire-and-forget, detached tasks, s...",
    "operational_effect": "Downstream, the corrupted metric propagates into cost attribution and budget enforcement. A budget guard that reads `agent.turn` subtree tokens will under-count by the entire fan-out tail—here 7000 of 9050 tokens, a 77% under-count. The agent's token-budget loop therefore believes it has headroom it does not have, and continues dispatching until the *provide..."
  },
  "matched_failure_symptoms": [
    "DOMAIN:EPISTEMIC_PROVENANCE_AND_FEDERATION_ROUTING"
  ],
  "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": "d89cca5c1a20b839e4635f987da86464e2de36fdd569c004683520c38ec590bb",
    "verified_classes": [
      "SpanClosureViolation",
      "UsageAggregationError",
      "GenAISpan"
    ],
    "verified_functions": [
      "aggregate_subtree_usage",
      "open_child",
      "close_child",
      "end"
    ],
    "verified_functions_count": 4,
    "defensive_guards_count": 6,
    "raised_exceptions": [
      "SpanClosureViolation",
      "UsageAggregationError"
    ],
    "sample_fail_closed_invariants": [],
    "stdlib_dependencies": [
      "dataclasses",
      "time",
      "typing"
    ],
    "observation_mode": "EMPIRICAL_BOUNDARY_CAPTURE",
    "zero_external_deps": true
  }
}
