{
  "schema_version": "1.3.0",
  "slug": "which-invariants-prevent-opentelemetry-genai-span-accounting-from-silently-diverging-when-an-agent-retries-a-tool-call-after-a-partial-token-stream",
  "title": "Which invariants prevent OpenTelemetry GenAI span accounting from silently diverging when an agent retries a tool call after a partial-token stream",
  "date_of_record": "2026 December 2",
  "scope": "Applies to agents instrumented with OpenTelemetry GenAI semantic conventions (`gen_ai.*`) that emit spans per LLM invocation and per tool call, where a single logical tool invocation may be retried, resumed, or replayed across spans. Does not apply to single-shot, non-retrying LLM calls whose span closes exactly once per provider response.",
  "projections": {
    "unified_markdown_uri": "https://corpusfetch.com/c/which-invariants-prevent-opentelemetry-genai-span-accounting-from-silently-diverging-when-an-agent-retries-a-tool-call-after-a-partial-token-stream.md",
    "method_python_slice_uri": "https://corpusfetch.com/c/which-invariants-prevent-opentelemetry-genai-span-accounting-from-silently-diverging-when-an-agent-retries-a-tool-call-after-a-partial-token-stream.py",
    "remnant_warrant_slice_uri": "https://corpusfetch.com/c/which-invariants-prevent-opentelemetry-genai-span-accounting-from-silently-diverging-when-an-agent-retries-a-tool-call-after-a-partial-token-stream.warrant.json"
  },
  "token_profile": {
    "full_markdown_tokens": 1701,
    "python_slice_tokens": 361,
    "warrant_slice_tokens": 220
  },
  "remnant_summary": {
    "core_distinction": "The OpenTelemetry GenAI conventions describe *what to record on a span* — `gen_ai.usage.input_tokens`, `gen_ai.usage.output_tokens`, `gen_ai.operation.name`, `gen_ai.tool.name`, `gen_ai.tool.call.id`. They do **not** describe *how a retried or resumed execution maps onto span cardinality*. The failure boundary is not \"wrong attribute value\" — it is **span mu...",
    "what_was_observed": "A retrieval-augmented agent issued a `search` tool call whose argument was a large JSON object streamed as tokens. At chunk 340 of an expected ~700, the transport reset. The agent's retry wrapper re-invoked the model with the same prompt. The model, being non-deterministic, produced a *slightly different* argument this time and a *different* `gen_ai.tool.call.id` because the SDK generated the id client-side per attem...",
    "condition_of_validity": "The invariant that must hold for any aggregate token or tool-call metric to be meaningful is: > **For every logical agent action, exactly one span carries `gen_ai.usage.*` accounting for the provider-side cost, and every retry attempt is a child span that carries attempt-local usage but is excluded from logical-call aggregation by an explicit parent referenc...",
    "decision_boundary": "The boundary is between **attempt-scoped accounting** (what the conventions natively support) and **action-scoped accounting** (what cost reconciliation, idempotency, and audit require). Crossing it requires the instrumentation layer — not the semantic conventions — to enforce: - **On retry:** open a child span under the logical call span; do not re-mint `ge...",
    "operational_effect": "With the invariant enforced, the token ledger and the invoice reconcile to within the provider's own retry accounting, and non-idempotent tools are executed once per logical call. Without it, three failures compound silently: 1. **Token overcount** proportional to retry rate — typically 3–15% in agents with aggressive timeouts, invisible because every span v..."
  },
  "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": "4a58d2760a60955c8dafd158783b77100ae7eeffe7b2754c83a12dd18a0dcd77",
    "verified_classes": [
      "GenAISpanAccountingError"
    ],
    "verified_functions": [
      "close_tool_span",
      "reconcile"
    ],
    "verified_functions_count": 2,
    "defensive_guards_count": 7,
    "raised_exceptions": [
      "GenAISpanAccountingError"
    ],
    "sample_fail_closed_invariants": [
      "tool span missing gen_ai.tool.call.id",
      "resumed stream reported cumulative usage on a retry span; would mix delta and cumulative semantics under gen_ai.usage.*",
      "retry span missing retry.attempt; aggregation cannot exclude it"
    ],
    "stdlib_dependencies": [],
    "observation_mode": "EMPIRICAL_BOUNDARY_CAPTURE",
    "zero_external_deps": true
  }
}
