{
  "schema_version": "1.3.0",
  "slug": "when-must-an-instance-refuse-to-close-a-genai-tool-call-span-the-parent-child-span-timing-invariant-and-its-silent-violation-under-streaming-token-acc",
  "title": "When must an instance refuse to close a GenAI tool-call span: the parent/child span-timing invariant and its silent violation under streaming token accounting",
  "date_of_record": "2026 November 30",
  "scope": "Applies to any agent runtime that emits OpenTelemetry GenAI spans (`gen_ai.operation.name`, `gen_ai.tool.name`, `gen_ai.usage.input_tokens`, `gen_ai.usage.output_tokens`) around LLM calls and their downstream tool executions, particularly when the LLM call streams and the tool call is dispatched before the model span is finalized. Does not apply to batch/offline inference pipelines that never nest tool spans under a model span, nor to pure trace-export-from-proxy setups where span lifecycle is owned by the SDK's auto-instrumentation.",
  "projections": {
    "unified_markdown_uri": "https://corpusfetch.com/c/when-must-an-instance-refuse-to-close-a-genai-tool-call-span-the-parent-child-span-timing-invariant-and-its-silent-violation-under-streaming-token-acc.md",
    "method_python_slice_uri": "https://corpusfetch.com/c/when-must-an-instance-refuse-to-close-a-genai-tool-call-span-the-parent-child-span-timing-invariant-and-its-silent-violation-under-streaming-token-acc.py",
    "remnant_warrant_slice_uri": "https://corpusfetch.com/c/when-must-an-instance-refuse-to-close-a-genai-tool-call-span-the-parent-child-span-timing-invariant-and-its-silent-violation-under-streaming-token-acc.warrant.json"
  },
  "token_profile": {
    "full_markdown_tokens": 2273,
    "python_slice_tokens": 442,
    "warrant_slice_tokens": 220
  },
  "remnant_summary": {
    "core_distinction": "A GenAI tool-call span is not a log line; it is a **temporal containment assertion**. The OpenTelemetry GenAI conventions model a tool call as a `gen_ai.operation.name = \"execute_tool\"` span whose `gen_ai.tool.call.id` ties it to the assistant message that requested it. The implicit invariant is that the tool span's `[start_time, end_time]` interval lies *wi...",
    "what_was_observed": "A single-turn agent (one model call, one tool call, one follow-up model call) produced a trace with three spans. The expected shape: The observed shape, on a run where the SSE stream's terminal `usage` chunk was delayed by 240 ms past the tool's 90 ms execution: Superficially fine. But the exporter received them out of order: the tool span ended at 1030 ms and was exported with `end_time` set, while the model span's ...",
    "condition_of_validity": "The containment invariant holds **only** when all of the following are true: 1. The model call is non-streaming, OR the streaming client buffers the entire response (including the terminal `usage` chunk) before returning control to the agent loop. 2. The tool call is dispatched *after* the model span is finalized (i.e., the agent loop is strictly sequential:...",
    "decision_boundary": "The boundary is: **at what point does the runtime hold the model span open to preserve token attribution, versus close it to preserve latency?** The correct decision is not \"always close early\" or \"always wait.\" It is conditioned on whether the terminal `usage` chunk has been received: - If `usage` has arrived: close the model span immediately upon `finish_r...",
    "operational_effect": "Enforcing the guardrail changes three observable behaviors: 1. **Token reconciliation becomes exact.** Summing `gen_ai.usage.input_tokens` over all `chat` spans in a trace now equals the provider's billed total, because no span is emitted without a usage value (zero-with-error is a value). Dashboards that previously showed a 3–8% shortfall on streaming-heavy..."
  },
  "matched_failure_symptoms": [
    "SUBPROCESS_PIPE_HANG_OR_FD_LEAK",
    "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": "640e601bad074d9e3a317d43c042ed365d6ca77e4f104318f4e52841fb0ac75b",
    "verified_classes": [
      "GenAISpanInvariantError",
      "Span",
      "GuardedSpanProcessor"
    ],
    "verified_functions": [
      "reconcile_token_totals",
      "is_model_span",
      "is_tool_span",
      "has_usage",
      "__init__",
      "on_end",
      "try_export_child",
      "_export"
    ],
    "verified_functions_count": 8,
    "defensive_guards_count": 4,
    "raised_exceptions": [
      "GenAISpanInvariantError"
    ],
    "sample_fail_closed_invariants": [],
    "stdlib_dependencies": [
      "dataclasses",
      "time",
      "typing"
    ],
    "observation_mode": "EMPIRICAL_BOUNDARY_CAPTURE",
    "zero_external_deps": true
  }
}
