Prompt
I'm building an LLM product and need prompt analytics tied to cost and latency
Latest observation
Nov 22, 2026
OpenAI APIWeb search: off
Absolutely — for an LLM product, prompt analytics should connect what was sent to the model with what it cost, how long it took, and ideally what happened downstream.
Here’s a practical framework you can implement.
1) Core metrics to track per request
Cost
Track at least:
- input_tokens
- output_tokens
- total_tokens
- model_name
- provider
- unit pricing at the time of request
- estimated_cost
- actual_cost if available from provider billing
Latency
Track:
- request_start_ts
- first_token_latency_ms if streaming
- end_to_end_latency_ms
- queue_time_ms if you have internal routing
- provider_latency_ms if exposed
- retry_count
- timeout_flag
Prompt analytics
Store:
- prompt_template_id
- prompt_version
- system_prompt_hash
- user_prompt_hash
- tool_calls_count
- retrieval_docs_count
- context_window_usage_pct
- prompt_length_chars/tokens
- structured fields extracted from the request
Quality / outcome
If possible:
- success/failure
- user_rating
- task_completion_flag
- regeneration_count
- fallback_used
- error_type
- hallucination / policy / refusal labels
2) Minimum event schema
A good request-level schema:
{
"request_id": "uuid",
"timestamp": "2026-11-22T12:34:56Z",
"user_id": "hashed-or-internal-id",
"session_id": "uuid",
"app_id": "my-product",
"environment": "prod",
"provider": "openai",
"model": "gpt-4.1-mini",
"prompt_template_id": "support_reply_v3",
"prompt_version": "3.2.1",
"input_tokens": 1200,
"output_tokens": 220,
"total_tokens": 1420,
"estimated_cost_usd": 0.0048,
"request_start_ts": 1730000000.123,
"first_token_latency_ms": 480,
"end_to_end_latency_ms": 1530,
"retry_count": 0,
"timeout": false,
"tool_calls_count": 1,
"retrieval_docs_count": 4,
"context_window_usage_pct": 62,
"status": "success",
"error_type": null
}
3) Useful derived metrics
Once you collect raw events, derive:
Efficiency
- cost per successful task
- latency per successful task
- tokens per successful task
- output tokens per input token
- cost per 1k tokens
- latency per 1k tokens
Quality-adjusted economics
- cost per accepted response
- cost per resolved ticket
- cost per conversion
- cost per high-rated response
Prompt optimization signals
- Which prompt versions have:
- highest cost
- highest latency
- lowest success rate
- most retries
- highest output token inflation
4) Analytics slices that are especially useful
Break down by:
- prompt_template_id
- prompt_version
- model
- tenant/customer
- user cohort
- task type
- channel (chat, email, API, agent)
- language
- region
- tooling path (RAG/no RAG, tools used, function calling)
- request size buckets
- time of day
This helps answer:
- “Which prompt versions are expensive?”
- “Which tasks are slow?”
- “Does adding retrieval reduce cost or increase latency?”
- “Which model is cheapest for acceptable quality?”
5) How to tie prompt text to analytics safely
Don’t log raw prompts everywhere by default. Instead:
- Store prompt hashes for deduping and grouping
- Store template IDs + version
- Redact or tokenize sensitive user content
- Optionally keep raw prompts in a restricted audit store
A common pattern:
- hot analytics table: metadata only
- secure archive: raw prompt/response with redaction controls
6) Dashboards worth building
Executive dashboard
- total requests
- total spend
- average latency
- success rate
- spend by model
- latency by model
Prompt dashboard
- spend by prompt template/version
- latency by prompt template/version
- token distribution
- regression detection after prompt changes
Debug dashboard
- request trace
- prompt input/output
- tool calls
- retrieval chunks
- retries/errors
- timing breakdown
7) Implementation tips
Instrument at the wrapper layer
Wrap every model call so you capture:
- prompt metadata
- token counts
- timing
- provider response
- retries/errors
Use trace IDs
Propagate:
request_idsession_idtrace_idacross your app and model calls so you can join analytics later.
Store timestamps in a standard way
Use epoch ms or RFC3339 UTC.
Capture token counts from provider if possible
Provider-reported token usage is better than estimates.
8) Example table design
llm_requests
- request_id
- timestamp
- user_id
- session_id
- app_id
- prompt_template_id
- prompt_version
- model
- provider
- input_tokens
- output_tokens
- total_tokens
- estimated_cost_usd
- first_token_latency_ms
- end_to_end_latency_ms
- retry_count
- status
- error_type
llm_request_events
For more detailed traces:
- request_id
- event_type
- event_ts
- payload_json
Examples: prompt_rendered, model_called, first_token_received, tool_called, response_postprocessed.
9) If you want to optimize cost and latency automatically
You can add:
- model routing based on prompt/task difficulty
- prompt length budgets
- dynamic truncation/summarization
- response length caps
- cache for repeated prompts
- fallback to cheaper models
- A/B testing on prompt versions
If you want, I can help with any of these next:
- a production-ready analytics schema
- a Postgres / BigQuery table design
- a LangChain / OpenAI / Anthropic instrumentation example
- a dashboard spec for cost + latency + prompt versions