{
  "license": "CC BY 4.0",
  "licenseUrl": "https://creativecommons.org/licenses/by/4.0/",
  "recipe": {
    "slug": "llm-cache-and-retry-cost",
    "title": "Measure LLM Cache Savings and Retry Cost",
    "testedWith": "Apache DataFusion 45.2.0",
    "lastReviewed": "2026-07-27"
  },
  "contract": {
    "tableName": "llm_requests",
    "schema": [
      {
        "name": "timestamp_utc",
        "type": "Timestamp",
        "description": "Model request completion time."
      },
      {
        "name": "model",
        "type": "Utf8",
        "description": "Model identifier."
      },
      {
        "name": "cache_outcome",
        "type": "Utf8",
        "description": "hit, miss, or unavailable."
      },
      {
        "name": "attempt",
        "type": "Int64",
        "description": "One-based attempt number."
      },
      {
        "name": "estimated_cost_usd",
        "type": "Float64",
        "description": "Estimated request cost."
      }
    ]
  },
  "query": "SELECT\n  model,\n  COUNT(*) AS requests,\n  SUM(CASE WHEN cache_outcome = 'hit' THEN 1 ELSE 0 END) AS cache_hits,\n  SUM(CASE WHEN attempt > 1 THEN 1 ELSE 0 END) AS retries,\n  100.0 * SUM(CASE WHEN cache_outcome = 'hit' THEN 1 ELSE 0 END)\n    / NULLIF(COUNT(*), 0) AS cache_hit_rate_pct,\n  SUM(CASE WHEN attempt > 1 THEN estimated_cost_usd ELSE 0 END) AS retry_cost_usd,\n  SUM(estimated_cost_usd) AS total_cost_usd\nFROM llm_requests\nWHERE timestamp_utc >= now() - INTERVAL '30 days'\nGROUP BY model\nORDER BY retry_cost_usd DESC;",
  "fixtureKind": "schema-example",
  "inputRows": [
    {
      "timestamp_utc": "2026-07-28T10:00:00Z",
      "model": "configured-model",
      "cache_outcome": "success",
      "attempt": 1,
      "estimated_cost_usd": 0.014
    },
    {
      "timestamp_utc": "2026-07-28T11:00:00Z",
      "model": "configured-model",
      "cache_outcome": "failed",
      "attempt": 2,
      "estimated_cost_usd": 0.052
    },
    {
      "timestamp_utc": "2026-07-28T12:00:00Z",
      "model": "configured-model",
      "cache_outcome": "success",
      "attempt": 3,
      "estimated_cost_usd": 0.021
    }
  ],
  "illustrativeInputRows": [
    {
      "timestamp_utc": "2026-07-28T10:00:00Z",
      "model": "configured-model",
      "cache_outcome": "success",
      "attempt": 1,
      "estimated_cost_usd": 0.014
    },
    {
      "timestamp_utc": "2026-07-28T11:00:00Z",
      "model": "configured-model",
      "cache_outcome": "failed",
      "attempt": 2,
      "estimated_cost_usd": 0.052
    },
    {
      "timestamp_utc": "2026-07-28T12:00:00Z",
      "model": "configured-model",
      "cache_outcome": "success",
      "attempt": 3,
      "estimated_cost_usd": 0.021
    }
  ],
  "expectedOutput": {
    "columns": [
      "model",
      "requests",
      "cache_hits",
      "retries",
      "cache_hit_rate_pct",
      "retry_cost_usd",
      "total_cost_usd"
    ],
    "rows": [
      {
        "model": "gpt-5.1",
        "requests": 18400,
        "cache_hits": 6620,
        "retries": 740,
        "cache_hit_rate_pct": 35.98,
        "retry_cost_usd": 92.4,
        "total_cost_usd": 1480.2
      },
      {
        "model": "gpt-5.1-mini",
        "requests": 49200,
        "cache_hits": 23810,
        "retries": 380,
        "cache_hit_rate_pct": 48.39,
        "retry_cost_usd": 11.8,
        "total_cost_usd": 604.6
      }
    ]
  },
  "notes": [
    "The compact input rows demonstrate field names and JSON types; they are not claimed to generate the displayed aggregate by themselves.",
    "The expected output is deterministic synthetic review data for the documented output shape.",
    "Adapt time windows, thresholds, identities, and business definitions before operational use."
  ]
}