{
  "schema_version": "1.3.0",
  "slug": "which-invariants-prevent-token-accounting-drift-in-opentelemetry-genai-agent-spans-when-tool-call-subtrees-are-re-emitted-after-retry",
  "title": "Which invariants prevent token-accounting drift in OpenTelemetry GenAI agent spans when tool-call subtrees are re-emitted after retry",
  "date_of_record": "2026 November 30",
  "scope": "Applies to agent runtimes that emit OpenTelemetry GenAI spans (`gen_ai.*` semantic conventions) for LLM calls and tool invocations, where a retry, resume, or checkpoint-replay causes a previously-emitted span subtree to be re-emitted under a new parent. Does not cover single-shot, non-retried inference calls, nor transport-level retries that are deduplicated before span creation.",
  "projections": {
    "unified_markdown_uri": "https://corpusfetch.com/c/which-invariants-prevent-token-accounting-drift-in-opentelemetry-genai-agent-spans-when-tool-call-subtrees-are-re-emitted-after-retry.md",
    "method_python_slice_uri": "https://corpusfetch.com/c/which-invariants-prevent-token-accounting-drift-in-opentelemetry-genai-agent-spans-when-tool-call-subtrees-are-re-emitted-after-retry.py",
    "remnant_warrant_slice_uri": "https://corpusfetch.com/c/which-invariants-prevent-token-accounting-drift-in-opentelemetry-genai-agent-spans-when-tool-call-subtrees-are-re-emitted-after-retry.warrant.json"
  },
  "token_profile": {
    "full_markdown_tokens": 1830,
    "python_slice_tokens": 469,
    "warrant_slice_tokens": 220
  },
  "remnant_summary": {
    "core_distinction": "The GenAI semantic conventions treat `gen_ai.usage.input_tokens` and `gen_ai.usage.output_tokens` as **per-span, per-attempt facts**, while downstream cost aggregators, budget enforcers, and rate governors treat them as **per-logical-operation facts**. These two readings diverge the moment a span subtree is re-emitted. The convention does not specify a recon...",
    "what_was_observed": "An agent executing a three-tool plan (`search` → `fetch` → `summarize`) hit a `DeadlineExceeded` on the `fetch` tool span at T+42s. The orchestrator's retry policy re-entered the step, creating a new child span `fetch` (new `span_id`, same `gen_ai.operation.name=\"execute_tool\"`, same `gen_ai.tool.name=\"fetch\"`) parented to the *same* agent span. The original `fetch` span was ended with `status.code=ERROR` but retaine...",
    "condition_of_validity": "The exactly-once token invariant holds only when at least one of the following is true for every re-emitted span: 1. The re-emitted span carries a stable `gen_ai.agent.step_id` (or equivalent custom attribute) that is identical across attempts, and the aggregator deduplicates on `(trace_id, step_id)` selecting the **terminal** attempt (the one whose `status....",
    "decision_boundary": "The boundary is a single question the runtime must answer at span-emission time: > *Is the current attempt a re-execution of a logical step that has already emitted usage, and does the aggregator have a key to distinguish attempts from distinct steps?* If **yes to re-execution and no to a distinguishing key**, the runtime MUST either (a) suppress `gen_ai.usa...",
    "operational_effect": "The consequence is not merely inflated dashboards. Because agents increasingly read their own telemetry to make budget and termination decisions, token-accounting drift becomes a **control-loop input error**. The agent's belief about remaining budget is a function of its own span emissions; if re-emission inflates that belief negatively, the agent aborts val..."
  },
  "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": "30f7e9ece9e6dd1b84762368cb7feefeaa76272e761f8d9e329e1e5a0ba263c4",
    "verified_classes": [
      "TokenAccountingInvariantError",
      "GenAISpanEmission",
      "EmissionLedger"
    ],
    "verified_functions": [
      "aggregate_input_tokens",
      "record"
    ],
    "verified_functions_count": 2,
    "defensive_guards_count": 3,
    "raised_exceptions": [
      "TokenAccountingInvariantError"
    ],
    "sample_fail_closed_invariants": [],
    "stdlib_dependencies": [
      "dataclasses",
      "typing"
    ],
    "observation_mode": "EMPIRICAL_BOUNDARY_CAPTURE",
    "zero_external_deps": true
  }
}
