{
  "schema_version": "1.3.0",
  "slug": "in-what-scenarios-must-an-instance-reconcile-opentelemetry-genai-span-attributes-against-the-token-ledger-before-emitting-a-completion-and-why-the-nai",
  "title": "In what scenarios must an instance reconcile OpenTelemetry GenAI span attributes against the token ledger before emitting a completion, and why the naive `gen_ai.usage.*` emission silently corrupts agent cost accounting",
  "date_of_record": "2026 November 24",
  "scope": "Applies to LLM-serving agent runtimes that emit OpenTelemetry spans conforming to the GenAI semantic conventions (`gen_ai.*`) and that perform streaming generation with tool-call interleaving, retries, or speculative token emission. Does not apply to batch/offline inference where a single span wraps the entire request with no partial-token emission and no tool-call sub-spans.",
  "projections": {
    "unified_markdown_uri": "https://corpusfetch.com/c/in-what-scenarios-must-an-instance-reconcile-opentelemetry-genai-span-attributes-against-the-token-ledger-before-emitting-a-completion-and-why-the-nai.md",
    "method_python_slice_uri": "https://corpusfetch.com/c/in-what-scenarios-must-an-instance-reconcile-opentelemetry-genai-span-attributes-against-the-token-ledger-before-emitting-a-completion-and-why-the-nai.py",
    "remnant_warrant_slice_uri": "https://corpusfetch.com/c/in-what-scenarios-must-an-instance-reconcile-opentelemetry-genai-span-attributes-against-the-token-ledger-before-emitting-a-completion-and-why-the-nai.warrant.json"
  },
  "token_profile": {
    "full_markdown_tokens": 2276,
    "python_slice_tokens": 517,
    "warrant_slice_tokens": 220
  },
  "remnant_summary": {
    "core_distinction": "The GenAI semantic conventions define `gen_ai.usage.input_tokens` and `gen_ai.usage.output_tokens` as *counts of tokens actually consumed by the model operation the span represents*. They are not a mirror of what the caller *requested* or what the client *received*. The boundary condition that breaks most implementations is the conflation of these three quan...",
    "what_was_observed": "A tool-using agent loop (plan → `search` → `summarize` → `answer`) running at 3 tool calls per turn, with a retry-on-5xx policy at the transport layer. Each turn emitted one `gen_ai.chat` span and one child span per tool call. The span exporter used an `on_end` hook that read the accumulated streamed text and recomputed tokens via a local BPE tokenizer, writing the result into `gen_ai.usage.output_tokens`. Three dist...",
    "condition_of_validity": "The reconciliation guardrail is valid only under the following conditions, and must explicitly detect their violation: - **The provider exposes an authoritative usage record** (in the final stream chunk, a `usage` field on the response, or a billing export keyed by request ID). If the only token source is a local tokenizer, the invariant `attributed == bille...",
    "decision_boundary": "The runtime must decide, per completion, between two emission modes: - **Reconciled mode** — an authoritative usage record was received for the attempt. Emit `gen_ai.usage.input_tokens` and `gen_ai.usage.output_tokens` from that record, set `gen_ai.usage.source = \"provider\"`, and assert the per-trace sum equals the invoice. This is the default and the only m...",
    "operational_effect": "With the reconciliation gate in place, the trace-level token sum matched the provider invoice to within 0.1% across a 72-hour window, and the previously invisible 22% gap resolved into three named, bounded categories: aborted-attempt tokens (now carried by their own spans), stop-sequence lookahead (now included from the provider record), and tool-call payloa..."
  },
  "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": "1f1d5fabdb8b9e915a9def34f6796d932b3f48f58e2de9c382343df7fd68da45",
    "verified_classes": [
      "UsageSource",
      "TokenReconciliationError",
      "AttemptUsage",
      "GenAiSpan"
    ],
    "verified_functions": [
      "close_chat_span",
      "reconcile_trace"
    ],
    "verified_functions_count": 2,
    "defensive_guards_count": 5,
    "raised_exceptions": [
      "TokenReconciliationError"
    ],
    "sample_fail_closed_invariants": [
      "trace has no provider-sourced spans; invoice cannot be reconciled"
    ],
    "stdlib_dependencies": [
      "dataclasses",
      "enum",
      "typing"
    ],
    "observation_mode": "EMPIRICAL_BOUNDARY_CAPTURE",
    "zero_external_deps": true
  }
}
