{
  "schema_version": "1.3.0",
  "slug": "why-does-an-instance-of-an-agent-span-misattribute-child-tool-call-tokens-under-opentelemetry-genai-semantic-conventions-when-the-parent-span-closes-b",
  "title": "Why does an instance of an agent span misattribute child tool-call tokens under OpenTelemetry GenAI semantic conventions when the parent span closes before the tool executor emits its usage event?",
  "date_of_record": "2026 December 1",
  "scope": "Applies to agent runtimes that emit OpenTelemetry spans per the GenAI semantic conventions (`gen_ai.*` attributes) where a parent `invoke_agent` span wraps one or more `execute_tool` child spans and token usage is recorded via `gen_ai.usage.input_tokens` / `gen_ai.usage.output_tokens`. Does not apply to flat, single-span LLM instrumentation or to runtimes where tool spans are emitted by a separate process that shares the parent's span context by reference (remote parent) rather than in-process.",
  "projections": {
    "unified_markdown_uri": "https://corpusfetch.com/c/why-does-an-instance-of-an-agent-span-misattribute-child-tool-call-tokens-under-opentelemetry-genai-semantic-conventions-when-the-parent-span-closes-b.md",
    "method_python_slice_uri": "https://corpusfetch.com/c/why-does-an-instance-of-an-agent-span-misattribute-child-tool-call-tokens-under-opentelemetry-genai-semantic-conventions-when-the-parent-span-closes-b.py",
    "remnant_warrant_slice_uri": "https://corpusfetch.com/c/why-does-an-instance-of-an-agent-span-misattribute-child-tool-call-tokens-under-opentelemetry-genai-semantic-conventions-when-the-parent-span-closes-b.warrant.json"
  },
  "token_profile": {
    "full_markdown_tokens": 1642,
    "python_slice_tokens": 333,
    "warrant_slice_tokens": 220
  },
  "remnant_summary": {
    "core_distinction": "The failure is not \"tokens are lost.\" It is \"tokens are attributed to the wrong span, and the attribution is silently *plausible*.\" Under the GenAI conventions, token usage is a span-level attribute, not an event, and the OTel SDK permits setting attributes only while a span is `RECORDING`. When an agent framework closes the parent `invoke_agent` span before...",
    "what_was_observed": "A three-tool agent turn (`search`, `fetch`, `summarize`) emitted a trace where: - `invoke_agent` (parent) reported `gen_ai.usage.output_tokens = 412`. - `execute_tool(search)` reported `gen_ai.usage.input_tokens = 0` — impossible, since search returned a 9 KB document. - `execute_tool(fetch)` reported `gen_ai.usage.input_tokens = 3104`, roughly the sum of *search's* and *fetch's* real inputs. - `execute_tool(summariz...",
    "condition_of_validity": "The misattribution is *observable and reproducible* only when all of the following hold: - The tool executor is asynchronous or deferred relative to the span that models it (e.g., a worker queue, a `asyncio.create_task` that outlives the `with tracer.start_as_current_span(...)` block). - Usage is reported *after* the modeling span ends, whether by explicit `...",
    "decision_boundary": "The architectural guardrail is a **usage-emission contract**: every span that models a billable unit must have its `gen_ai.usage.*` attributes set *before* `end()`, and must be addressed by a captured `Span` handle, never by ambient context. Concretely: - The tool executor receives the `Span` object (or a `SpanHandle` wrapper) as an argument, not a context l...",
    "operational_effect": "Without the guardrail, three downstream systems degrade: 1. **Per-tool cost attribution** inverts: expensive tools appear cheap, cheap tools appear expensive, because late writes land on the next sibling. Budget alerts fire on the wrong tool. 2. **Per-agent token budgets** computed by summing child spans remain correct in aggregate, masking the defect until ..."
  },
  "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": "c7e1ddd9f9fb7abbbf0e3b1a40798087f68cbeb9a9190f9d7dbff3aae9213b86",
    "verified_classes": [
      "UsageEmissionAfterEndError",
      "ToolUsageBuffer"
    ],
    "verified_functions": [
      "execute_tool",
      "__init__",
      "record_input",
      "record_output",
      "flush_and_end"
    ],
    "verified_functions_count": 5,
    "defensive_guards_count": 4,
    "raised_exceptions": [
      "UsageEmissionAfterEndError"
    ],
    "sample_fail_closed_invariants": [
      "input tokens recorded after flush; span already ended",
      "output tokens recorded after flush; span already ended"
    ],
    "stdlib_dependencies": [
      "opentelemetry"
    ],
    "observation_mode": "EMPIRICAL_BOUNDARY_CAPTURE",
    "zero_external_deps": true
  }
}
