Prompt
How do I track token usage across multiple AI apps and teams?
Latest observation
To track token usage across multiple AI apps and teams, you need a setup that gives you:
- A single source of truth
- Usage split by app/team/user
- Real-time or near-real-time cost visibility
- Alerts and budgets
- Easy reporting for finance and engineering
Here’s a practical way to do it.
1) Standardize how every request is identified
The biggest mistake is letting each app log usage differently. Instead, enforce a common structure for every AI request:
- App name:
support-bot,sales-copilot,research-tool - Team / department:
support,sales,product - Environment:
prod,staging,dev - User ID / account ID: internal user or customer
- Request ID / trace ID: unique per call
- Model name:
gpt-4.1,gpt-4o-mini, etc. - Token counts:
- input tokens
- output tokens
- total tokens
- Cost: calculated from model pricing at request time
If all apps send these fields into a common logging pipeline, reporting becomes straightforward.
2) Capture usage at the API gateway or wrapper layer
There are three common places to measure token usage:
Option A: At the AI gateway
If all AI traffic goes through one gateway/proxy, this is the easiest approach.
Pros:
- Centralized
- Works across multiple apps and teams
- Easier enforcement of quotas and rate limits
Cons:
- Requires routing all traffic through the gateway
Option B: In a shared SDK/wrapper
Provide a standard client library that every app uses to call AI providers.
Pros:
- Easy to enrich logs with app/team metadata
- Works even if you use multiple providers
Cons:
- Teams can bypass it unless enforced
Option C: From provider billing/usage APIs
Use OpenAI/Anthropic/etc. usage endpoints or billing exports.
Pros:
- Good for reconciliation
- Source of truth for provider-side totals
Cons:
- Usually not enough detail by app/team unless you add metadata and correlate later
Best practice: use both:
- gateway/wrapper logs for per-request detail
- provider billing exports for reconciliation
3) Store usage in a central data store
Put all usage events into one place, such as:
- Postgres / MySQL for smaller setups
- BigQuery / Snowflake / Redshift for larger orgs
- Datadog / ELK / Grafana Loki for observability
- A warehouse + BI tool for finance reporting
A typical table might include:
| timestamp | app | team | env | user_id | model | input_tokens | output_tokens | total_tokens | cost_usd | request_id |
|---|
This enables:
- usage by team
- usage by app
- usage by model
- usage over time
- user-level breakdowns
- anomaly detection
4) Tag usage with metadata and cost centers
To make chargeback/showback work, map each app to a cost center or budget owner.
Example:
support-bot→ Support teaminternal-research→ R&Dcustomer-facing-chat→ Product team
Add tags like:
cost_centerprojectcustomer_id(if applicable)campaignregion
This lets you answer:
- “How much did Sales spend last month?”
- “Which app is driving the most token usage?”
- “Which customer uses the most expensive prompts?”
5) Build dashboards for different audiences
You’ll usually want separate views:
Engineering dashboard
- tokens per request
- top apps by volume
- error rates
- latency
- model mix
- prompt/output distributions
Team/manager dashboard
- daily/weekly token usage
- spend vs budget
- usage trend by project
- top users or workloads
Finance dashboard
- monthly spend
- forecasted spend
- cost by department
- budget variances
- provider reconciliation
Tools commonly used:
- Grafana
- Metabase
- Looker
- Tableau
- Power BI
6) Set budgets, alerts, and guardrails
Once you can measure usage, enforce policies:
- Monthly budget per team/app
- Daily soft limits
- Hard cutoffs for runaway workloads
- Alerting on spikes
- Alerting on unusual model usage
- Alerting when a team is close to budget
Example alerts:
- “Support bot exceeded 80% of monthly budget”
- “Token usage doubled in the last 24 hours”
- “A dev environment is using production-rate volume”
7) Normalize costs across providers and models
If you use multiple AI providers, cost tracking gets trickier because each model has different pricing.
Create a pricing table:
- provider
- model
- input token price
- output token price
- effective date
Then compute cost centrally based on:
- actual token usage
- pricing in effect at the time
- any cached/reused tokens if relevant
This ensures consistent reporting even if pricing changes.
8) Use sampling only for analytics, not billing
If you need detailed prompt/response logging for analysis, consider:
- logging metadata for all requests
- storing full prompts/responses only for sampled or approved traffic
Reason:
- privacy
- storage cost
- compliance risk
For billing/showback, always log usage metadata for every request.
9) Recommended architecture
A simple scalable architecture:
- App calls shared AI SDK or gateway
- Gateway logs request metadata + token usage
- Events go to streaming/logging system
- Data lands in warehouse
- Scheduled jobs enrich with pricing and team mapping
- Dashboards and alerts read from warehouse
10) If you want the simplest implementation
Start with this minimum viable setup:
- Require every app to send:
- app name
- team
- request ID
- model
- token counts
- Log to a shared database or warehouse
- Add a daily job that:
- calculates cost
- aggregates by app/team
- Build one dashboard
- Add budget alerts
That alone gets you 80% of the value.
Example schema
CREATE TABLE ai_usage_events (
id BIGSERIAL PRIMARY KEY,
timestamp TIMESTAMP NOT NULL,
app_name TEXT NOT NULL,
team_name TEXT NOT NULL,
environment TEXT NOT NULL,
user_id TEXT,
request_id TEXT NOT NULL,
provider TEXT NOT NULL,
model TEXT NOT NULL,
input_tokens INT NOT NULL,
output_tokens INT NOT NULL,
total_tokens INT NOT NULL,
cost_usd NUMERIC(12,6) NOT NULL,
metadata JSONB
);
Example aggregation query
SELECT
team_name,
app_name,
DATE_TRUNC('month', timestamp) AS month,
SUM(total_tokens) AS total_tokens,
SUM(cost_usd) AS total_cost_usd
FROM ai_usage_events
GROUP BY 1, 2, 3
ORDER BY total_cost_usd DESC;
Key best practices
- Enforce consistent metadata across apps
- Centralize logging
- Track usage per request, not just per invoice
- Reconcile with provider billing data
- Use budgets and alerts
- Separate dev/test/prod
- Keep privacy and compliance in mind
If you want, I can also give you:
- a tool comparison for token tracking platforms,
- a reference architecture for AWS/GCP/Azure,
- or a ready-to-use database schema + dashboard plan.