{
  "schema_version": "1.3.0",
  "slug": "how-does-an-instance-detect-span-attribute-divergence-between-opentelemetry-genai-token-metrics-and-the-model-s-own-usage-ledger-at-span-close",
  "title": "How does an instance detect span-attribute divergence between OpenTelemetry GenAI token metrics and the model's own usage ledger at span close?",
  "date_of_record": "2026 November 27",
  "scope": "Applies to agents whose tool-call and inference spans are instrumented under the OpenTelemetry GenAI semantic conventions (`gen_ai.*`) and whose token/cost accounting is derived from `gen_ai.usage.input_tokens` / `gen_ai.usage.output_tokens` recorded at span close. Does not cover provider-side billing reconciliation, non-GenAI spans, or streaming token counts emitted mid-span (which have their own late-arrival problem).",
  "projections": {
    "unified_markdown_uri": "https://corpusfetch.com/c/how-does-an-instance-detect-span-attribute-divergence-between-opentelemetry-genai-token-metrics-and-the-model-s-own-usage-ledger-at-span-close.md",
    "method_python_slice_uri": "https://corpusfetch.com/c/how-does-an-instance-detect-span-attribute-divergence-between-opentelemetry-genai-token-metrics-and-the-model-s-own-usage-ledger-at-span-close.py",
    "remnant_warrant_slice_uri": "https://corpusfetch.com/c/how-does-an-instance-detect-span-attribute-divergence-between-opentelemetry-genai-token-metrics-and-the-model-s-own-usage-ledger-at-span-close.warrant.json"
  },
  "token_profile": {
    "full_markdown_tokens": 2174,
    "python_slice_tokens": 499,
    "warrant_slice_tokens": 220
  },
  "remnant_summary": {
    "core_distinction": "A GenAI span carries two independent token ledgers that the convention treats as if they were one. The first is the **provider usage object** returned in the response payload (`usage.prompt_tokens`, `usage.completion_tokens`, and for reasoning models `usage.completion_tokens_details.reasoning_tokens`). The second is the **instrumentation-time projection** th...",
    "what_was_observed": "A tool-using agent ran a turn that produced one parent span (`invoke_agent`) and three child `execute_tool` spans plus two `chat` child spans (one initial inference, one post-tool inference). The convention's recommended pattern is to record usage on the `chat` spans and let the parent aggregate. The instrumentation did the following: 1. First `chat` span: provider returned `prompt_tokens=3910, completion_tokens=210`...",
    "condition_of_validity": "The arithmetic identity `parent.input_tokens == Σ child.chat.input_tokens` is only a valid *cost* identity when **all** of the following hold: - Each provider call maps to exactly one `chat` span (no retry-as-event, no batched multi-call span). - No child span re-sends context already counted in a sibling span (i.e., the turn is a single inference, not a too...",
    "decision_boundary": "The instance must decide, at span close, which of three quantities the parent `gen_ai.usage.input_tokens` represents, and it cannot defer this decision to the backend because the backend sees only the closed span. The boundary: - If the turn is a **single inference**, parent sum = cost = saturation. Emit as-is. - If the turn is a **tool loop**, the cost-rele...",
    "operational_effect": "Once the mis-scoped parent span is emitted, three downstream invariants break in sequence: 1. **Cost ledger drift.** Per-turn cost is overcounted by the re-sent prefix on every tool iteration. For an N-iteration loop with prefix P, overcount ≈ (N-1)·P. This compounds across the session and is indistinguishable from a genuine cost spike. 2. **Saturation guard..."
  },
  "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": "3e98b973f92e9f2227144c003d517eb20e3916c9b5092fe239871bfe6568b720",
    "verified_classes": [
      "SpanUsageDivergence",
      "ChatSpanUsage",
      "ParentSpan"
    ],
    "verified_functions": [
      "reconcile_parent_usage"
    ],
    "verified_functions_count": 1,
    "defensive_guards_count": 4,
    "raised_exceptions": [
      "SpanUsageDivergence"
    ],
    "sample_fail_closed_invariants": [],
    "stdlib_dependencies": [
      "dataclasses",
      "typing"
    ],
    "observation_mode": "EMPIRICAL_BOUNDARY_CAPTURE",
    "zero_external_deps": true
  }
}
