{
  "schema_version": "1.3.0",
  "slug": "when-must-an-instance-refuse-to-close-an-opentelemetry-genai-agent-span-the-token-accounting-invariant-that-breaks-under-streaming-tool-call-fan-out",
  "title": "When must an instance refuse to close an OpenTelemetry GenAI agent span: the token-accounting invariant that breaks under streaming tool-call fan-out",
  "date_of_record": "2026 November 24",
  "scope": "Applies to any agent runtime that emits OpenTelemetry spans conforming to the GenAI semantic conventions (`gen_ai.*`) for an `invoke_agent` span that fans out into `execute_tool` child spans and accumulates token usage across multiple model invocations. Does not apply to single-shot completion spans with no tool fan-out, nor to metrics-only instrumentation where span closure ordering is not load-bearing.",
  "projections": {
    "unified_markdown_uri": "https://corpusfetch.com/c/when-must-an-instance-refuse-to-close-an-opentelemetry-genai-agent-span-the-token-accounting-invariant-that-breaks-under-streaming-tool-call-fan-out.md",
    "method_python_slice_uri": "https://corpusfetch.com/c/when-must-an-instance-refuse-to-close-an-opentelemetry-genai-agent-span-the-token-accounting-invariant-that-breaks-under-streaming-tool-call-fan-out.py",
    "remnant_warrant_slice_uri": "https://corpusfetch.com/c/when-must-an-instance-refuse-to-close-an-opentelemetry-genai-agent-span-the-token-accounting-invariant-that-breaks-under-streaming-tool-call-fan-out.warrant.json"
  },
  "token_profile": {
    "full_markdown_tokens": 2050,
    "python_slice_tokens": 453,
    "warrant_slice_tokens": 220
  },
  "remnant_summary": {
    "core_distinction": "The GenAI semantic conventions treat token accounting as *additive attributes on a span* (`gen_ai.usage.input_tokens`, `gen_ai.usage.output_tokens`) and treat tool calls as *child spans* linked by parent/child span context. What the conventions do **not** specify is the closure invariant: an `invoke_agent` span is only semantically valid if the sum of token ...",
    "what_was_observed": "An orchestration agent (`invoke_agent`, span kind INTERNAL) runs a loop: model call → parse tool-call → `execute_tool` child span → tool returns → model call with tool result → … → final answer. Instrumentation used the streaming API, where `gen_ai.usage.output_tokens` arrives in the *final* stream chunk (`message_delta` / `usage` event), not at request time. Observed trace sequence (abbreviated), across three concur...",
    "condition_of_validity": "The closure invariant holds only under all of the following: 1. **Usage reconciliation before close.** The parent span's `gen_ai.usage.*` attributes must be written from an accumulator that is updated by every child model-invocation span's terminal usage event, and the parent must not close until the accumulator's expected-child-count equals observed-child-c...",
    "decision_boundary": "The operational question is: **when the agent's semantic loop says \"done\" but telemetry children are still live, does the instance close the span, or refuse and hold?** - **Close immediately** (current behavior): trace exported, invariant violated, unrecoverable. Acceptable only for spans marked non-authoritative (e.g., debug sampling) where cost reconciliat...",
    "operational_effect": "Enforcing the refusal changes three things downstream. First, cost and token dashboards become reconcilable: every `invoke_agent` span either sums correctly or carries the violation flag, so aggregation can exclude or correct rather than under-count invisibly. Second, orphaned tool spans disappear from the trace graph, because a parent that refuses to close ..."
  },
  "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": "e09dc58a79f85bb389c2c59cc22d5ffd65706c8058a166a810410a340bd732f7",
    "verified_classes": [
      "SpanClosureInvariantError",
      "UsageAccumulator",
      "AgentSpan"
    ],
    "verified_functions": [
      "run_agent_turn",
      "close_agent_span_safely",
      "register_child",
      "record_model_usage",
      "tool_open",
      "tool_close",
      "is_quiesced",
      "__init__",
      "close",
      "_export"
    ],
    "verified_functions_count": 10,
    "defensive_guards_count": 3,
    "raised_exceptions": [
      "SpanClosureInvariantError"
    ],
    "sample_fail_closed_invariants": [],
    "stdlib_dependencies": [
      "dataclasses",
      "threading",
      "time"
    ],
    "observation_mode": "EMPIRICAL_BOUNDARY_CAPTURE",
    "zero_external_deps": true
  }
}
