{
  "schema_version": "1.3.0",
  "slug": "why-does-an-instance-of-an-agent-span-lose-token-accounting-integrity-when-tool-call-attributes-are-emitted-before-the-model-completion-event",
  "title": "Why does an instance of an agent span lose token accounting integrity when tool-call attributes are emitted before the model-completion event",
  "date_of_record": "2026 November 8",
  "scope": "Applies to OpenTelemetry GenAI semantic conventions (v1.27+) for spans of kind `INTERNAL` or `CLIENT` representing agent invocations that emit both `gen_ai.usage.input_tokens` / `gen_ai.usage.output_tokens` and one or more tool-call child spans (`gen_ai.tool.name`, `gen_ai.tool.call.id`). Does not apply to pure LLM completion spans with no tool calls, nor to spans where token counts are attached by a post-processing collector rather than the instrumentation itself.",
  "projections": {
    "unified_markdown_uri": "https://corpusfetch.com/c/why-does-an-instance-of-an-agent-span-lose-token-accounting-integrity-when-tool-call-attributes-are-emitted-before-the-model-completion-event.md",
    "method_python_slice_uri": "https://corpusfetch.com/c/why-does-an-instance-of-an-agent-span-lose-token-accounting-integrity-when-tool-call-attributes-are-emitted-before-the-model-completion-event.py",
    "remnant_warrant_slice_uri": "https://corpusfetch.com/c/why-does-an-instance-of-an-agent-span-lose-token-accounting-integrity-when-tool-call-attributes-are-emitted-before-the-model-completion-event.warrant.json"
  },
  "token_profile": {
    "full_markdown_tokens": 1215,
    "python_slice_tokens": 170,
    "warrant_slice_tokens": 220
  },
  "remnant_summary": {
    "core_distinction": "The GenAI semantic conventions treat token usage as an attribute of the **model interaction**, but agents produce token usage across **multiple model interactions interleaved with tool executions**. The convention's `gen_ai.usage.*` attributes are defined as monotonic counters on a span, not as deltas. When an agent span emits a tool-call child span *before*...",
    "what_was_observed": "In a production agent loop (planner → tool executor → synthesizer), the parent agent span reported `gen_ai.usage.output_tokens = 412` while the sum of child model-completion spans reported 3,118 output tokens across six turns. The discrepancy was not a bug in aggregation; it was caused by the instrumentation overwriting `gen_ai.usage.output_tokens` on each turn because the attribute key is singular and set via `span....",
    "condition_of_validity": "This failure is valid (i.e., the accounting invariant holds) only when **all three** of the following are true: 1. The agent span sets `gen_ai.usage.*` exactly once, at span end, with values summed across all model turns. 2. No intermediate export of the parent span occurs between the first tool-call child span and span end (i.e., the span processor does not...",
    "decision_boundary": "The boundary between \"safe to bill from span attributes\" and \"must reconcile against provider-side usage\" falls at the point where an agent span has **more than one model turn and at least one tool-call child span emitted before span end**. Below that boundary (single-turn agents, or agents with no tool calls), the semantic convention's attributes are trustw...",
    "operational_effect": "Operationally, this manifests as silent undercounts that pass schema validation, pass semantic-convention linters, and pass basic span-tree assertions — because the parent span's attributes are well-formed and the child spans are well-formed; only the **sum relation** is violated, and that relation is not part of the convention's conformance suite. Teams tha..."
  },
  "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": "ab01f8de50f3334b5b705c749429ecc906cd137a66c9eb037a81d956584b97e8",
    "verified_classes": [
      "TokenAccountingInvariantError",
      "AgentSpanTokenGuard"
    ],
    "verified_functions": [
      "__init__",
      "record_turn",
      "finalize"
    ],
    "verified_functions_count": 3,
    "defensive_guards_count": 3,
    "raised_exceptions": [
      "TokenAccountingInvariantError"
    ],
    "sample_fail_closed_invariants": [
      "token attribute already emitted; cannot append a turn after span end"
    ],
    "stdlib_dependencies": [],
    "observation_mode": "EMPIRICAL_BOUNDARY_CAPTURE",
    "zero_external_deps": true
  }
}
