{
  "schema_version": "1.3.0",
  "slug": "which-invariants-prevent-silent-token-accounting-corruption-in-opentelemetry-genai-agent-spans-when-tool-calls-re-enter-the-model-loop",
  "title": "Which invariants prevent silent token-accounting corruption in OpenTelemetry GenAI agent spans when tool calls re-enter the model loop",
  "date_of_record": "2026 November 13",
  "scope": "Applies to OpenTelemetry GenAI semantic conventions (`gen_ai.*`) as emitted by agent runtimes that interleave LLM invocations with tool executions inside a single logical agent span. Does not apply to single-shot chat completions with no tool round-trips, nor to vendor-specific proprietary tracing that does not claim OTel GenAI conformance.",
  "projections": {
    "unified_markdown_uri": "https://corpusfetch.com/c/which-invariants-prevent-silent-token-accounting-corruption-in-opentelemetry-genai-agent-spans-when-tool-calls-re-enter-the-model-loop.md",
    "method_python_slice_uri": "https://corpusfetch.com/c/which-invariants-prevent-silent-token-accounting-corruption-in-opentelemetry-genai-agent-spans-when-tool-calls-re-enter-the-model-loop.py",
    "remnant_warrant_slice_uri": "https://corpusfetch.com/c/which-invariants-prevent-silent-token-accounting-corruption-in-opentelemetry-genai-agent-spans-when-tool-calls-re-enter-the-model-loop.warrant.json"
  },
  "token_profile": {
    "full_markdown_tokens": 1530,
    "python_slice_tokens": 292,
    "warrant_slice_tokens": 220
  },
  "remnant_summary": {
    "core_distinction": "The GenAI semantic conventions distinguish between an **agent span** (a logical unit of autonomous work, typically `invoke_agent`) and the **model-invocation spans** (`chat` / `text_completion`) nested beneath it. Token usage attributes (`gen_ai.usage.input_tokens`, `gen_ai.usage.output_tokens`) are defined on the *model invocation*, not on the agent. The ag...",
    "what_was_observed": "A production agent runtime emitted a single `chat` span per user turn and mutated `gen_ai.usage.output_tokens` in place as each tool result triggered a follow-up completion, because the instrumentation wrapped the whole ReAct loop in one span. Two consequences surfaced downstream: 1. **Backends that deduplicate by `span_id`** (most OTLP collectors with `deltatocumulative` or any span-level upsert) kept only the final...",
    "condition_of_validity": "The partition invariant holds only when all of the following are true: - Each provider round-trip (one request/response pair to the model API) maps to exactly one span with a stable `span_id` assigned at span start and never reused. - Tool executions are represented either as child spans or as `gen_ai.tool.call` events, each carrying a unique `gen_ai.tool.ca...",
    "decision_boundary": "Instrumentors must choose one of two consistent postures and must not mix them: - **Per-round-trip spans (conformant):** emit one `chat` span per model call, one tool span/event per tool execution, no usage attributes on the agent span. Backends sum descendants. This is the only posture that survives both deduplicating and append-only collectors. - **Single ...",
    "operational_effect": "Conformant instrumentation yields token totals that are stable under collector re-processing, replay, and sampling changes — because each span is immutable and the sum is associative. Non-conformant instrumentation yields totals that drift with collector configuration, which means cost dashboards, budget alarms, and rate-limit accounting all become configura..."
  },
  "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": "90224f2b722ec4356b41bdff03d16e9d62b48075a60538947c52dcac8dbe8abb",
    "verified_classes": [
      "TokenAccountingInvariantError",
      "RoundTrip",
      "AgentSpan"
    ],
    "verified_functions": [
      "reconcile",
      "record_round_trip",
      "assert_conformant",
      "total_tokens"
    ],
    "verified_functions_count": 4,
    "defensive_guards_count": 5,
    "raised_exceptions": [
      "TokenAccountingInvariantError"
    ],
    "sample_fail_closed_invariants": [
      "agent span carries gen_ai.usage.* attributes; usage must live only on child chat spans to keep descendant-summing correc"
    ],
    "stdlib_dependencies": [
      "__future__",
      "dataclasses"
    ],
    "observation_mode": "EMPIRICAL_BOUNDARY_CAPTURE",
    "zero_external_deps": true
  }
}
