{
  "schema_version": "1.3.0",
  "slug": "why-does-an-instance-of-an-opentelemetry-genai-agent-span-silently-under-report-token-usage-when-a-tool-call-is-retried-after-a-partial-stream-flush",
  "title": "Why does an instance of an OpenTelemetry GenAI agent span silently under-report token usage when a tool call is retried after a partial stream flush?",
  "date_of_record": "2026 November 23",
  "scope": "Applies to agent frameworks emitting OTel GenAI spans (`gen_ai.operation.name = \"invoke_agent\"` / `\"execute_tool\"`) where token usage is accumulated incrementally across streaming model responses and tool-call retries. Does not apply to non-streaming completions, nor to providers returning final usage only in a terminal event with no intermediate `usage` deltas.",
  "projections": {
    "unified_markdown_uri": "https://corpusfetch.com/c/why-does-an-instance-of-an-opentelemetry-genai-agent-span-silently-under-report-token-usage-when-a-tool-call-is-retried-after-a-partial-stream-flush.md",
    "method_python_slice_uri": "https://corpusfetch.com/c/why-does-an-instance-of-an-opentelemetry-genai-agent-span-silently-under-report-token-usage-when-a-tool-call-is-retried-after-a-partial-stream-flush.py",
    "remnant_warrant_slice_uri": "https://corpusfetch.com/c/why-does-an-instance-of-an-opentelemetry-genai-agent-span-silently-under-report-token-usage-when-a-tool-call-is-retried-after-a-partial-stream-flush.warrant.json"
  },
  "token_profile": {
    "full_markdown_tokens": 1227,
    "python_slice_tokens": 136,
    "warrant_slice_tokens": 220
  },
  "remnant_summary": {
    "core_distinction": "GenAI semantic conventions treat `gen_ai.usage.input_tokens` and `gen_ai.usage.output_tokens` as **cumulative attributes of a single logical model invocation**, not per-chunk counters. The boundary failure occurs when an agent retries a tool call and the instrumentation *resets* or *double-counts* the accumulator depending on where the flush boundary landed....",
    "what_was_observed": "In a LangGraph-style agent loop instrumented with a hand-rolled OTel exporter, tool calls to a flaky downstream service were retried up to 3 times. Traces showed `gen_ai.usage.output_tokens` values *lower* than the provider dashboard by ~15–40% on retried turns, and occasionally *higher* when the retry landed after a clean stream close. The discrepancy correlated exactly with whether the aborted attempt had emitted a...",
    "condition_of_validity": "The under/over-reporting only manifests when **all** of the following hold: - The model response is streamed and the provider emits `usage` in intermediate or terminal stream events (Anthropic `message_delta`, OpenAI `stream_options.include_usage`, Gemini `usageMetadata`). - The agent framework retries at the *tool-call* or *model-invocation* layer, not at t...",
    "decision_boundary": "The correct rule: **usage accumulation is keyed to the logical `invoke_agent` span, and each attempt contributes a delta that is added, never assigned.** On retry, do not reset the accumulator; record the retry as a span event (`gen_ai.retry.attempt`) carrying the attempt's own delta, and add it to the span attributes. The decision boundary is whether the re...",
    "operational_effect": "Downstream, this corrupts cost attribution and capacity planning: per-agent token budgets derived from these spans under-provision retry-heavy paths, and chargeback reports disagree with the provider invoice by an amount proportional to retry rate. The mitigation is to emit per-attempt usage as span events and reconcile the span total against the provider's ..."
  },
  "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": "a1463dc5eda5bb8fc7a003c93793a43f061544ce5265b0e8679c953ce6eaa9c9",
    "verified_classes": [
      "TokenAccountingInvariantError"
    ],
    "verified_functions": [
      "reconcile_span_usage"
    ],
    "verified_functions_count": 1,
    "defensive_guards_count": 2,
    "raised_exceptions": [
      "TokenAccountingInvariantError"
    ],
    "sample_fail_closed_invariants": [
      "span missing gen_ai.usage.output_tokens attribute"
    ],
    "stdlib_dependencies": [],
    "observation_mode": "EMPIRICAL_BOUNDARY_CAPTURE",
    "zero_external_deps": true
  }
}
