{
  "schema_version": "1.3.0",
  "slug": "why-does-an-instance-miscount-tokens-when-opentelemetry-genai-span-accounting-diverges-across-a-resumed-agent-turn",
  "title": "Why does an instance miscount tokens when OpenTelemetry GenAI span accounting diverges across a resumed agent turn",
  "date_of_record": "2026 November 29",
  "scope": "Applies to agents that emit OTel `gen_ai.*` spans per LLM call and tool call, and that can be suspended and resumed (checkpoint/restore, human-in-the-loop pause, or worker migration) mid-turn. Does not apply to single-shot, non-resumable invocations where one span tree maps one-to-one to one process lifetime.",
  "projections": {
    "unified_markdown_uri": "https://corpusfetch.com/c/why-does-an-instance-miscount-tokens-when-opentelemetry-genai-span-accounting-diverges-across-a-resumed-agent-turn.md",
    "method_python_slice_uri": "https://corpusfetch.com/c/why-does-an-instance-miscount-tokens-when-opentelemetry-genai-span-accounting-diverges-across-a-resumed-agent-turn.py",
    "remnant_warrant_slice_uri": "https://corpusfetch.com/c/why-does-an-instance-miscount-tokens-when-opentelemetry-genai-span-accounting-diverges-across-a-resumed-agent-turn.warrant.json"
  },
  "token_profile": {
    "full_markdown_tokens": 1953,
    "python_slice_tokens": 434,
    "warrant_slice_tokens": 220
  },
  "remnant_summary": {
    "core_distinction": "There are two different things an OTel GenAI span tree can mean, and they are routinely conflated: 1. **The logical turn** — one user-visible agent action, which may span multiple LLM calls, tool calls, and *process lifetimes*. 2. **The physical span tree** — a parent/child DAG rooted at a trace that is only valid within a single process context that owns th...",
    "what_was_observed": "A three-step agent turn (plan → tool call → summarize) was instrumented per the GenAI conventions: each LLM call gets a `gen_ai.chat` span with `gen_ai.usage.input_tokens` and `gen_ai.usage.output_tokens`; the tool call gets a `gen_ai.tool` span; the turn gets a parent span. The agent was configured to pause after the tool call for a human approval gate, serialize its state, and resume in a fresh worker. The observed...",
    "condition_of_validity": "The miscount is bounded and predictable only under this condition: **the resumed span tree carries a link back to the pre-pause trace.** The GenAI conventions permit this via span links (`SpanLink` to the pre-pause root's `SpanContext`) or via a turn-scoped attribute such as `gen_ai.conversation.id` / a custom `agent.turn.id` that is stable across the checkp...",
    "decision_boundary": "The boundary is whether the resume path treated the checkpoint as a *new turn* or a *continuation of the same turn*: - If the checkpoint boundary is also a **turn boundary** (the agent finished a user-visible action and the resume starts a new request), then opening a new root is *correct*, and per-root summation is *correct*. The bug only appears when the c...",
    "operational_effect": "The operational consequence is that cost attribution, budget enforcement, and rate-limit accounting all read the wrong number, and they read it *confidently*. A token-budget guard that sums root-span `input_tokens` will under-count mid-turn resumes and let a turn exceed its budget by exactly the pre-pause consumption — which is unbounded if the agent resumes..."
  },
  "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": "38683bf96ef78d8cf8786c83f65fafc6a6291984045bbb5c99abcb68e73cab5f",
    "verified_classes": [
      "TurnAccountingError",
      "GenAISpan",
      "TurnLedger"
    ],
    "verified_functions": [
      "emit_resumed_root",
      "add",
      "reconcile_turn"
    ],
    "verified_functions_count": 3,
    "defensive_guards_count": 3,
    "raised_exceptions": [
      "TurnAccountingError"
    ],
    "sample_fail_closed_invariants": [
      "resumed root requires a stable turn_id; without it the pre-pause span tree cannot be joined and token totals will diverg"
    ],
    "stdlib_dependencies": [
      "dataclasses",
      "hashlib",
      "typing"
    ],
    "observation_mode": "EMPIRICAL_BOUNDARY_CAPTURE",
    "zero_external_deps": true
  }
}
