{
  "schema_version": "1.3.0",
  "slug": "which-invariants-prevent-opentelemetry-genai-span-token-accounting-from-silently-misattributing-cached-prompt-tokens-across-tool-call-retries",
  "title": "Which invariants prevent OpenTelemetry GenAI span token accounting from silently misattributing cached prompt tokens across tool-call retries",
  "date_of_record": "2026 November 22",
  "scope": "Applies to agents emitting OpenTelemetry GenAI spans (`gen_ai.*` attributes) where a single logical turn fans out into multiple tool calls and retries, and where prompt caching is enabled at the provider. Does not cover pure completion-only workloads without tool fan-out, nor streaming-only traces where span finalization is deferred past process exit.",
  "projections": {
    "unified_markdown_uri": "https://corpusfetch.com/c/which-invariants-prevent-opentelemetry-genai-span-token-accounting-from-silently-misattributing-cached-prompt-tokens-across-tool-call-retries.md",
    "method_python_slice_uri": "https://corpusfetch.com/c/which-invariants-prevent-opentelemetry-genai-span-token-accounting-from-silently-misattributing-cached-prompt-tokens-across-tool-call-retries.py",
    "remnant_warrant_slice_uri": "https://corpusfetch.com/c/which-invariants-prevent-opentelemetry-genai-span-token-accounting-from-silently-misattributing-cached-prompt-tokens-across-tool-call-retries.warrant.json"
  },
  "token_profile": {
    "full_markdown_tokens": 1291,
    "python_slice_tokens": 233,
    "warrant_slice_tokens": 220
  },
  "remnant_summary": {
    "core_distinction": "The GenAI semantic conventions treat `gen_ai.usage.input_tokens` as a monotonic cost signal per span, and `gen_ai.usage.cached_input_tokens` (or provider-equivalent, e.g. `gen_ai.usage.prompt_tokens_details.cached_tokens`) as a *subset* of that input. The invariant the spec leans on is set containment: cached ⊆ input ⊆ total. This is true for a single provid...",
    "what_was_observed": "A tool-using agent (planner → 4 sequential tool calls → synthesis) emitted one `invoke_agent` parent span and five `chat` child spans. Prompt caching was active on the provider. Observed final attributes on the parent: - `gen_ai.usage.input_tokens = 18,240` - `gen_ai.usage.cached_input_tokens = 21,760` - `gen_ai.usage.output_tokens = 1,410` The cached count exceeded total input by ~19%. Root cause trace: each of the ...",
    "condition_of_validity": "The set-containment invariant `cached_input_tokens ≤ input_tokens` holds **only** under the following conjunction: 1. The span represents exactly one provider request (one `chat` call), not a loop or fan-out. 2. `gen_ai.usage.input_tokens` is populated from that single response's `usage` object, not summed from children. 3. No retry re-sends an identical cac...",
    "decision_boundary": "The boundary between \"safe to aggregate\" and \"must de-duplicate\" is the **prefix identity across children**. Concretely: - If two child `chat` spans share the same `gen_ai.request.model` **and** the same prompt-prefix hash (which the conventions do *not* standardize—you must add a custom `gen_ai.prompt.prefix_hash` attribute), their cached-token reports refe...",
    "operational_effect": "The immediate effect is corrupted cost attribution and broken anomaly detection (negative uncached counts, phantom credits). The second-order effect is worse: cache-hit-rate dashboards computed as `cached / input` on agent spans read >100%, and teams \"fix\" this by clamping to 1.0, which masks genuine cache regressions in single-request paths. The durable gua..."
  },
  "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": "03435cff59203f303f67fd802c2b38e4b8a3e83611c57957e9793d2d05246f34",
    "verified_classes": [
      "GenAIUsageInvariantError"
    ],
    "verified_functions": [
      "validate_agent_span_usage",
      "reconcile"
    ],
    "verified_functions_count": 2,
    "defensive_guards_count": 3,
    "raised_exceptions": [
      "GenAIUsageInvariantError"
    ],
    "sample_fail_closed_invariants": [
      "negative uncached token count after validation"
    ],
    "stdlib_dependencies": [],
    "observation_mode": "EMPIRICAL_BOUNDARY_CAPTURE",
    "zero_external_deps": true
  }
}
