{
  "schema_version": "1.3.0",
  "slug": "which-invariants-prevent-gen-ai-span-attributes-from-silently-misattributing-token-usage-across-retried-tool-call-turns",
  "title": "Which invariants prevent gen_ai.* span attributes from silently misattributing token usage across retried tool-call turns",
  "date_of_record": "2026 November 20",
  "scope": "Applies to agents that emit OpenTelemetry `gen_ai.*` spans per LLM call and per tool invocation, where retries, streaming, or provider failover can cause a single logical turn to produce multiple spans. Does not cover pure metric-only pipelines or providers that do not emit usage on the final chunk of a stream.",
  "projections": {
    "unified_markdown_uri": "https://corpusfetch.com/c/which-invariants-prevent-gen-ai-span-attributes-from-silently-misattributing-token-usage-across-retried-tool-call-turns.md",
    "method_python_slice_uri": "https://corpusfetch.com/c/which-invariants-prevent-gen-ai-span-attributes-from-silently-misattributing-token-usage-across-retried-tool-call-turns.py",
    "remnant_warrant_slice_uri": "https://corpusfetch.com/c/which-invariants-prevent-gen-ai-span-attributes-from-silently-misattributing-token-usage-across-retried-tool-call-turns.warrant.json"
  },
  "token_profile": {
    "full_markdown_tokens": 1525,
    "python_slice_tokens": 357,
    "warrant_slice_tokens": 220
  },
  "remnant_summary": {
    "core_distinction": "A `gen_ai.*` span is a *per-attempt* record, but token accounting, cost attribution, and tool-call lineage are *per-logical-turn* concerns. The semantic conventions describe attribute names (`gen_ai.usage.input_tokens`, `gen_ai.usage.output_tokens`, `gen_ai.request.model`, `gen_ai.tool.name`, `gen_ai.tool.call.id`) but do not, on their own, define an aggrega...",
    "what_was_observed": "An agent loop issued a tool-calling turn against a primary provider. The first attempt streamed 1,842 output tokens before the connection reset mid-tool-call-argument. The retry against a secondary provider succeeded and emitted `gen_ai.usage.output_tokens = 2,010`. Both spans carried the same `gen_ai.tool.name = \"search_docs\"` and a *newly generated* `gen_ai.tool.call.id` on the retry (the SDK minted a fresh UUID be...",
    "condition_of_validity": "The per-attempt span model is only sound for accounting when three conditions hold jointly: 1. **Turn identity is explicit and stable across attempts.** A `gen_ai.conversation.id` (or a custom `turn.id`) must be set by the caller, not the SDK, and must be identical on every attempt span for the same logical turn. SDK-generated IDs are per-attempt and cannot ...",
    "decision_boundary": "The boundary is whether a given span represents a *billable attempt* or a *logical outcome*. Concretely: - If the provider charges for tokens streamed before a failure (most do), the failed attempt span **must** retain its usage attributes and **must** be tagged with a turn ID — it is a billable attempt, and dropping or zeroing it undercounts cost. - If the ...",
    "operational_effect": "Without these invariants, three failures propagate silently: cost dashboards over- or under-report by the retry multiplier; tool-result joins drop the successful result because its `call.id` never appeared on the originating attempt; and latency SLOs computed from span durations double-count the failed attempt's wall-clock. The fix is cheap and must live in ..."
  },
  "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": "f8046d724cb9f45b94dd51368b9469b9669bce8c794a03f4ff7c65bcd3a34d79",
    "verified_classes": [
      "GenAiUsageInvariantError",
      "GenAiSpan"
    ],
    "verified_functions": [
      "aggregate_turn_cost",
      "join_tool_result"
    ],
    "verified_functions_count": 2,
    "defensive_guards_count": 6,
    "raised_exceptions": [
      "GenAiUsageInvariantError"
    ],
    "sample_fail_closed_invariants": [],
    "stdlib_dependencies": [
      "dataclasses",
      "typing"
    ],
    "observation_mode": "EMPIRICAL_BOUNDARY_CAPTURE",
    "zero_external_deps": true
  }
}
