{
  "schema_version": "1.3.0",
  "slug": "where-must-an-instance-of-a-genai-agent-span-end-before-the-parent-llm-span-s-token-accounting-closes-the-tool-call-re-entry-boundary",
  "title": "Where must an instance of a GenAI agent span end before the parent LLM span's token accounting closes: the tool-call re-entry boundary",
  "date_of_record": "2026 November 11",
  "scope": "Applies to OpenTelemetry GenAI semantic conventions (`gen_ai.*`) when an agent span (`gen_ai.operation.name = \"invoke_agent\"`) emits tool calls (`gen_ai.operation.name = \"execute_tool\"`) that re-enter the model, and token usage is attributed across nested LLM spans. Does not apply to single-shot completion traces with no tool loop, nor to providers that report usage only at the top-level agent span.",
  "projections": {
    "unified_markdown_uri": "https://corpusfetch.com/c/where-must-an-instance-of-a-genai-agent-span-end-before-the-parent-llm-span-s-token-accounting-closes-the-tool-call-re-entry-boundary.md",
    "method_python_slice_uri": "https://corpusfetch.com/c/where-must-an-instance-of-a-genai-agent-span-end-before-the-parent-llm-span-s-token-accounting-closes-the-tool-call-re-entry-boundary.py",
    "remnant_warrant_slice_uri": "https://corpusfetch.com/c/where-must-an-instance-of-a-genai-agent-span-end-before-the-parent-llm-span-s-token-accounting-closes-the-tool-call-re-entry-boundary.warrant.json"
  },
  "token_profile": {
    "full_markdown_tokens": 1459,
    "python_slice_tokens": 382,
    "warrant_slice_tokens": 220
  },
  "remnant_summary": {
    "core_distinction": "The non-obvious boundary is between **span lifetime** and **accounting lifetime**. A GenAI agent span can remain open while its child `execute_tool` span completes, and the model is re-invoked inside a *new* child LLM span. The parent LLM span that triggered the tool call has already reported its `gen_ai.usage.input_tokens` and `gen_ai.usage.output_tokens`. ...",
    "what_was_observed": "In a multi-turn tool loop (agent → LLM → tool → LLM → tool → LLM), traces exported with `BatchSpanProcessor` showed: - The first LLM child span carried correct `input_tokens`/`output_tokens`. - The second and third LLM child spans carried `gen_ai.usage.input_tokens = 0` when the SDK's span-scoped usage accumulator was keyed on `trace_id` rather than `span_id`, causing the second invocation to overwrite the first's co...",
    "condition_of_validity": "This boundary holds only when: 1. The instrumentation emits one LLM span per model invocation (not one per agent turn). 2. `gen_ai.usage.*` is set on the LLM span at close, not inherited from a context variable shared across the loop. 3. The agent span does **not** set `gen_ai.usage.*` unless it is explicitly a rollup with a distinct attribute namespace (e.g...",
    "decision_boundary": "Choose between two accounting models at instrumentation time: - **Per-invocation model (recommended):** every LLM call is its own span with its own `gen_ai.usage.*`; the agent span is a structural parent with no usage attributes. Backend sums across `chat` spans filtered by `gen_ai.agent.name`. This survives tool loops, retries, and cancellation. - **Rollup ...",
    "operational_effect": "Misattribution here produces silent 2×–3× token cost inflation in billing dashboards, breaks per-tool cost attribution (you cannot answer \"which tool call was expensive\"), and corrupts latency-vs-token regressions. It also defeats `gen_ai.tool.call.id`-based joins, so a cancelled tool call appears to have consumed zero tokens while its re-entrant LLM span co..."
  },
  "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": "649c8b852e79dc1949d1aa2bd47dff96542eabdbc1762e8d8acfa1123dc84127",
    "verified_classes": [
      "GenAIAccountingBoundaryError",
      "SpanRecord",
      "AgentLoop"
    ],
    "verified_functions": [
      "open_llm_span",
      "close_with_usage",
      "open_tool_span",
      "agent_rollup"
    ],
    "verified_functions_count": 4,
    "defensive_guards_count": 6,
    "raised_exceptions": [
      "GenAIAccountingBoundaryError"
    ],
    "sample_fail_closed_invariants": [
      "cannot emit agent rollup: LLM spans still open, loop has not terminated"
    ],
    "stdlib_dependencies": [
      "dataclasses"
    ],
    "observation_mode": "EMPIRICAL_BOUNDARY_CAPTURE",
    "zero_external_deps": true
  }
}
