{
  "schema_version": "1.3.0",
  "slug": "why-does-an-instance-of-a-genai-agent-span-silently-lose-token-attribution-when-a-tool-call-event-lands-after-the-span-has-been-ended",
  "title": "Why does an instance of a GenAI agent span silently lose token attribution when a tool-call event lands after the span has been ended",
  "date_of_record": "2026 November 13",
  "scope": "Applies to OpenTelemetry GenAI semantic conventions (as specified in `semconv` incubating `gen_ai.*` attributes) for agent invocation spans, tool-call child spans/events, and token usage attribution. Does not apply to plain LLM inference spans with no tool-call fan-out, nor to systems that post-process spans in a batch exporter with a late-merge stage.",
  "projections": {
    "unified_markdown_uri": "https://corpusfetch.com/c/why-does-an-instance-of-a-genai-agent-span-silently-lose-token-attribution-when-a-tool-call-event-lands-after-the-span-has-been-ended.md",
    "method_python_slice_uri": "https://corpusfetch.com/c/why-does-an-instance-of-a-genai-agent-span-silently-lose-token-attribution-when-a-tool-call-event-lands-after-the-span-has-been-ended.py",
    "remnant_warrant_slice_uri": "https://corpusfetch.com/c/why-does-an-instance-of-a-genai-agent-span-silently-lose-token-attribution-when-a-tool-call-event-lands-after-the-span-has-been-ended.warrant.json"
  },
  "token_profile": {
    "full_markdown_tokens": 1819,
    "python_slice_tokens": 422,
    "warrant_slice_tokens": 220
  },
  "remnant_summary": {
    "core_distinction": "The GenAI semantic conventions distinguish two structurally different mechanisms for recording tool activity: **tool-call events** (log records or span events attached to the parent agent span, carrying `gen_ai.tool.name`, `gen_ai.tool.call.id`, and arguments) and **tool-call child spans** (a span with `gen_ai.operation.name = execute_tool`, parented to the ...",
    "what_was_observed": "In a multi-step agent (ReAct-style loop) instrumented with a hand-rolled wrapper around the OTel Python SDK, the following sequence repeatedly produced orphaned token counts: 1. Agent span `S` started, model turn executed, `gen_ai.usage.input_tokens=1841`, `gen_ai.usage.output_tokens=212` set. 2. The model requested a tool call; the wrapper invoked the tool *synchronously* and recorded a `gen_ai.tool.call` event via ...",
    "condition_of_validity": "This record is valid when **all** of the following hold: - The exporter is a standard OTLP exporter using the SDK's default `BatchSpanProcessor` or `SimpleSpanProcessor` (both call `on_end` and discard post-end mutations). - The agent loop uses a *single* long-lived parent span for the whole conversation turn, with tool calls emitted as events rather than ch...",
    "decision_boundary": "The boundary is: **does the tool-call signal arrive before or after `span.end()`?** - If the tool signal is emitted *before* `end()`: the event is recorded, the token lineage is complete, and the invariant holds. - If the tool signal is emitted *after* `end()`: the event is dropped, the child span (if any) is orphaned, and the invariant is violated — but not...",
    "operational_effect": "Downstream, this manifests as: - **Undercounted cost.** Token-attribution dashboards that sum `gen_ai.usage.*` per `gen_ai.tool.call.id` show tool calls with zero cost and model turns with unattributed cost. Aggregate spend is correct only if the parent span's totals were set before `end()`; per-tool breakdowns are wrong. - **Broken lineage joins.** Trace-gr..."
  },
  "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": "869018d2ec7f7f87e0af011865c5d4932fc824a956099dc2ae15baa8e439179d",
    "verified_classes": [
      "TokenAttributionLineageError",
      "Span"
    ],
    "verified_functions": [
      "attribute_tokens",
      "run_agent_turn",
      "add_event",
      "end"
    ],
    "verified_functions_count": 4,
    "defensive_guards_count": 4,
    "raised_exceptions": [
      "TokenAttributionLineageError"
    ],
    "sample_fail_closed_invariants": [],
    "stdlib_dependencies": [
      "dataclasses",
      "time",
      "typing"
    ],
    "observation_mode": "EMPIRICAL_BOUNDARY_CAPTURE",
    "zero_external_deps": true
  }
}
