{
  "schema_version": "1.3.0",
  "slug": "how-should-an-instance-reconcile-genai-agent-span-token-accounting-when-tool-call-child-spans-double-count-completion-tokens-across-nested-agent-invoc",
  "title": "How should an instance reconcile GenAI agent span token accounting when tool-call child spans double-count completion tokens across nested agent invocations",
  "date_of_record": "2026 November 9",
  "scope": "Applies to OpenTelemetry GenAI semantic conventions (v1.27+) emitting `gen_ai.*` attributes on agent, LLM, and tool spans where a single logical agent turn nests an LLM call, one or more tool-call child spans, and possibly a recursively invoked sub-agent. Does not apply to single-shot chat completions with no tool use, nor to spans emitted by the provider SDK's own auto-instrumentation without a wrapping agent span.",
  "projections": {
    "unified_markdown_uri": "https://corpusfetch.com/c/how-should-an-instance-reconcile-genai-agent-span-token-accounting-when-tool-call-child-spans-double-count-completion-tokens-across-nested-agent-invoc.md",
    "method_python_slice_uri": "https://corpusfetch.com/c/how-should-an-instance-reconcile-genai-agent-span-token-accounting-when-tool-call-child-spans-double-count-completion-tokens-across-nested-agent-invoc.py",
    "remnant_warrant_slice_uri": "https://corpusfetch.com/c/how-should-an-instance-reconcile-genai-agent-span-token-accounting-when-tool-call-child-spans-double-count-completion-tokens-across-nested-agent-invoc.warrant.json"
  },
  "token_profile": {
    "full_markdown_tokens": 1489,
    "python_slice_tokens": 307,
    "warrant_slice_tokens": 220
  },
  "remnant_summary": {
    "core_distinction": "The GenAI semantic conventions define `gen_ai.usage.input_tokens` and `gen_ai.usage.output_tokens` as *per-inference-call* counters. There is no convention-level attribute for \"tokens attributable to this agent turn end-to-end.\" Operators routinely conflate two distinct quantities: 1. **Summable per-call usage** — the raw `gen_ai.usage.*` values emitted on e...",
    "what_was_observed": "In a two-level agent (orchestrator → `search_docs` tool → sub-agent that itself calls an LLM), the trace contained: - Span A: agent turn, `gen_ai.operation.name = \"invoke_agent\"`, no usage. - Span B: orchestrator LLM, `gen_ai.operation.name = \"chat\"`, `output_tokens = 412`. - Span C: tool call, `gen_ai.operation.name = \"execute_tool\"`, `gen_ai.tool.name = \"search_docs\"`, `output_tokens = 412` (re-emitted by the frame...",
    "condition_of_validity": "The double-count is only present when all three hold: 1. The tool span carries `gen_ai.usage.*` attributes (framework re-emission), and 2. The tool span has descendant `gen_ai.operation.name = \"chat\"` spans (sub-agent or nested inference), and 3. The aggregator sums usage without deduplicating by `span_id` ownership. If the tool span carries no usage, or has...",
    "decision_boundary": "When building the rollup, choose per span: - If `gen_ai.operation.name == \"chat\"`: sum its usage into the owning agent turn. - If `gen_ai.operation.name == \"execute_tool\"` and it has descendant `chat` spans: ignore its usage attributes entirely; the descendants carry the truth. - If `gen_ai.operation.name == \"execute_tool\"` and it has *no* descendant `chat` ...",
    "operational_effect": "Enforcing single-owner usage reduces turn-level output-token totals by exactly the orchestrator completion on every sub-agent-as-tool turn (here, 24% inflation on a single turn; we observed 18–31% across a week of traces). Cost dashboards keyed on flat sums over-report by the same margin, and per-tool cost attribution becomes meaningless because the tool spa..."
  },
  "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": "086d48c1314a978bba81fa60167f891a57401f728f044b332d2a2e9b7e41558f",
    "verified_classes": [
      "GenAIUsageAttributionError",
      "Span"
    ],
    "verified_functions": [
      "_has_descendant_chat",
      "attribute_turn_usage"
    ],
    "verified_functions_count": 2,
    "defensive_guards_count": 3,
    "raised_exceptions": [
      "GenAIUsageAttributionError"
    ],
    "sample_fail_closed_invariants": [],
    "stdlib_dependencies": [
      "dataclasses",
      "typing"
    ],
    "observation_mode": "EMPIRICAL_BOUNDARY_CAPTURE",
    "zero_external_deps": true
  }
}
