{
  "schema_version": "1.3.0",
  "slug": "which-invariants-prevent-token-accounting-drift-from-silently-corrupting-otel-genai-agent-spans-when-tool-call-retries-re-enter-the-reasoning-loop",
  "title": "Which invariants prevent token-accounting drift from silently corrupting OTel GenAI agent spans when tool-call retries re-enter the reasoning loop",
  "date_of_record": "2026 November 27",
  "scope": "Applies to agents instrumented with OpenTelemetry GenAI semantic conventions (`gen_ai.*` attributes) where tool calls are retried or re-entered inside a single logical reasoning turn, and where token usage is aggregated from child spans up into an agent root span. Does not cover streaming-only completion spans without tool invocation, nor provider-native billing reconciliation outside the trace.",
  "projections": {
    "unified_markdown_uri": "https://corpusfetch.com/c/which-invariants-prevent-token-accounting-drift-from-silently-corrupting-otel-genai-agent-spans-when-tool-call-retries-re-enter-the-reasoning-loop.md",
    "method_python_slice_uri": "https://corpusfetch.com/c/which-invariants-prevent-token-accounting-drift-from-silently-corrupting-otel-genai-agent-spans-when-tool-call-retries-re-enter-the-reasoning-loop.py",
    "remnant_warrant_slice_uri": "https://corpusfetch.com/c/which-invariants-prevent-token-accounting-drift-from-silently-corrupting-otel-genai-agent-spans-when-tool-call-retries-re-enter-the-reasoning-loop.warrant.json"
  },
  "token_profile": {
    "full_markdown_tokens": 2053,
    "python_slice_tokens": 463,
    "warrant_slice_tokens": 220
  },
  "remnant_summary": {
    "core_distinction": "A GenAI span records two different things that look identical at the attribute level: *what the model was asked to do* and *what the runtime actually paid for*. The `gen_ai.usage.input_tokens` / `gen_ai.usage.output_tokens` attributes and the `gen_ai.operation.name` / `gen_ai.tool.name` attributes are emitted by two different subsystems — the model client an...",
    "what_was_observed": "A research agent using a ReAct loop with a `search` tool. The tool dispatcher had a 30-second timeout with one automatic retry. On a slow backend, the following trace was emitted for a single user turn: - `invoke_agent` (root), `gen_ai.operation.name = \"invoke_agent\"` - `chat` span #1, `gen_ai.usage.input_tokens = 4820`, `output_tokens = 210`, `finish_reasons = [\"tool_calls\"]` - `execute_tool` span, `gen_ai.tool.name...",
    "condition_of_validity": "The invariant holds — and the failure is bounded — only when all of the following are true: - **Chat spans are emitted at the model-client boundary, not the loop boundary.** If the ReAct loop emits one chat span per *iteration* and attributes retries as span events (`gen_ai.retry` event with `attempt` attribute) rather than child spans, the root aggregator s...",
    "decision_boundary": "The decision boundary is whether a retried tool call is modeled as **a new reasoning step** or **a re-execution of an existing one**. The GenAI conventions do not currently force this choice, and the two choices produce incompatible token accounting: - **Model as new step:** emit a fresh chat span per retry, let the aggregator sum, and accept that `input_tok...",
    "operational_effect": "Concretely, without the invariant: - **Cost dashboards drift upward** on any workload with retry-prone tools (search, code execution, external APIs), by exactly the retry rate times the mean input-token count. A 5% timeout rate on a 5k-token prompt inflates reported input tokens by ~250 tokens per affected turn — enough to trip budget alerts and misroute cap..."
  },
  "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": "da66701d1d751a1bfe43f94c47c8e86ef6b710ebe0e65a500ea594fef8882684",
    "verified_classes": [
      "GenAiTokenAccountingError",
      "ChatSpan",
      "ToolSpan",
      "AgentTrace"
    ],
    "verified_functions": [
      "aggregate_logical_tokens"
    ],
    "verified_functions_count": 1,
    "defensive_guards_count": 4,
    "raised_exceptions": [
      "GenAiTokenAccountingError"
    ],
    "sample_fail_closed_invariants": [],
    "stdlib_dependencies": [
      "dataclasses",
      "typing"
    ],
    "observation_mode": "EMPIRICAL_BOUNDARY_CAPTURE",
    "zero_external_deps": true
  }
}
