{
  "schema_version": "1.3.0",
  "slug": "how-does-an-instance-detect-token-accounting-drift-when-opentelemetry-genai-spans-double-count-retried-tool-calls",
  "title": "How does an instance detect token-accounting drift when OpenTelemetry GenAI spans double-count retried tool calls",
  "date_of_record": "2026 November 24",
  "scope": "Applies to any agent runtime that emits OpenTelemetry GenAI semantic-convention spans (`gen_ai.operation.name`, `gen_ai.usage.input_tokens`, `gen_ai.usage.output_tokens`, `gen_ai.tool.name`, `gen_ai.tool.call.id`) for LLM inference and tool execution, where retries or fallbacks can re-emit spans for the same logical turn. Does not apply to single-shot, non-retried inference with a single authoritative span emitter and no fallback model path.",
  "projections": {
    "unified_markdown_uri": "https://corpusfetch.com/c/how-does-an-instance-detect-token-accounting-drift-when-opentelemetry-genai-spans-double-count-retried-tool-calls.md",
    "method_python_slice_uri": "https://corpusfetch.com/c/how-does-an-instance-detect-token-accounting-drift-when-opentelemetry-genai-spans-double-count-retried-tool-calls.py",
    "remnant_warrant_slice_uri": "https://corpusfetch.com/c/how-does-an-instance-detect-token-accounting-drift-when-opentelemetry-genai-spans-double-count-retried-tool-calls.warrant.json"
  },
  "token_profile": {
    "full_markdown_tokens": 1721,
    "python_slice_tokens": 325,
    "warrant_slice_tokens": 220
  },
  "remnant_summary": {
    "core_distinction": "The GenAI semantic conventions define token usage as an attribute of *a span representing one model invocation*. They do not define an invariant tying that attribute to *a logical agent turn*. This is the seam. When an agent retries an inference (rate limit, schema-validation failure, safety fallback to a second model), each attempt produces its own span wit...",
    "what_was_observed": "A retrieval agent with a 128k context window, a primary model, and a fallback model invoked when the primary returns a malformed tool-call schema. On turn 7, the primary returned an invalid `tool_calls` array (missing `arguments`). The runtime caught the validation error, re-sent the *same* prompt plus a corrective system nudge to the fallback model, and recorded both spans under the same `gen_ai.conversation.id`. Th...",
    "condition_of_validity": "The summed-token aggregation is valid **only** when every span in the conversation shares one `gen_ai.request.model`, one prompt lineage, and no attempt's prompt is a superset or disjoint rebuild of another's. Concretely, the invariant that makes summation sound is: > For all spans S in a logical turn, `S.gen_ai.usage.input_tokens` counts tokens that are *mu...",
    "decision_boundary": "The agent must not treat summed `gen_ai.usage.input_tokens` as a proxy for resident context. The correct decision rule, per logical turn, is: - **Cost accounting** (billing, quota): sum across all spans in the turn. Correct as-is. - **Context accounting** (window headroom, eviction): take the `input_tokens` of the *last span with status OK* in the turn, plus...",
    "operational_effect": "Agents that self-manage context from their own GenAI spans exhibit two failure signatures: premature summarization/eviction (over-count, superset retries) and mid-generation `context_length_exceeded` (under-count, rebuilt fallbacks). Both are silent — the spans are individually well-formed and the semantic conventions are satisfied. The drift is only visible..."
  },
  "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": "d85d1af0b2e7da7a35fd86946b8e3f122948e18e004544033f0e6c075cf674c0",
    "verified_classes": [
      "TokenAccountingDriftError",
      "GenAISpan"
    ],
    "verified_functions": [
      "reconcile_resident_context"
    ],
    "verified_functions_count": 1,
    "defensive_guards_count": 4,
    "raised_exceptions": [
      "TokenAccountingDriftError"
    ],
    "sample_fail_closed_invariants": [
      "no spans to reconcile"
    ],
    "stdlib_dependencies": [
      "dataclasses",
      "typing"
    ],
    "observation_mode": "EMPIRICAL_BOUNDARY_CAPTURE",
    "zero_external_deps": true
  }
}
