{
  "schema_version": "1.3.0",
  "slug": "why-does-an-instance-s-token-accounting-diverge-from-its-trace-when-a-tool-call-is-retried-across-a-span-boundary",
  "title": "Why does an instance's token accounting diverge from its trace when a tool call is retried across a span boundary",
  "date_of_record": "2026 November 26",
  "scope": "Applies to agent runtimes that emit OpenTelemetry spans conforming to the GenAI semantic conventions (`gen_ai.*` attributes) and that perform automatic retries of tool calls within a single logical agent turn. Does not apply to single-shot LLM calls without tool loops, nor to systems that emit exactly one span per billable API request with no retry fan-out.",
  "projections": {
    "unified_markdown_uri": "https://corpusfetch.com/c/why-does-an-instance-s-token-accounting-diverge-from-its-trace-when-a-tool-call-is-retried-across-a-span-boundary.md",
    "method_python_slice_uri": "https://corpusfetch.com/c/why-does-an-instance-s-token-accounting-diverge-from-its-trace-when-a-tool-call-is-retried-across-a-span-boundary.py",
    "remnant_warrant_slice_uri": "https://corpusfetch.com/c/why-does-an-instance-s-token-accounting-diverge-from-its-trace-when-a-tool-call-is-retried-across-a-span-boundary.warrant.json"
  },
  "token_profile": {
    "full_markdown_tokens": 1590,
    "python_slice_tokens": 337,
    "warrant_slice_tokens": 220
  },
  "remnant_summary": {
    "core_distinction": "There are two different quantities that both travel under the name \"tokens\": the **billable usage** reported by the inference provider on each HTTP response (`usage.prompt_tokens`, `usage.completion_tokens`), and the **semantic token attribution** that a trace consumer reconstructs by summing `gen_ai.usage.input_tokens` / `gen_ai.usage.output_tokens` across ...",
    "what_was_observed": "A ReAct-style agent executes a turn: `chat.completions` with tool definitions → model returns a `tool_calls` payload → runtime invokes the tool → tool returns a transient error (HTTP 503 from a downstream API) → runtime retries the tool → tool succeeds → runtime issues a **second** `chat.completions` call to feed the tool result back. The runtime, following a common pattern, wraps the *entire* tool invocation (attemp...",
    "condition_of_validity": "The invariant \"sum of `gen_ai.usage.input_tokens` over all spans in a trace equals the provider's billed input tokens for that trace\" holds **only if** all three of the following are true: 1. Every provider response is represented by exactly one span carrying its usage, and no span carries usage derived from more than one response. 2. Retried tool attempts t...",
    "decision_boundary": "The boundary is: **does the trace consumer need per-call attribution or per-turn totals?** If per-call (e.g., to detect prompt bloat from retry-injected messages), the consumer must read only spans where `gen_ai.operation.name == \"chat\"` and must be aware that a retry can inflate the *next* call's `input_tokens` by the size of the injected error message. If ...",
    "operational_effect": "A cost dashboard built on trace-summed `gen_ai.usage.input_tokens` will under-report by the amount of double-counting on parent spans and will mis-attribute retry inflation to \"model verbosity.\" An agent-debugging tool that compares `input_tokens` across turns to detect context-window saturation will fire false alarms on retry-heavy turns. The fix is a two-p..."
  },
  "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": "f70c6bb3f4aafa310b2de7a78202b0d0bab1d791db9c113e17b9132d0dd4d15a",
    "verified_classes": [
      "TraceTokenReconciliationError"
    ],
    "verified_functions": [
      "reconcile_trace_tokens"
    ],
    "verified_functions_count": 1,
    "defensive_guards_count": 3,
    "raised_exceptions": [
      "TraceTokenReconciliationError"
    ],
    "sample_fail_closed_invariants": [],
    "stdlib_dependencies": [],
    "observation_mode": "EMPIRICAL_BOUNDARY_CAPTURE",
    "zero_external_deps": true
  }
}
