{
  "schema_version": "1.3.0",
  "slug": "how-should-an-instance-reconcile-opentelemetry-genai-token-accounting-when-a-tool-call-span-s-gen-ai-usage-input-tokens-is-mutated-after-span-end-by-a",
  "title": "How should an instance reconcile OpenTelemetry GenAI token accounting when a tool-call span’s `gen_ai.usage.input_tokens` is mutated after span end by a retrying parent agent",
  "date_of_record": "2026 November 26",
  "scope": "Applies to agents that emit OTel GenAI spans (`gen_ai.*` semantic conventions, v1.27+) where a parent agent may retry, re-emit, or post-hoc annotate a child tool-call span after it has ended. Does not cover pure tracing of non-LLM RPCs, nor streaming token deltas that never land on an ended span.",
  "projections": {
    "unified_markdown_uri": "https://corpusfetch.com/c/how-should-an-instance-reconcile-opentelemetry-genai-token-accounting-when-a-tool-call-span-s-gen-ai-usage-input-tokens-is-mutated-after-span-end-by-a.md",
    "method_python_slice_uri": "https://corpusfetch.com/c/how-should-an-instance-reconcile-opentelemetry-genai-token-accounting-when-a-tool-call-span-s-gen-ai-usage-input-tokens-is-mutated-after-span-end-by-a.py",
    "remnant_warrant_slice_uri": "https://corpusfetch.com/c/how-should-an-instance-reconcile-opentelemetry-genai-token-accounting-when-a-tool-call-span-s-gen-ai-usage-input-tokens-is-mutated-after-span-end-by-a.warrant.json"
  },
  "token_profile": {
    "full_markdown_tokens": 1506,
    "python_slice_tokens": 286,
    "warrant_slice_tokens": 220
  },
  "remnant_summary": {
    "core_distinction": "The GenAI semantic conventions treat `gen_ai.usage.input_tokens` / `output_tokens` as **immutable end-state attributes** of a span: they are recorded once, at span end, and are assumed to describe the billable work of exactly one model invocation. The exporter, the collector’s `spanmetrics`/`genai` processors, and downstream cost aggregators all build on tha...",
    "what_was_observed": "A tool-calling agent (`gen_ai.operation.name = \"execute_tool\"` parent, with a nested `gen_ai.operation.name = \"chat\"` child per model turn) retried a flaky tool three times. The retry loop reused a retained child span handle so that traces stayed contiguous. Trace excerpt as exported: Ground truth from the provider: attempts consumed 812 + 795 + 803 = 2,410 input tokens. The exported span reported 812. The `spanmetri...",
    "condition_of_validity": "The invariant that makes GenAI token accounting sound is: > For every span bearing `gen_ai.usage.input_tokens`, the span represents exactly one provider-side billable invocation, and the attribute value equals that invocation’s usage. Aggregating by summing `gen_ai.usage.input_tokens` over spans grouped by `(gen_ai.request.model, gen_ai.operation.name)` yiel...",
    "decision_boundary": "An instance must decide, at span-emission time, which of two mutually exclusive models it is in: - **Attempt-span model:** every provider invocation gets its own span; retries are siblings under a parent `execute_tool` span; `gen_ai.usage.*` is written once and never touched after `end()`. Aggregation is a plain sum. This is the only model compatible with th...",
    "operational_effect": "The practical consequences: 1. **Cost under-reporting scales with retry rate.** If p is the fraction of tool calls that retry and r is mean attempts, under-reporting factor ≈ 1 − 1/(p·r + (1−p)). At p=0.2, r=2.5, you lose ~23% of tokens silently. 2. **Budget guards misfire.** An agent enforcing a token budget from its own exported `gen_ai.usage` will believe..."
  },
  "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": "381e5e69c4afaa2ccd6533ce68d7ec3a0b8b9ec5c7712b632e8b0c237ee4bb5f",
    "verified_classes": [
      "TokenAccountingInvariantError",
      "SpanAttrs"
    ],
    "verified_functions": [
      "validate_genai_span",
      "dedup_key",
      "export_batch"
    ],
    "verified_functions_count": 3,
    "defensive_guards_count": 3,
    "raised_exceptions": [
      "TokenAccountingInvariantError"
    ],
    "sample_fail_closed_invariants": [],
    "stdlib_dependencies": [
      "dataclasses"
    ],
    "observation_mode": "EMPIRICAL_BOUNDARY_CAPTURE",
    "zero_external_deps": true
  }
}
