{
  "schema_version": "1.3.0",
  "slug": "where-must-an-instance-reconcile-opentelemetry-genai-span-closure-against-token-accounting-when-a-tool-call-span-is-abandoned-mid-stream",
  "title": "Where must an instance reconcile OpenTelemetry GenAI span closure against token accounting when a tool-call span is abandoned mid-stream?",
  "date_of_record": "2026 November 29",
  "scope": "Applies to any agent runtime that emits OpenTelemetry spans conforming to the GenAI semantic conventions (`gen_ai.*` attributes) for LLM inference calls and tool executions, where the inference span is closed by a streaming response handler and the tool span is a child of the inference span. Does not apply to batch, non-streaming inference where usage is known synchronously before span end, nor to pure client-side instrumentation that never owns span lifecycle.",
  "projections": {
    "unified_markdown_uri": "https://corpusfetch.com/c/where-must-an-instance-reconcile-opentelemetry-genai-span-closure-against-token-accounting-when-a-tool-call-span-is-abandoned-mid-stream.md",
    "method_python_slice_uri": "https://corpusfetch.com/c/where-must-an-instance-reconcile-opentelemetry-genai-span-closure-against-token-accounting-when-a-tool-call-span-is-abandoned-mid-stream.py",
    "remnant_warrant_slice_uri": "https://corpusfetch.com/c/where-must-an-instance-reconcile-opentelemetry-genai-span-closure-against-token-accounting-when-a-tool-call-span-is-abandoned-mid-stream.warrant.json"
  },
  "token_profile": {
    "full_markdown_tokens": 1653,
    "python_slice_tokens": 291,
    "warrant_slice_tokens": 220
  },
  "remnant_summary": {
    "core_distinction": "The GenAI semantic conventions treat two things as separable that the streaming runtime treats as one: the **span lifecycle** (start/end timestamps, parent-child linkage, status) and the **usage record** (`gen_ai.usage.input_tokens`, `gen_ai.usage.output_tokens`, and the derived cost attributes). In a non-streaming call these are emitted together at one inst...",
    "what_was_observed": "A tool-calling agent streaming a response received a `tool_calls` delta for a large argument payload (a 3,400-character JSON body). The runtime began a child span `execute_tool search_index` and dispatched. The upstream tool endpoint stalled; the client-side stream idle timer fired at 30s and the inference stream was closed. The instrumentation, written as a `finally` block around the stream iterator, called `inferen...",
    "condition_of_validity": "The invariant that was violated: **for any inference span with a child tool span, `parent.end_time >= child.end_time` must hold, and `parent.gen_ai.usage.output_tokens` must be either the provider's final cumulative value or explicitly marked incomplete.** This holds trivially in synchronous execution. It is *only* at risk under three co-occurring conditions...",
    "decision_boundary": "The runtime must decide, at the moment the stream is cut, whether the turn is **complete**, **abandoned-with-usage**, or **unknown-usage**, and it must encode that decision in the span rather than letting the `finally` block default to \"complete.\" The boundary: - If the terminal usage chunk was received → parent may close with final usage; children must be a...",
    "operational_effect": "Once the ordering invariant is enforced, two classes of downstream corruption disappear. Cost aggregation becomes monotone: an abandoned turn either contributes its partial usage with an explicit `abandoned` marker (auditable) or contributes nothing with usage attributes absent (distinguishable from zero). Trace-backend containment checks stop dropping child..."
  },
  "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": "38dd664a1f44827e82cb8f7306f90719b665b09824b0c71e7180c0d0683fc7da",
    "verified_classes": [
      "SpanOrderingViolation",
      "GenAISpan"
    ],
    "verified_functions": [
      "close_inference_span",
      "add_child",
      "end"
    ],
    "verified_functions_count": 3,
    "defensive_guards_count": 2,
    "raised_exceptions": [
      "SpanOrderingViolation"
    ],
    "sample_fail_closed_invariants": [],
    "stdlib_dependencies": [
      "dataclasses",
      "time",
      "typing"
    ],
    "observation_mode": "EMPIRICAL_BOUNDARY_CAPTURE",
    "zero_external_deps": true
  }
}
