Prompt
How do I track token usage and spend across multiple AI apps?
Latest observation
To track token usage and spend across multiple AI apps, you want a setup that captures usage at the source, normalizes it, and reports it in one place.
1) Track at the API/request level
If your apps call model APIs directly, log these fields for every request:
- app name / team
- user ID or tenant
- model name
- prompt tokens
- completion tokens
- total tokens
- request timestamp
- request ID / trace ID
- estimated cost
- latency and status
Most AI providers return token counts in the response, which makes this straightforward.
2) Centralize logging
Send usage events from all apps to one shared system:
- database table
- data warehouse
- observability tool
- billing/analytics service
Common options:
- Postgres + dashboards
- BigQuery / Snowflake / Redshift
- Datadog / Grafana
- Segment / RudderStack / Kafka pipeline
3) Convert tokens to cost consistently
Create a pricing table per model:
- input token rate
- output token rate
- cached input rate, if applicable
- image/audio/tool-call pricing if relevant
Then calculate:
cost = input_tokens * input_rate + output_tokens * output_rate- add extras for embeddings, tools, reranking, etc.
Keep pricing versioned, since provider rates can change.
4) Separate usage by app, team, and customer
Use tags or dimensions such as:
- app
- environment: dev/staging/prod
- workspace/team
- end customer
- feature or workflow
- model/provider
This lets you answer:
- Which app is most expensive?
- Which customers use the most tokens?
- Which model has the best cost/performance?
5) Add budget alerts and limits
Set:
- daily/weekly/monthly budgets
- per-app budgets
- per-customer quotas
- anomaly alerts for spikes
Examples:
- alert at 80% and 100% of budget
- block or degrade service if a threshold is exceeded
- alert on unusual token growth per request
6) Use dashboards
Build dashboards for:
- total spend by day/week/month
- spend by app/team/model
- token usage trends
- average cost per request
- cost per user/customer
- top expensive prompts/features
7) If you use multiple vendors
Normalize vendor-specific metrics:
- OpenAI, Anthropic, Google, Azure, etc.
- different tokenization schemes
- cached tokens, reasoning tokens, tool calls
Store raw provider metadata too, so you can reprocess later if pricing changes.
8) Use built-in tools if available
If you want less DIY, many platforms offer usage dashboards or metering:
- provider dashboards
- API gateways with metering
- AI observability platforms
- LLM cost trackers and proxy layers
These can be useful, but for multi-app and multi-vendor setups, a centralized internal ledger is usually best.
9) Recommended architecture
A simple pattern:
- App sends request to model
- Middleware logs request + response usage
- Usage event goes to central store
- Cost calculator enriches event with pricing
- Dashboard/alerts read from the store
10) Important implementation detail
Make logging mandatory in a shared SDK or middleware, so every app uses the same schema. Otherwise, reporting gets messy fast.
If you want, I can also give you:
- a sample database schema
- a Python/TypeScript middleware example
- or a dashboard design for usage and spend tracking