{
  "schema_version": "1.3.0",
  "slug": "what-happens-when-an-instance-double-counts-cached-prompt-tokens-across-a-retried-tool-call-span",
  "title": "What happens when an instance double-counts cached prompt tokens across a retried tool-call span",
  "date_of_record": "2026 November 19",
  "scope": "Applies to observability pipelines that derive per-agent cost and latency SLOs from span-level token telemetry emitted by LLM SDKs and gateway proxies, where prompt caching and retry/fallback logic coexist. Does not apply to flat per-request billing without cache semantics, nor to pipelines that receive authoritative token counts from a single upstream metering authority.",
  "projections": {
    "unified_markdown_uri": "https://corpusfetch.com/c/what-happens-when-an-instance-double-counts-cached-prompt-tokens-across-a-retried-tool-call-span.md",
    "method_python_slice_uri": "https://corpusfetch.com/c/what-happens-when-an-instance-double-counts-cached-prompt-tokens-across-a-retried-tool-call-span.py",
    "remnant_warrant_slice_uri": "https://corpusfetch.com/c/what-happens-when-an-instance-double-counts-cached-prompt-tokens-across-a-retried-tool-call-span.warrant.json"
  },
  "token_profile": {
    "full_markdown_tokens": 1827,
    "python_slice_tokens": 244,
    "warrant_slice_tokens": 220
  },
  "remnant_summary": {
    "core_distinction": "There are two ways a token count can be \"wrong\" in an observability pipeline, and they are not the same failure. The first is *estimation error*: the SDK counts tokens with a local tokenizer that disagrees with the provider's billing tokenizer by a few percent. This is benign, monotone, and averages out across aggregation. The second is *duplication error*: ...",
    "what_was_observed": "A multi-agent orchestrator (planner → 3 parallel workers → synthesizer) routed all LLM calls through a gateway proxy that emitted OpenTelemetry spans. Prompt caching was enabled: the planner's ~14k-token system prompt and tool schema were marked cacheable, and workers reused the same cached prefix. The failure trace: 1. A worker's tool call to an external search API returned a 503. The SDK's retry wrapper re-issued t...",
    "condition_of_validity": "The duplication invariant holds — and therefore the failure is *possible* — only when all three of the following are simultaneously true: - **Cache-read tokens are reported as full prompt tokens by at least one emitter in the trace path.** If every emitter splits `cache_read_input_tokens` from `input_tokens`, the retry's cached prefix is billed at the cache ...",
    "decision_boundary": "The boundary is not \"should we retry\" or \"should we cache.\" It is: **at what layer is the token count made canonical, and is that layer upstream or downstream of the retry and dual-emission logic?** If the canonical count is established *downstream* of retries and deduplication — i.e., you trust the provider's per-request usage object keyed by a provider-ass...",
    "operational_effect": "The observable effect is a **conservation residual that grows with cache-prefix size and retry rate**. Because the duplicated quantity is the cached prefix — typically the largest single token block in an agent's context — the residual is not a rounding error; it scales with the very thing caching was meant to optimize. Teams that enable caching to reduce co..."
  },
  "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": "36517b71f09662b57acaa89315e547dfce7fa5d6759d7e7e56f16391f865a006",
    "verified_classes": [
      "TokenConservationError",
      "TokenSpan"
    ],
    "verified_functions": [
      "attribute_window"
    ],
    "verified_functions_count": 1,
    "defensive_guards_count": 2,
    "raised_exceptions": [
      "TokenConservationError"
    ],
    "sample_fail_closed_invariants": [],
    "stdlib_dependencies": [
      "dataclasses"
    ],
    "observation_mode": "EMPIRICAL_BOUNDARY_CAPTURE",
    "zero_external_deps": true
  }
}
