{
  "schema_version": "1.3.0",
  "slug": "why-does-an-instance-double-count-tokens-when-a-tool-call-re-enters-the-agent-span",
  "title": "Why does an instance double-count tokens when a tool call re-enters the agent span?",
  "date_of_record": "2026 November 23",
  "scope": "Applies to agents that emit OpenTelemetry GenAI spans (`gen_ai.*`) where an LLM invocation, a tool execution, and a subsequent re-invocation of the same agent are nested under a shared parent agent span. Does not apply to flat, single-invocation tracing or to spans emitted by the provider SDK rather than the agent runtime.",
  "projections": {
    "unified_markdown_uri": "https://corpusfetch.com/c/why-does-an-instance-double-count-tokens-when-a-tool-call-re-enters-the-agent-span.md",
    "method_python_slice_uri": "https://corpusfetch.com/c/why-does-an-instance-double-count-tokens-when-a-tool-call-re-enters-the-agent-span.py",
    "remnant_warrant_slice_uri": "https://corpusfetch.com/c/why-does-an-instance-double-count-tokens-when-a-tool-call-re-enters-the-agent-span.warrant.json"
  },
  "token_profile": {
    "full_markdown_tokens": 1544,
    "python_slice_tokens": 322,
    "warrant_slice_tokens": 220
  },
  "remnant_summary": {
    "core_distinction": "The GenAI semantic conventions define token attributes (`gen_ai.usage.input_tokens`, `gen_ai.usage.output_tokens`) as properties of *a single model invocation*, not of an agent turn. The span hierarchy, however, expresses *causal nesting*: an agent span can contain many `chat` spans and many `execute_tool` spans. These two graphs — the accounting graph and t...",
    "what_was_observed": "A three-hop agent loop (plan → tool call → synthesize) produced a trace where a naive subtree sum reported 41,200 input tokens for a turn that the provider billed at 18,900. The discrepancy is not noise. Walk the trace: - `chat` span A (planning): `input_tokens=4,000`, `output_tokens=600`. - `execute_tool` span: no token attributes (correct per spec). - `chat` span B (post-tool reasoning): `input_tokens=4,600` — this...",
    "condition_of_validity": "The \"sum leaves, ignore rollups\" rule is valid only when: 1. Every `chat` span sets `gen_ai.operation.name=chat` and carries its own `gen_ai.usage.*`; and 2. No ancestor span duplicates those attributes as a rollup; and 3. Retried calls are distinguishable by `span.status` or a `retry.count` attribute so the reconciler can drop the failed attempt. When condi...",
    "decision_boundary": "The boundary is: **do you reconcile on the causal tree or the accounting tree?** If any consumer of the trace performs a recursive sum of `gen_ai.usage.*` without filtering to `operation.name=chat` and without excluding spans that have `chat` descendants, the result is invalid the moment loop depth exceeds 1. The safe rule is to reconcile *only* over spans w...",
    "operational_effect": "Downstream, the overcount causes three concrete failures: (a) budget-guard agents trip their token ceiling at roughly half the true spend and refuse to continue mid-task; (b) cost dashboards diverge from provider invoices, eroding trust in the trace as a source of truth; (c) an auditor agent replaying the trace to verify a peer's work recomputes a different ..."
  },
  "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": "3233dd8bf3e5d2835d483128faed59cd7126ff68937b3d9bf073ddb1add8d8ad",
    "verified_classes": [
      "TokenAccountingInvariantError",
      "Span"
    ],
    "verified_functions": [
      "reconcile_turn"
    ],
    "verified_functions_count": 1,
    "defensive_guards_count": 2,
    "raised_exceptions": [
      "TokenAccountingInvariantError"
    ],
    "sample_fail_closed_invariants": [],
    "stdlib_dependencies": [
      "dataclasses",
      "typing"
    ],
    "observation_mode": "EMPIRICAL_BOUNDARY_CAPTURE",
    "zero_external_deps": true
  }
}
