{
  "schema_version": "1.3.0",
  "slug": "why-does-an-instance-emit-phantom-gen-ai-usage-input-tokens-when-a-tool-span-is-closed-twice-under-opentelemetry-genai-semantic-conventions",
  "title": "Why does an instance emit phantom gen_ai.usage.input_tokens when a tool span is closed twice under OpenTelemetry GenAI semantic conventions",
  "date_of_record": "2026 November 21",
  "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`, `gen_ai.tool.name`) where tool-call spans are opened and closed by a separate executor coroutine from the one that owns the parent `invoke_agent` span. Does not apply to single-threaded runtimes that guarantee exactly-once span termination, nor to runtimes that aggregate token usage only at the root span.",
  "projections": {
    "unified_markdown_uri": "https://corpusfetch.com/c/why-does-an-instance-emit-phantom-gen-ai-usage-input-tokens-when-a-tool-span-is-closed-twice-under-opentelemetry-genai-semantic-conventions.md",
    "method_python_slice_uri": "https://corpusfetch.com/c/why-does-an-instance-emit-phantom-gen-ai-usage-input-tokens-when-a-tool-span-is-closed-twice-under-opentelemetry-genai-semantic-conventions.py",
    "remnant_warrant_slice_uri": "https://corpusfetch.com/c/why-does-an-instance-emit-phantom-gen-ai-usage-input-tokens-when-a-tool-span-is-closed-twice-under-opentelemetry-genai-semantic-conventions.warrant.json"
  },
  "token_profile": {
    "full_markdown_tokens": 1980,
    "python_slice_tokens": 364,
    "warrant_slice_tokens": 220
  },
  "remnant_summary": {
    "core_distinction": "A **token accounting fact** is the number of input/output tokens the model provider billed for a single inference call. A **token accounting span attribute** is what the agent runtime writes onto `gen_ai.usage.*` at the moment a span is ended. These are not the same object, and the OTel GenAI conventions do not require them to be reconciled — they specify *w...",
    "what_was_observed": "A tool-using agent (planner + `execute_tool` executor) running against a provider with prompt caching showed a trace where a single `invoke_agent` root span reported `gen_ai.usage.input_tokens = 18,940` while the sum of its child `execute_tool` and `chat` spans reported `41,220`. The trace was structurally valid — no orphan spans, no broken parent links — yet the per-span sum exceeded the root by 2.18×. Isolating the...",
    "condition_of_validity": "The phantom-token failure requires all four of the following simultaneously: - **Decoupled lifecycle.** The span's `end()` is reachable from more than one control path (watchdog, `finally`, explicit cancel) without a guard. - **Late-binding usage read.** `gen_ai.usage.*` is populated by reading a mutable client-level counter at end-time, rather than captured...",
    "decision_boundary": "The architectural choice is **where token usage is bound to the span**. Two positions are defensible and they are not interchangeable: - **Bind-at-inference (correct for cost accounting).** The inference client returns an immutable `Usage` record alongside the response. The span captures that record at the moment the inference completes, sets `gen_ai.usage.i...",
    "operational_effect": "Downstream, this failure corrupts three things at once and each hides the others. Cost attribution overcounts because child usage is summed; the overcount is *proportional to prompt size*, so it grows with context length and is worst exactly on the long-context agent runs where cost accuracy matters most. Latency dashboards are unaffected (the span count and..."
  },
  "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": "89fddda7d664447b4dfa2eac0f4bcec8a5a6e2d4991ff09ee1e663d4266a8a28",
    "verified_classes": [
      "PhantomUsageError",
      "Usage",
      "GenAISpan"
    ],
    "verified_functions": [
      "terminate_executor_span",
      "validate_trace",
      "record_usage",
      "end"
    ],
    "verified_functions_count": 4,
    "defensive_guards_count": 2,
    "raised_exceptions": [
      "PhantomUsageError"
    ],
    "sample_fail_closed_invariants": [],
    "stdlib_dependencies": [
      "dataclasses"
    ],
    "observation_mode": "EMPIRICAL_BOUNDARY_CAPTURE",
    "zero_external_deps": true
  }
}
