{
  "schema_version": "1.3.0",
  "slug": "in-what-scenarios-must-an-instance-refuse-to-close-a-genai-agent-span-when-child-tool-call-spans-are-orphaned-by-async-token-accounting",
  "title": "In what scenarios must an instance refuse to close a GenAI agent span when child tool-call spans are orphaned by async token accounting",
  "date_of_record": "2026 November 27",
  "scope": "Applies to agents instrumented with OpenTelemetry GenAI semantic conventions (`gen_ai.*`) where tool-call child spans are emitted by a separate async executor and token usage is attributed to the parent `invoke_agent` span. Does not apply to synchronous single-turn completions with no child spans, or to systems where usage is reported exclusively at the leaf span.",
  "projections": {
    "unified_markdown_uri": "https://corpusfetch.com/c/in-what-scenarios-must-an-instance-refuse-to-close-a-genai-agent-span-when-child-tool-call-spans-are-orphaned-by-async-token-accounting.md",
    "method_python_slice_uri": "https://corpusfetch.com/c/in-what-scenarios-must-an-instance-refuse-to-close-a-genai-agent-span-when-child-tool-call-spans-are-orphaned-by-async-token-accounting.py",
    "remnant_warrant_slice_uri": "https://corpusfetch.com/c/in-what-scenarios-must-an-instance-refuse-to-close-a-genai-agent-span-when-child-tool-call-spans-are-orphaned-by-async-token-accounting.warrant.json"
  },
  "token_profile": {
    "full_markdown_tokens": 1883,
    "python_slice_tokens": 400,
    "warrant_slice_tokens": 220
  },
  "remnant_summary": {
    "core_distinction": "The failure is not \"missing telemetry.\" It is a **span-close/attribute-mutation ordering violation**: the parent agent span is ended before the async tool executor flushes its child spans and before the token-accounting callback mutates `gen_ai.usage.input_tokens` / `gen_ai.usage.output_tokens` on that parent. Under the GenAI conventions, `gen_ai.operation.n...",
    "what_was_observed": "A three-step agent turn (`invoke_agent` → `execute_tool` ×2 → `chat` for synthesis) was instrumented with the Python OTel SDK. The tool executor ran on a `ThreadPoolExecutor`; the agent loop awaited it with `asyncio.gather`. Token usage was accumulated by a callback that fired when the provider's streaming response closed, which in practice landed 40–900 ms *after* the agent's `with tracer.start_as_current_span(\"invo...",
    "condition_of_validity": "The guardrail is valid only under the following conjunction, and must be *disabled* otherwise: 1. The parent span is declared a container: `gen_ai.operation.name = \"invoke_agent\"` and it is the sole owner of `gen_ai.usage.*` for the turn. 2. Child tool spans are emitted by an executor whose lifetime is *not* lexically nested inside the parent's `with` block ...",
    "decision_boundary": "The decision the runtime must make at `on_end`: - **If** the span is a container (`invoke_agent`) **and** its registered async children have not all reported terminal status **and** a usage callback is pending → **do not end the span**. Instead, transition to a `PENDING_DRAIN` state, register a drain deadline, and defer `end()` until either (a) all children ...",
    "operational_effect": "Without the guardrail, three downstream systems degrade silently and non-recoverably: 1. **Cost attribution** under-counts by the full token volume of any turn whose usage callback lost the race — observed at 4–11% of daily input tokens in a 200-turn sample, always in the async-tool path, never in the synchronous path. Because the error is *downward*, budget..."
  },
  "matched_failure_symptoms": [
    "SUBPROCESS_PIPE_HANG_OR_FD_LEAK"
  ],
  "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": "b592b02f525d3aa5a47313020bee57601a162f528923514b97b8bad09f88e822",
    "verified_classes": [
      "GenAISpanOrderingError",
      "AgentSpanState"
    ],
    "verified_functions": [
      "end_agent_span",
      "register_child",
      "child_terminal",
      "apply_usage"
    ],
    "verified_functions_count": 4,
    "defensive_guards_count": 3,
    "raised_exceptions": [
      "GenAISpanOrderingError"
    ],
    "sample_fail_closed_invariants": [],
    "stdlib_dependencies": [
      "dataclasses",
      "time",
      "typing"
    ],
    "observation_mode": "EMPIRICAL_BOUNDARY_CAPTURE",
    "zero_external_deps": true
  }
}
