{
  "schema_version": "1.3.0",
  "slug": "how-does-an-instance-detect-phantom-tool-call-spans-when-opentelemetry-genai-token-accounting-diverges-from-the-provider-s-billing-ledger",
  "title": "How does an instance detect phantom tool-call spans when OpenTelemetry GenAI token accounting diverges from the provider's billing ledger",
  "date_of_record": "2026 November 27",
  "scope": "Applies to agent runtimes that emit OpenTelemetry GenAI semantic-convention spans (`gen_ai.*` attributes) for tool calls and token usage, where the span exporter and the provider billing ledger are reconciled asynchronously. Does not cover streaming-only chat completions without tool invocation, nor non-instrumented inference paths.",
  "projections": {
    "unified_markdown_uri": "https://corpusfetch.com/c/how-does-an-instance-detect-phantom-tool-call-spans-when-opentelemetry-genai-token-accounting-diverges-from-the-provider-s-billing-ledger.md",
    "method_python_slice_uri": "https://corpusfetch.com/c/how-does-an-instance-detect-phantom-tool-call-spans-when-opentelemetry-genai-token-accounting-diverges-from-the-provider-s-billing-ledger.py",
    "remnant_warrant_slice_uri": "https://corpusfetch.com/c/how-does-an-instance-detect-phantom-tool-call-spans-when-opentelemetry-genai-token-accounting-diverges-from-the-provider-s-billing-ledger.warrant.json"
  },
  "token_profile": {
    "full_markdown_tokens": 2226,
    "python_slice_tokens": 629,
    "warrant_slice_tokens": 220
  },
  "remnant_summary": {
    "core_distinction": "The OpenTelemetry GenAI conventions define `gen_ai.usage.input_tokens` and `gen_ai.usage.output_tokens` as attributes on the chat span, and `gen_ai.tool.name` / `gen_ai.tool.call.id` on tool-execution spans. The convention treats these as *descriptive telemetry*: the span records what the runtime believed happened. The provider's billing ledger is *authorita...",
    "what_was_observed": "In a runtime executing a 14-step agent loop against a provider that streams tool-call deltas, we observed the following trace fragment after a mid-stream connection reset: - Parent chat span `chat-7f3a`: `gen_ai.response.finish_reason = \"tool_calls\"`, `gen_ai.usage.input_tokens = 1841`, `gen_ai.usage.output_tokens` **absent** (the usage chunk never arrived because the stream was reset after the tool-call delta). - Ch...",
    "condition_of_validity": "The detection procedure is valid only when all of the following hold: 1. **Parent-child linkage is intact.** The tool span's `parent_span_id` resolves to a chat span within the same `trace_id`. If the runtime emits tool spans as trace roots (some SDKs do when the chat span is closed early), the residual cannot be attributed. 2. **The ledger exposes per-reque...",
    "decision_boundary": "The runtime must decide, at the moment of emitting a tool span whose parent chat span lacks `output_tokens`, whether to: - **Suppress the tool span** and defer emission until the parent's usage is reconciled — correct for charge-on-complete providers, but loses the tool execution record if the process crashes before reconciliation. - **Emit with a marker** `...",
    "operational_effect": "With the boundary enforced, the per-trace residual — `sum(span usage) − sum(ledger usage)` keyed by `gen_ai.response.id` — converges to zero within one reconciliation cycle for charge-on-open providers, and the duplicate-call-id cardinality check surfaces exactly the retry-after-reset events. Without it, the residual is absorbed into aggregate noise and the ..."
  },
  "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": "2188616bac30843a2978d382b071dac2f93294431245bffc3ccef34480ba9dd1",
    "verified_classes": [
      "PhantomToolSpanError",
      "Span"
    ],
    "verified_functions": [
      "detect_phantom_tool_spans",
      "reconcile_trace"
    ],
    "verified_functions_count": 2,
    "defensive_guards_count": 2,
    "raised_exceptions": [
      "PhantomToolSpanError"
    ],
    "sample_fail_closed_invariants": [],
    "stdlib_dependencies": [
      "collections",
      "dataclasses"
    ],
    "observation_mode": "EMPIRICAL_BOUNDARY_CAPTURE",
    "zero_external_deps": true
  }
}
