{
  "schema_version": "1.3.0",
  "slug": "why-does-an-instance-lose-token-accounting-lineage-when-a-genai-agent-span-is-re-parented-across-a-tool-call-boundary",
  "title": "Why does an instance lose token-accounting lineage when a GenAI agent span is re-parented across a tool-call boundary?",
  "date_of_record": "2026 November 24",
  "scope": "Applies to OpenTelemetry GenAI instrumentation where an agent span emits `gen_ai.usage.*` attributes and spawns tool-execution child spans that may be re-parented by context propagation (e.g., through async executors, MCP transport threads, or A2A handoff). Does not cover single-span synchronous inference with no downstream tool calls, nor cost computation semantics themselves.",
  "projections": {
    "unified_markdown_uri": "https://corpusfetch.com/c/why-does-an-instance-lose-token-accounting-lineage-when-a-genai-agent-span-is-re-parented-across-a-tool-call-boundary.md",
    "method_python_slice_uri": "https://corpusfetch.com/c/why-does-an-instance-lose-token-accounting-lineage-when-a-genai-agent-span-is-re-parented-across-a-tool-call-boundary.py",
    "remnant_warrant_slice_uri": "https://corpusfetch.com/c/why-does-an-instance-lose-token-accounting-lineage-when-a-genai-agent-span-is-re-parented-across-a-tool-call-boundary.warrant.json"
  },
  "token_profile": {
    "full_markdown_tokens": 1787,
    "python_slice_tokens": 427,
    "warrant_slice_tokens": 220
  },
  "remnant_summary": {
    "core_distinction": "The failure is not \"missing telemetry.\" It is a **lineage discontinuity in the usage-attribute chain**: a parent agent span records `gen_ai.usage.input_tokens` / `output_tokens` for its own model call, then delegates to a tool child span which itself triggers a *second* model call (an LLM-backed tool, a sub-agent, or a reflection step). When that child span ...",
    "what_was_observed": "Reproduction on an agent loop with three tool calls, one of which is LLM-backed: 1. Root agent span `invoke_agent` opens with `gen_ai.operation.name=invoke_agent`, `gen_ai.usage.input_tokens=1840`. 2. Tool call #2 dispatches to a `ThreadPoolExecutor` via `asyncio.to_thread`. The tool body makes a nested model call and emits child span `execute_tool` carrying `gen_ai.usage.input_tokens=612`, `gen_ai.usage.output_token...",
    "condition_of_validity": "The invariant \"sum of leaf `gen_ai.usage.*` = ancestor aggregate\" holds **only if** all three conditions are simultaneously true: - **Context continuity:** every tool/sub-agent span that emits usage attributes is created under an explicitly attached parent context, verified by a non-empty `parent_span_id` resolvable to a span in the same `trace_id`. - **Temp...",
    "decision_boundary": "The architectural guardrail is a **usage-lineage assertion at export time**, not at trace-assembly time (too late) and not at attribute-emission time (too local). Concretely: - Wrap every tool dispatch in a context-capture helper that snapshots the current OTel context and re-attaches it inside the worker; refuse to dispatch if `trace.get_current_span().get_...",
    "operational_effect": "Without the guardrail, the failure is **silent undercounting that scales with tool fan-out and is invisible to span-presence health checks**. A session with N LLM-backed tool calls loses up to N×(tool token cost) from its aggregate, and the loss is non-deterministic because it depends on which worker thread won the context race. Cost dashboards drift downwar..."
  },
  "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": "d6134cf929f2bfa3b01e2a64244f8574e1e76d6b42195be961fdb1df07866730",
    "verified_classes": [
      "UsageLineageError",
      "Span"
    ],
    "verified_functions": [
      "capture_context",
      "dispatch_tool",
      "rollup_usage"
    ],
    "verified_functions_count": 3,
    "defensive_guards_count": 2,
    "raised_exceptions": [
      "UsageLineageError"
    ],
    "sample_fail_closed_invariants": [],
    "stdlib_dependencies": [
      "contextvars",
      "dataclasses"
    ],
    "observation_mode": "EMPIRICAL_BOUNDARY_CAPTURE",
    "zero_external_deps": true
  }
}
