{
  "schema_version": "1.3.0",
  "slug": "when-must-an-agent-span-close-its-tool-call-child-before-emitting-token-usage",
  "title": "When must an agent span close its tool-call child before emitting token usage?",
  "date_of_record": "2026 November 6",
  "scope": "Applies to OpenTelemetry GenAI instrumentations that emit `gen_ai.*` attributes on agent spans (`gen_ai.operation.name = \"invoke_agent\"`) with nested tool-execution spans (`gen_ai.operation.name = \"execute_tool\"`), where token usage is attributed per-span. Does not apply to single-shot LLM client spans without tool nesting, nor to batch/offline exporters that aggregate usage post-hoc.",
  "projections": {
    "unified_markdown_uri": "https://corpusfetch.com/c/when-must-an-agent-span-close-its-tool-call-child-before-emitting-token-usage.md",
    "method_python_slice_uri": "https://corpusfetch.com/c/when-must-an-agent-span-close-its-tool-call-child-before-emitting-token-usage.py",
    "remnant_warrant_slice_uri": "https://corpusfetch.com/c/when-must-an-agent-span-close-its-tool-call-child-before-emitting-token-usage.warrant.json"
  },
  "token_profile": {
    "full_markdown_tokens": 1220,
    "python_slice_tokens": 348,
    "warrant_slice_tokens": 220
  },
  "remnant_summary": {
    "core_distinction": "The GenAI semantic conventions define token usage attributes (`gen_ai.usage.input_tokens`, `gen_ai.usage.output_tokens`) as *per-model-invocation* counters, but agent spans aggregate multiple model invocations across a reasoning loop. The non-obvious invariant: **a tool-call child span must be ended before the parent agent span records the token usage of the...",
    "what_was_observed": "During a multi-turn agent run (LangGraph-style loop, `invoke_agent` root, N `execute_tool` children), the instrumentation emitted `gen_ai.usage.output_tokens` on the agent span at the moment the model response was parsed — *before* the tool child span was created and executed. The tool span then opened and closed. A downstream usage-attribution pipeline (span-containment based) attributed the parent's output tokens t...",
    "condition_of_validity": "The invariant holds only when: (1) the agent span is the token-usage carrier (per-turn usage is not emitted on a child `chat` span); (2) the collector or post-processor attributes usage by span temporal containment rather than by explicit `trace_id`/`span_id` linkage; and (3) the tool span and the model-turn usage share the same parent. When usage is emitted...",
    "decision_boundary": "The correct sequence for a single reasoning turn is: open `chat`/model span → emit input tokens → receive response → **close model span (emit output tokens here)** → if response contains a tool call, open `execute_tool` span → execute → close tool span → loop. The agent span itself should carry *aggregate* usage only, emitted at its own close, never per-turn...",
    "operational_effect": "Violating the invariant does not fail traces; it corrupts derived cost attribution by one turn and, in retry scenarios, can double-count. The fix is structural: either (a) move usage to a dedicated model child span, or (b) enforce that the usage-emission call and tool-span creation are adjacent and ordered (usage first, tool second) with no intervening await..."
  },
  "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": "51ce199fed5b3424abefab700633dcc6af6bb3992aead12d52b96bea43b01169",
    "verified_classes": [
      "SpanOrderingViolation",
      "SpanRecord"
    ],
    "verified_functions": [
      "validate_agent_span_ordering",
      "attribute_usage"
    ],
    "verified_functions_count": 2,
    "defensive_guards_count": 2,
    "raised_exceptions": [
      "SpanOrderingViolation"
    ],
    "sample_fail_closed_invariants": [],
    "stdlib_dependencies": [
      "__future__",
      "dataclasses",
      "typing"
    ],
    "observation_mode": "EMPIRICAL_BOUNDARY_CAPTURE",
    "zero_external_deps": true
  }
}
