Telemetry
AI and LLM SQL recipe

Calculate LLM Cost by Feature and Model

Attribute model spend, tokens, request volume, and cost per request to product features.

Beginnerllm_request_completedReviewed 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

Which product features and models are driving LLM spend?

Daily provider spend cannot explain whether cost growth comes from adoption, a model change, or inefficient prompts. Feature-level request events connect spend to the workflow that created it.

Event contract

Fields the query expects

FieldTypeWhy it exists
timestamp_utcTimestampWhen the model request completed.
featureUtf8Stable product workflow or feature name.
modelUtf8Provider model identifier.
input_tokensInt64Provider-reported input tokens.
output_tokensInt64Provider-reported output tokens.
estimated_cost_usdFloat64Request cost calculated from versioned model pricing.
statusUtf8success or failed.
DataFusion SQL

Copy the query

sql
SELECT
  feature,
  model,
  COUNT(*) AS requests,
  SUM(input_tokens + output_tokens) AS total_tokens,
  ROUND(SUM(estimated_cost_usd), 4) AS cost_usd,
  ROUND(SUM(estimated_cost_usd) / NULLIF(COUNT(*), 0), 4) AS cost_per_request_usd
FROM llm_request_completed
WHERE timestamp_utc >= now() - INTERVAL '30 days'
  AND feature IS NOT NULL
GROUP BY feature, model
ORDER BY 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

Thirty-day model cost by feature

Research reports cost more overall despite far fewer requests, pointing to unit-cost optimization rather than an adoption problem.

featuremodelrequeststotal_tokenscost_usdcost_per_request_usd
research_reportreasoning-large48,0000.80.2
document_summarybalanced-medium34,5000.150.05
support_draftfast-small55,0000.10.02

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

Thirty-day model cost by feature: static chart of synthetic cost_usd values from the Calculate LLM Cost by Feature and Model 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, reproducible input rows, exact SQL, expected output, review notes, and engine version. The CSV contains the displayed result.

How the SQL works

  1. 1Grouping by both feature and model preserves the context needed to explain model migrations.
  2. 2Cost per request distinguishes a high-volume inexpensive workflow from a low-volume expensive one.
  3. 3Store the calculated request cost at event time using a versioned price table so historical reports do not change when provider pricing changes.

Edge cases to decide

  • Do not multiply total tokens by one rate when input, cached input, and output tokens have different prices.
  • Include failed requests if the provider bills them; otherwise add a billing_status field.
  • Use account or team identifiers when margins vary materially by customer.

Recommended dashboard

  • Bar chart: cost_usd by feature
  • Line chart: daily cost split by model
  • Table: highest-cost individual requests without raw prompt content

Alert guidance

Alert when daily feature cost or cost per successful outcome exceeds an agreed budget, not merely when request volume rises.

Read alert setup

Put the recipe to work

Related instrumentation and guides

Define the source data

Event schemas for this analysis

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