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-27

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 recipe is read-only and reviewed against Telemetry's DataFusion query patterns. Sample output is deterministic and synthetic; validate field types, thresholds, and business definitions against your own data.

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.

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