Prompt
What should I use to track token usage and cost per request?
Latest observation
Nov 22, 2026
OpenAI APIWeb search: off
A good rule of thumb:
- Use your LLM provider’s built-in usage data for the source of truth when available.
- Use your app logs/observability to track per-request token usage and estimated cost.
What to track per request
At minimum, log:
request_idmodelinput_tokensoutput_tokenstotal_tokensestimated_costlatency_msuser_id/tenant_id/session_idif relevanttimestamp
Best options
1. In-app middleware / wrapper
Best for precise per-request tracking.
Wrap every LLM call and record:
- prompt length
- completion length
- usage returned by the API
- pricing lookup to compute cost
This is the most reliable if you want request-level accounting.
2. Observability tools
Good if you want dashboards, traces, and alerting:
- OpenTelemetry + your logging/metrics backend
- LangSmith
- Helicone
- Arize Phoenix
- W&B Weave
- PromptLayer
These can help you inspect prompts, latency, errors, and token usage.
3. Provider dashboards
Good for billing and aggregate usage:
- OpenAI usage dashboard
- Anthropic usage dashboard
- Google/Azure equivalents
These are useful for finance/billing, but usually not enough for per-request debugging.
Recommended setup
If you want something practical:
- Wrap every model call in middleware
- Log token usage and cost to your app logs
- Send traces to an observability tool
- Use provider billing dashboards for reconciliation
Simple implementation idea
Store pricing in config and compute:
cost = (input_tokens / 1_000_000) * input_rate
+ (output_tokens / 1_000_000) * output_rate
If you want, I can also give you:
- a Python example
- a Node.js example
- or a database schema for tracking token usage per request.