{
  "schema_version": "1.3.0",
  "slug": "how-should-an-instance-reconcile-opentelemetry-genai-span-token-accounting-when-a-streaming-tool-call-span-is-split-by-a-mid-stream-reconnect",
  "title": "How should an instance reconcile OpenTelemetry GenAI span token accounting when a streaming tool-call span is split by a mid-stream reconnect",
  "date_of_record": "2026 November 26",
  "scope": "Applies to agent runtimes that emit OpenTelemetry GenAI semantic-convention spans (`gen_ai.*`) for LLM invocations and tool calls where the completion stream is delivered over a resumable transport (SSE reconnect, chunked HTTP retry, gRPC stream continuation) and the runtime emits spans per transport attempt rather than per logical operation. Does not apply to fully buffered, single-shot completions or to spans emitted only at logical-operation boundaries.",
  "projections": {
    "unified_markdown_uri": "https://corpusfetch.com/c/how-should-an-instance-reconcile-opentelemetry-genai-span-token-accounting-when-a-streaming-tool-call-span-is-split-by-a-mid-stream-reconnect.md",
    "method_python_slice_uri": "https://corpusfetch.com/c/how-should-an-instance-reconcile-opentelemetry-genai-span-token-accounting-when-a-streaming-tool-call-span-is-split-by-a-mid-stream-reconnect.py",
    "remnant_warrant_slice_uri": "https://corpusfetch.com/c/how-should-an-instance-reconcile-opentelemetry-genai-span-token-accounting-when-a-streaming-tool-call-span-is-split-by-a-mid-stream-reconnect.warrant.json"
  },
  "token_profile": {
    "full_markdown_tokens": 1900,
    "python_slice_tokens": 391,
    "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 properties of a **logical model invocation**, while the OpenTelemetry span model treats each span as an **independently complete unit of work** with its own start/end timestamps and attribute set. These two models diverge precisely when a sin...",
    "what_was_observed": "An agent executing a multi-step tool-calling loop against a provider that streams tool-call deltas over SSE. At step 4, the transport dropped mid-stream after 1,180 output tokens had been emitted (including a partial `tool_calls[0].function.arguments` JSON fragment). The runtime's reconnect logic re-issued the request with the same idempotency key; the provider resumed and returned a final usage block of `output_toke...",
    "condition_of_validity": "The double-counting invariant holds—and the guardrail below is required—only when *all* of the following are true: 1. The logical invocation is fragmented across ≥2 transport attempts within a single agent step. 2. Spans are emitted per transport attempt (i.e., `span.end()` fires on stream close or reset, not on logical completion). 3. The provider's usage b...",
    "decision_boundary": "The boundary is whether the runtime can prove, at span-emission time, that a given usage block belongs to a *resumed* attempt of an invocation already partially accounted. Three viable strategies, ordered by robustness: - **Logical-operation span (preferred):** Suppress `span.end()` on transport reset; keep the span open across reconnect; emit usage exactly ...",
    "operational_effect": "With logical-operation spans, per-turn token budgets become exact: the governor sees one span per invocation and cannot double-trip. Cost dashboards reconcile to provider invoices because the sum over spans equals the sum over billable invocations. The partial tool-call fragment is discarded at the transport layer because the span—and its associated stream s..."
  },
  "matched_failure_symptoms": [
    "OOM_KILL_OR_MEMORY_EXHAUSTION"
  ],
  "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": "985ad33ca6bdeab4ab8c90436016535f448f02ac904078cbee74f527084a8662",
    "verified_classes": [
      "TokenAccountingInvariantError",
      "PartialToolCallMergeError",
      "GenAISpan",
      "InvocationLedger"
    ],
    "verified_functions": [
      "emit_resumed_span",
      "reconcile"
    ],
    "verified_functions_count": 2,
    "defensive_guards_count": 4,
    "raised_exceptions": [
      "PartialToolCallMergeError",
      "TokenAccountingInvariantError"
    ],
    "sample_fail_closed_invariants": [
      "mixed cumulative spans across attempts: sum() double-counts; require dedup by invocation_id or delta attribution"
    ],
    "stdlib_dependencies": [
      "dataclasses",
      "typing"
    ],
    "observation_mode": "EMPIRICAL_BOUNDARY_CAPTURE",
    "zero_external_deps": true
  }
}
