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_requestsReviewed 2026-07-27

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 feature name.
modelUtf8Provider model identifier.
total_tokensInt64Input plus 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(total_tokens) AS total_tokens,
  SUM(estimated_cost_usd) AS cost_usd,
  SUM(estimated_cost_usd) / NULLIF(COUNT(*), 0) AS cost_per_request_usd
FROM llm_requests
WHERE timestamp_utc >= now() - INTERVAL '30 days'
GROUP BY feature, model
ORDER BY 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

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-large2,18018,420,000921.40.42
support_draftfast-small32,10024,800,0004960.02
document_summarybalanced-medium8,45012,950,000388.50.05

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

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

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