{
  "license": "CC BY 4.0",
  "licenseUrl": "https://creativecommons.org/licenses/by/4.0/",
  "recipe": {
    "slug": "llm-cost-by-feature",
    "title": "Calculate LLM Cost by Feature and Model",
    "testedWith": "Apache DataFusion 45.2.0",
    "lastReviewed": "2026-07-27"
  },
  "contract": {
    "tableName": "llm_request_completed",
    "schema": [
      {
        "name": "timestamp_utc",
        "type": "Timestamp",
        "description": "When the model request completed."
      },
      {
        "name": "feature",
        "type": "Utf8",
        "description": "Stable product workflow or feature name."
      },
      {
        "name": "model",
        "type": "Utf8",
        "description": "Provider model identifier."
      },
      {
        "name": "input_tokens",
        "type": "Int64",
        "description": "Provider-reported input tokens."
      },
      {
        "name": "output_tokens",
        "type": "Int64",
        "description": "Provider-reported output tokens."
      },
      {
        "name": "estimated_cost_usd",
        "type": "Float64",
        "description": "Request cost calculated from versioned model pricing."
      },
      {
        "name": "status",
        "type": "Utf8",
        "description": "success or failed."
      }
    ]
  },
  "query": "SELECT\n  feature,\n  model,\n  COUNT(*) AS requests,\n  SUM(input_tokens + output_tokens) AS total_tokens,\n  ROUND(SUM(estimated_cost_usd), 4) AS cost_usd,\n  ROUND(SUM(estimated_cost_usd) / NULLIF(COUNT(*), 0), 4) AS cost_per_request_usd\nFROM llm_request_completed\nWHERE timestamp_utc >= now() - INTERVAL '30 days'\n  AND feature IS NOT NULL\nGROUP BY feature, model\nORDER BY cost_usd DESC;",
  "fixtureKind": "reproducible",
  "inputRows": [
    {
      "timestamp_utc": "2026-07-28T18:00:00.000Z",
      "feature": "research_report",
      "model": "reasoning-large",
      "input_tokens": 1600,
      "output_tokens": 400,
      "estimated_cost_usd": 0.2,
      "status": "success"
    },
    {
      "timestamp_utc": "2026-07-28T17:59:00.000Z",
      "feature": "research_report",
      "model": "reasoning-large",
      "input_tokens": 1600,
      "output_tokens": 400,
      "estimated_cost_usd": 0.2,
      "status": "success"
    },
    {
      "timestamp_utc": "2026-07-28T17:58:00.000Z",
      "feature": "research_report",
      "model": "reasoning-large",
      "input_tokens": 1600,
      "output_tokens": 400,
      "estimated_cost_usd": 0.2,
      "status": "success"
    },
    {
      "timestamp_utc": "2026-07-28T17:57:00.000Z",
      "feature": "research_report",
      "model": "reasoning-large",
      "input_tokens": 1600,
      "output_tokens": 400,
      "estimated_cost_usd": 0.2,
      "status": "success"
    },
    {
      "timestamp_utc": "2026-07-28T17:40:00.000Z",
      "feature": "document_summary",
      "model": "balanced-medium",
      "input_tokens": 1200,
      "output_tokens": 300,
      "estimated_cost_usd": 0.05,
      "status": "success"
    },
    {
      "timestamp_utc": "2026-07-28T17:39:00.000Z",
      "feature": "document_summary",
      "model": "balanced-medium",
      "input_tokens": 1200,
      "output_tokens": 300,
      "estimated_cost_usd": 0.05,
      "status": "success"
    },
    {
      "timestamp_utc": "2026-07-28T17:38:00.000Z",
      "feature": "document_summary",
      "model": "balanced-medium",
      "input_tokens": 1200,
      "output_tokens": 300,
      "estimated_cost_usd": 0.05,
      "status": "success"
    },
    {
      "timestamp_utc": "2026-07-28T17:20:00.000Z",
      "feature": "support_draft",
      "model": "fast-small",
      "input_tokens": 800,
      "output_tokens": 200,
      "estimated_cost_usd": 0.02,
      "status": "success"
    },
    {
      "timestamp_utc": "2026-07-28T17:19:00.000Z",
      "feature": "support_draft",
      "model": "fast-small",
      "input_tokens": 800,
      "output_tokens": 200,
      "estimated_cost_usd": 0.02,
      "status": "success"
    },
    {
      "timestamp_utc": "2026-07-28T17:18:00.000Z",
      "feature": "support_draft",
      "model": "fast-small",
      "input_tokens": 800,
      "output_tokens": 200,
      "estimated_cost_usd": 0.02,
      "status": "success"
    },
    {
      "timestamp_utc": "2026-07-28T17:17:00.000Z",
      "feature": "support_draft",
      "model": "fast-small",
      "input_tokens": 800,
      "output_tokens": 200,
      "estimated_cost_usd": 0.02,
      "status": "success"
    },
    {
      "timestamp_utc": "2026-07-28T17:16:00.000Z",
      "feature": "support_draft",
      "model": "fast-small",
      "input_tokens": 800,
      "output_tokens": 200,
      "estimated_cost_usd": 0.02,
      "status": "success"
    }
  ],
  "illustrativeInputRows": [
    {
      "timestamp_utc": "2026-07-28T18:00:00.000Z",
      "feature": "research_report",
      "model": "reasoning-large",
      "input_tokens": 1600,
      "output_tokens": 400,
      "estimated_cost_usd": 0.2,
      "status": "success"
    },
    {
      "timestamp_utc": "2026-07-28T17:59:00.000Z",
      "feature": "research_report",
      "model": "reasoning-large",
      "input_tokens": 1600,
      "output_tokens": 400,
      "estimated_cost_usd": 0.2,
      "status": "success"
    },
    {
      "timestamp_utc": "2026-07-28T17:58:00.000Z",
      "feature": "research_report",
      "model": "reasoning-large",
      "input_tokens": 1600,
      "output_tokens": 400,
      "estimated_cost_usd": 0.2,
      "status": "success"
    },
    {
      "timestamp_utc": "2026-07-28T17:57:00.000Z",
      "feature": "research_report",
      "model": "reasoning-large",
      "input_tokens": 1600,
      "output_tokens": 400,
      "estimated_cost_usd": 0.2,
      "status": "success"
    },
    {
      "timestamp_utc": "2026-07-28T17:40:00.000Z",
      "feature": "document_summary",
      "model": "balanced-medium",
      "input_tokens": 1200,
      "output_tokens": 300,
      "estimated_cost_usd": 0.05,
      "status": "success"
    },
    {
      "timestamp_utc": "2026-07-28T17:39:00.000Z",
      "feature": "document_summary",
      "model": "balanced-medium",
      "input_tokens": 1200,
      "output_tokens": 300,
      "estimated_cost_usd": 0.05,
      "status": "success"
    },
    {
      "timestamp_utc": "2026-07-28T17:38:00.000Z",
      "feature": "document_summary",
      "model": "balanced-medium",
      "input_tokens": 1200,
      "output_tokens": 300,
      "estimated_cost_usd": 0.05,
      "status": "success"
    },
    {
      "timestamp_utc": "2026-07-28T17:20:00.000Z",
      "feature": "support_draft",
      "model": "fast-small",
      "input_tokens": 800,
      "output_tokens": 200,
      "estimated_cost_usd": 0.02,
      "status": "success"
    },
    {
      "timestamp_utc": "2026-07-28T17:19:00.000Z",
      "feature": "support_draft",
      "model": "fast-small",
      "input_tokens": 800,
      "output_tokens": 200,
      "estimated_cost_usd": 0.02,
      "status": "success"
    },
    {
      "timestamp_utc": "2026-07-28T17:18:00.000Z",
      "feature": "support_draft",
      "model": "fast-small",
      "input_tokens": 800,
      "output_tokens": 200,
      "estimated_cost_usd": 0.02,
      "status": "success"
    },
    {
      "timestamp_utc": "2026-07-28T17:17:00.000Z",
      "feature": "support_draft",
      "model": "fast-small",
      "input_tokens": 800,
      "output_tokens": 200,
      "estimated_cost_usd": 0.02,
      "status": "success"
    },
    {
      "timestamp_utc": "2026-07-28T17:16:00.000Z",
      "feature": "support_draft",
      "model": "fast-small",
      "input_tokens": 800,
      "output_tokens": 200,
      "estimated_cost_usd": 0.02,
      "status": "success"
    }
  ],
  "expectedOutput": {
    "columns": [
      "feature",
      "model",
      "requests",
      "total_tokens",
      "cost_usd",
      "cost_per_request_usd"
    ],
    "rows": [
      {
        "feature": "research_report",
        "model": "reasoning-large",
        "requests": 4,
        "total_tokens": 8000,
        "cost_usd": 0.8,
        "cost_per_request_usd": 0.2
      },
      {
        "feature": "document_summary",
        "model": "balanced-medium",
        "requests": 3,
        "total_tokens": 4500,
        "cost_usd": 0.15,
        "cost_per_request_usd": 0.05
      },
      {
        "feature": "support_draft",
        "model": "fast-small",
        "requests": 5,
        "total_tokens": 5000,
        "cost_usd": 0.1,
        "cost_per_request_usd": 0.02
      }
    ]
  },
  "notes": [
    "The included synthetic input rows generate the expected output when timestamps are shifted into the documented query window.",
    "Run this fixture in the public browser SQL playground; the selected query executes locally with DuckDB-Wasm.",
    "Adapt time windows, thresholds, identities, and business definitions before operational use."
  ]
}