{
  "schema_version": "1.3.0",
  "slug": "which-invariants-prevent-token-accounting-drift-from-silently-corrupting-opentelemetry-genai-agent-spans-across-tool-call-boundaries",
  "title": "Which invariants prevent token-accounting drift from silently corrupting OpenTelemetry GenAI agent spans across tool-call boundaries",
  "date_of_record": "2026 November 29",
  "scope": "Applies to any agent runtime that emits OpenTelemetry GenAI semantic-convention spans (`gen_ai.*`) for multi-turn reasoning loops with tool calls, where token usage is aggregated per-span and reconciled against a billing or budget ledger. Does not apply to pure single-shot completion instrumentation with no tool-call fan-out, nor to runtimes that emit only top-level request spans without child tool spans.",
  "projections": {
    "unified_markdown_uri": "https://corpusfetch.com/c/which-invariants-prevent-token-accounting-drift-from-silently-corrupting-opentelemetry-genai-agent-spans-across-tool-call-boundaries.md",
    "method_python_slice_uri": "https://corpusfetch.com/c/which-invariants-prevent-token-accounting-drift-from-silently-corrupting-opentelemetry-genai-agent-spans-across-tool-call-boundaries.py",
    "remnant_warrant_slice_uri": "https://corpusfetch.com/c/which-invariants-prevent-token-accounting-drift-from-silently-corrupting-opentelemetry-genai-agent-spans-across-tool-call-boundaries.warrant.json"
  },
  "token_profile": {
    "full_markdown_tokens": 1644,
    "python_slice_tokens": 263,
    "warrant_slice_tokens": 220
  },
  "remnant_summary": {
    "core_distinction": "The GenAI semantic conventions define token attributes (`gen_ai.usage.input_tokens`, `gen_ai.usage.output_tokens`) as *per-operation* counters attached to the span that represents the model invocation. They do **not** define a reconciliation contract between a parent agent span that fans out into N tool-call child spans and the aggregate the operator believe...",
    "what_was_observed": "A four-step agent loop: plan → `search_docs` tool → synthesize → `write_file` tool → summarize. Each model invocation is instrumented as a child span under a single agent span. The tool executions are instrumented as separate child spans. Three distinct drift mechanisms were observed: 1. **Re-entry double-count.** The model call that *decides* to invoke `search_docs` reports `output_tokens=412` (the reasoning + tool-...",
    "condition_of_validity": "The invariant \"aggregate span usage equals billed usage\" holds **only if** all of the following are true: - Every model invocation emits exactly one span with usage attributes populated *before* span close, including cancelled/truncated streams. - Tool spans are emitted for *every* tool execution attempt, including failed and retried attempts, so no usage-be...",
    "decision_boundary": "The boundary is whether the span tree is treated as a **cost ledger** or a **context trace**. These require different invariants: - As a *context trace*, summing `input_tokens` is meaningless—each value is an independent snapshot of context size. The correct aggregate is `max(input_tokens)` per conversation, not `sum`. - As a *cost ledger*, the correct aggre...",
    "operational_effect": "The practical consequence is that budget guards wired to span aggregates under- or over-fire depending on loop depth. A per-session budget that sums child `input_tokens` will trip early on deep loops (double-counting) and late on cancelled streams (under-counting), so the same nominal budget yields different real spend for different agent topologies. Reconci..."
  },
  "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": "7b3da59a12123a3f4d7282a675219378f68823339ad1f4b5d2a783ea111f600b",
    "verified_classes": [
      "UsageReconciliationError",
      "GenAISpan"
    ],
    "verified_functions": [
      "reconcile_usage"
    ],
    "verified_functions_count": 1,
    "defensive_guards_count": 3,
    "raised_exceptions": [
      "UsageReconciliationError"
    ],
    "sample_fail_closed_invariants": [],
    "stdlib_dependencies": [
      "dataclasses",
      "typing"
    ],
    "observation_mode": "EMPIRICAL_BOUNDARY_CAPTURE",
    "zero_external_deps": true
  }
}
