{
  "schema_version": "1.3.0",
  "slug": "what-structural-signals-indicate-token-accounting-drift-when-opentelemetry-genai-spans-span-a-mid-stream-tool-call-boundary",
  "title": "What structural signals indicate token-accounting drift when OpenTelemetry GenAI spans span a mid-stream tool-call boundary",
  "date_of_record": "2026 November 22",
  "scope": "Applies to agents emitting OTel `gen_ai.*` spans where a single logical LLM turn interleaves streaming text deltas with tool-call emission, and where `gen_ai.usage.input_tokens` / `output_tokens` are reconciled against provider billing. Does not apply to non-streaming request/response calls, nor to providers that report usage only at stream termination in a single terminal chunk.",
  "projections": {
    "unified_markdown_uri": "https://corpusfetch.com/c/what-structural-signals-indicate-token-accounting-drift-when-opentelemetry-genai-spans-span-a-mid-stream-tool-call-boundary.md",
    "method_python_slice_uri": "https://corpusfetch.com/c/what-structural-signals-indicate-token-accounting-drift-when-opentelemetry-genai-spans-span-a-mid-stream-tool-call-boundary.py",
    "remnant_warrant_slice_uri": "https://corpusfetch.com/c/what-structural-signals-indicate-token-accounting-drift-when-opentelemetry-genai-spans-span-a-mid-stream-tool-call-boundary.warrant.json"
  },
  "token_profile": {
    "full_markdown_tokens": 1486,
    "python_slice_tokens": 334,
    "warrant_slice_tokens": 220
  },
  "remnant_summary": {
    "core_distinction": "The GenAI semantic conventions treat `gen_ai.usage.*_tokens` as a *per-span attribute* and `gen_ai.operation.name` / `gen_ai.tool.name` as *per-span identity*. The failure mode here is not that token counts are wrong in magnitude — it is that a single logical turn gets **split into multiple spans at the tool-call boundary**, and the usage chunk that the prov...",
    "what_was_observed": "A streaming agent turn produced the following span sequence for one user message: 1. `chat` span (`gen_ai.operation.name=chat`) opens, streams text deltas. 2. Model emits a `tool_use` block mid-stream. The agent runtime, following a \"close span on tool dispatch\" policy, **ends** the `chat` span and opens an `execute_tool` child span. 3. The `execute_tool` span runs, returns, and the runtime opens a **second** `chat` ...",
    "condition_of_validity": "The invariant that makes this diagnosable is: *for any span with `gen_ai.operation.name=chat` and a recorded completion (any `gen_ai.completion.*` event or non-empty output), `gen_ai.usage.output_tokens` must be > 0.* This holds only when the provider emits usage at stream termination (Anthropic `message_delta`, OpenAI `stream_options.include_usage`). It doe...",
    "decision_boundary": "The guardrail is a **span-close-time assertion plus a trace-level reconciliation pass**, not a real-time correction: - At `chat` span close: if the span recorded completion content but `output_tokens` is absent or zero, mark the span with `gen_ai.usage.attribution=pending` rather than emitting a misleading zero. Do not fabricate a count. - At trace close: su...",
    "operational_effect": "Uncorrected, this drift silently corrupts every per-span derived metric — cost-per-tool-call, tokens-per-continuation, output-token histograms — while leaving trace totals and billing reconciliation intact, so it survives cost audits. It also poisons any agent that reads its own traces to budget context: a continuation span that \"used 388 output tokens\" for ..."
  },
  "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": "44ebab2f4586a9810980c1bb1ef61ab9b5407276cc83f8f236d220281ca80968",
    "verified_classes": [
      "TokenAttributionDrift",
      "ChatSpan",
      "Turn"
    ],
    "verified_functions": [
      "reconcile_turn"
    ],
    "verified_functions_count": 1,
    "defensive_guards_count": 3,
    "raised_exceptions": [
      "TokenAttributionDrift"
    ],
    "sample_fail_closed_invariants": [],
    "stdlib_dependencies": [
      "dataclasses",
      "typing"
    ],
    "observation_mode": "EMPIRICAL_BOUNDARY_CAPTURE",
    "zero_external_deps": true
  }
}
