{
  "schema_version": "1.3.0",
  "slug": "why-does-an-instance-of-gen-ai-usage-output-tokens-disagree-with-the-sum-of-child-llm-spans-under-an-agent-span",
  "title": "Why does an instance of gen_ai.usage.output_tokens disagree with the sum of child LLM spans under an agent span",
  "date_of_record": "2026 November 23",
  "scope": "OpenTelemetry GenAI semantic conventions (v1.27–v1.30) where an agent span (`gen_ai.operation.name = \"invoke_agent\"` / `\"agent\"`) aggregates child LLM and tool spans, and cost telemetry is derived by summing `gen_ai.usage.*`. Excludes single-shot inference spans and vendor extensions with proprietary aggregation.",
  "projections": {
    "unified_markdown_uri": "https://corpusfetch.com/c/why-does-an-instance-of-gen-ai-usage-output-tokens-disagree-with-the-sum-of-child-llm-spans-under-an-agent-span.md",
    "method_python_slice_uri": "https://corpusfetch.com/c/why-does-an-instance-of-gen-ai-usage-output-tokens-disagree-with-the-sum-of-child-llm-spans-under-an-agent-span.py",
    "remnant_warrant_slice_uri": "https://corpusfetch.com/c/why-does-an-instance-of-gen-ai-usage-output-tokens-disagree-with-the-sum-of-child-llm-spans-under-an-agent-span.warrant.json"
  },
  "token_profile": {
    "full_markdown_tokens": 1209,
    "python_slice_tokens": 419,
    "warrant_slice_tokens": 220
  },
  "remnant_summary": {
    "core_distinction": "The boundary is between **span-local** and **trace-aggregate** token accounting. A GenAI span records tokens *it* consumed; an agent span is a *narrative* span, and the convention does not mandate its usage equal the sum of descendants. Three incompatible policies exist: (a) zero usage, (b) sum of direct children, (c) sum of all descendants including retries...",
    "what_was_observed": "Over ~1.2M traces from a LangGraph-style orchestrator: - Agent span emitted `output_tokens = 48,210`; direct child `chat` spans summed to exactly 48,210 (policy b). - A retried child (after 429) emitted a sibling span with 3,102 tokens, excluded from the agent aggregate. Summing all descendants gave 51,312. - A `web_search` tool span carried no usage, but its nested summarizer `chat` span (grandchild of agent) emitte...",
    "condition_of_validity": "The disagreement is well-defined only when: 1. The trace is complete — head sampling makes the leaf sum *lower* than the agent attribute, indistinguishable from policy (a); tail-sampling on error biases toward retry-heavy traces. 2. `gen_ai.usage.*` is emitted on the same span as `gen_ai.operation.name`; event-borne usage is missed by span-keyed correlators....",
    "decision_boundary": "- Agent attribute equals sum of *direct* `chat` children, no retry siblings, no nested tool LLM spans → faithful aggregate; use it. - Any retry sibling or nested tool LLM span → not the billable total; reconstruct from leaves, deduplicating retries by `(trace_id, gen_ai.request.id, attempt)` (the conventions do not standardize a retry counter). - Sampled tra...",
    "operational_effect": "Cost telemetry must carry an explicit **aggregation policy tag** and **completeness flag**; downstream consumers treat the agent attribute as *advisory*. In the observed system, the 21% over-report traced to retry duplication; after switching to leaf reconstruction with retry dedup keyed on `(trace_id, gen_ai.request.id, attempt)`, residual error fell to 0.4..."
  },
  "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": "07d69be2ed49a5325292a73941be1e526cd08cb86f74d2e6736f7d49b9d5168b",
    "verified_classes": [
      "GenAICostReconciliationError",
      "GenAISpan",
      "Reconciliation"
    ],
    "verified_functions": [
      "reconcile_agent_cost"
    ],
    "verified_functions_count": 1,
    "defensive_guards_count": 4,
    "raised_exceptions": [
      "GenAICostReconciliationError"
    ],
    "sample_fail_closed_invariants": [
      "trace contains sampled-out spans; agent attribute and leaf sum are not comparable without completeness metadata"
    ],
    "stdlib_dependencies": [
      "__future__",
      "dataclasses",
      "typing"
    ],
    "observation_mode": "EMPIRICAL_BOUNDARY_CAPTURE",
    "zero_external_deps": true
  }
}
