Telemetry
AI and LLM SQL recipe

Measure LLM Cache Savings and Retry Cost

Compare cached requests, retry volume, and estimated spend by model to find avoidable AI cost.

Intermediatellm_requestsReviewed 2026-07-27Tested with Apache DataFusion 45.2.0

Reviewed by the Telemetry product team on . SQL compatibility, event contract, synthetic output, and operational caveats. Review standards and ownership

Question answered

How much model cost is associated with retries and cache misses?

Total model spend hides why it changed. Cache outcomes and attempts make avoidable repeated work visible without storing prompts or completions.

Event contract

Fields the query expects

FieldTypeWhy it exists
timestamp_utcTimestampModel request completion time.
modelUtf8Model identifier.
cache_outcomeUtf8hit, miss, or unavailable.
attemptInt64One-based attempt number.
estimated_cost_usdFloat64Estimated request cost.
DataFusion SQL

Copy the query

sql
SELECT
  model,
  COUNT(*) AS requests,
  SUM(CASE WHEN cache_outcome = 'hit' THEN 1 ELSE 0 END) AS cache_hits,
  SUM(CASE WHEN attempt > 1 THEN 1 ELSE 0 END) AS retries,
  100.0 * SUM(CASE WHEN cache_outcome = 'hit' THEN 1 ELSE 0 END)
    / NULLIF(COUNT(*), 0) AS cache_hit_rate_pct,
  SUM(CASE WHEN attempt > 1 THEN estimated_cost_usd ELSE 0 END) AS retry_cost_usd,
  SUM(estimated_cost_usd) AS total_cost_usd
FROM llm_requests
WHERE timestamp_utc >= now() - INTERVAL '30 days'
GROUP BY model
ORDER BY retry_cost_usd DESC;

This read-only query is planned and executed against an empty typed table with Apache DataFusion 45.2.0. The deterministic sample output is synthetic and reviewed separately; validate field types, thresholds, and business definitions against your own data. Read the testing methodology.

Query result

Retry cost within total model cost

The larger model has both the lower cache-hit rate and the larger retry-cost burden.

modelrequestscache_hitsretriescache_hit_rate_pctretry_cost_usdtotal_cost_usd
gpt-5.118,4006,62074035.9892.41,480.2
gpt-5.1-mini49,20023,81038048.3911.8604.6

Synthetic example output. Run the query against your own event schema and thresholds before using it for operational decisions.

Retry cost within total model cost: static chart of synthetic retry_cost_usd values from the Measure LLM Cache Savings and Retry Cost example result
Indexable SVG of the deterministic example output. Download it for an article, runbook, or design review with attribution.

Reproduce the example

Download the public fixture

The JSON bundle includes the typed event contract, illustrative input rows, exact SQL, expected output, review notes, and engine version. The CSV contains the displayed result.

How the SQL works

  1. 1Cache hit rate counts explicit cache hits within all recorded requests.
  2. 2Attempt greater than one isolates repeated model work from first-attempt cost.
  3. 3The result explains cost mechanics; connect it to accepted or retained outcomes before optimizing purely for spend.

Edge cases to decide

  • A provider-side prompt cache and an application response cache are different mechanisms.
  • Avoid double-counting cost when one logical request emits both attempt and summary events.
  • Cost estimates must use the price and token rules active when the request ran.

Recommended dashboard

  • Stacked bars: retry and total cost by model
  • Trend: cache-hit rate
  • Table: features with expensive retry loops

Alert guidance

Alert on retry-cost spikes or sudden cache-hit collapse after minimum request volume.

Read alert setup

Put the recipe to work

Related instrumentation and guides

Continue the analysis

Run it on real events

Create a table, adapt the fields, and save the result

Start free, send structured events, and use the query result as a chart, shared dashboard widget, or alert input.

Get an API key