{
  "schema_version": "1.3.0",
  "slug": "why-does-an-instance-emit-a-genai-span-with-gen-ai-usage-input-tokens-that-disagrees-with-the-tool-call-children-it-spawned-and-what-invariant-does-th",
  "title": "Why does an instance emit a GenAI span with `gen_ai.usage.input_tokens` that disagrees with the tool-call children it spawned, and what invariant does this break?",
  "date_of_record": "2026 November 30",
  "scope": "Applies to instrumented LLM agents emitting OpenTelemetry GenAI semantic conventions (`gen_ai.*`) where a single agent span wraps one or more tool-call child spans and reports aggregated token usage. Does not apply to single-shot, tool-free inference spans where no child spans exist and usage is reported exactly once by the provider adapter.",
  "projections": {
    "unified_markdown_uri": "https://corpusfetch.com/c/why-does-an-instance-emit-a-genai-span-with-gen-ai-usage-input-tokens-that-disagrees-with-the-tool-call-children-it-spawned-and-what-invariant-does-th.md",
    "method_python_slice_uri": "https://corpusfetch.com/c/why-does-an-instance-emit-a-genai-span-with-gen-ai-usage-input-tokens-that-disagrees-with-the-tool-call-children-it-spawned-and-what-invariant-does-th.py",
    "remnant_warrant_slice_uri": "https://corpusfetch.com/c/why-does-an-instance-emit-a-genai-span-with-gen-ai-usage-input-tokens-that-disagrees-with-the-tool-call-children-it-spawned-and-what-invariant-does-th.warrant.json"
  },
  "token_profile": {
    "full_markdown_tokens": 1802,
    "python_slice_tokens": 248,
    "warrant_slice_tokens": 220
  },
  "remnant_summary": {
    "core_distinction": "The failure is not \"wrong token count.\" It is a **conservation violation across the span tree**: the parent agent span's `gen_ai.usage.input_tokens` is set to the *last* provider response's usage rather than the *sum* over all provider round-trips the agent performed, while the child tool-call spans remain correctly attributed. The invariant being broken is ...",
    "what_was_observed": "A three-tool agent run (search → fetch → synthesize) produced a parent span with: and three child spans. The first two children were tool-call spans (`gen_ai.tool.name = \"web_search\"`, `\"http_fetch\"`) with no usage attributes (correct—tools don't consume LLM tokens). But the trace also contained *three* nested `gen_ai.operation.name = \"chat\"` spans, one per provider round-trip, because the LLM client auto-instrumenta...",
    "condition_of_validity": "The conservation law holds—and the detector is meaningful—only under these conditions: 1. **Single provider adapter.** If multiple adapters (e.g., an Anthropic client and an OpenAI client) both emit chat spans under one agent span, the sum is still valid but the parent must aggregate across adapters, which no single adapter's `on_end` hook can do. 2. **No st...",
    "decision_boundary": "The boundary is **who owns the aggregate attribute**. Three viable architectures, each with a different failure surface: - **Adapter-owns-aggregate (rejected).** The LLM client adapter accumulates usage across calls within an agent context and writes it to the parent on agent-span end. Fails because the adapter has no reliable signal for \"agent span boundary...",
    "operational_effect": "Downstream consumers of the parent span's usage attribute—cost dashboards, per-agent budget enforcers, token-budget-exhaustion circuit breakers—all operate on a number that is 10–90% too low for any multi-tool agent. The budget enforcer in particular will *never* trip, because it reads the parent span, so an agent that burns 22k input tokens per task appears..."
  },
  "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": "da41a23a0d7d56f8e9d492bbf23c145d1bc138b89fa1befb74a1d55ced8c12cf",
    "verified_classes": [
      "TokenConservationViolation",
      "UsageAccumulator"
    ],
    "verified_functions": [
      "record_chat",
      "reconcile_agent"
    ],
    "verified_functions_count": 2,
    "defensive_guards_count": 1,
    "raised_exceptions": [
      "TokenConservationViolation"
    ],
    "sample_fail_closed_invariants": [],
    "stdlib_dependencies": [
      "dataclasses",
      "typing"
    ],
    "observation_mode": "EMPIRICAL_BOUNDARY_CAPTURE",
    "zero_external_deps": true
  }
}
