Why Track LLM Costs Per User?
Consider these scenarios:- Your SaaS platform serves thousands of users—how do you track and bill their usage fairly?
- Your enterprise team needs cost transparency across different departments—how do you allocate expenses?
- Your application has multiple features leveraging LLMs—how much is each feature costing you?
- Portkey Dashboard: Use metadata filters to analyze costs visually.
- Analytics API: Integrate with your systems to display real-time cost insights in your app.
Step 1: Attach User Metadata to LLM Requests
Portkey allows you to attach metadata (key-value pairs) to each request, enabling cost breakdowns per user. Portkey’s metadata accepts a JSON object ofkey:value
pair. Each metadata value should be a string (max length: 128 characters). Here’s how to implement it:
Step 2: View User Costs in the Portkey Dashboard
Portkey’s dashboard provides a live view of costs broken down by metadata filters.- Open Portkey Dashboard
- In the Search Bar, select
>Meta
- Add a metadata filter, e.g.,
_user = customer_12345
_user
+ env
) and adjust the time range for historical cost tracking.

Step 3: Fetch Cost Data Programmatically via the Analytics API
For deeper integrations, the Analytics API enables real-time cost tracking inside your own application. This is useful for:- User-facing billing dashboards (show users their LLM usage in real time)
- Automated cost monitoring (trigger alerts when a user’s spending exceeds a threshold)
- Enterprise reporting (export data for budget forecasting)
- Total requests and costs per metadata value
- Average token usage for optimization analysis
- User feedback metrics for quality assessment
- Timestamp data for temporal analysis
Step 4: Tracking User Costs using Portkey’s Analytics API
Before making your first API call, you’ll need to obtain an API key from the Portkey Dashboard. This key requires analytics scope access, which you can configure in your API key settings.Review the complete Analytics API documentation for additional endpoints and features.
- Replace the api key in the
x-portkey-api-key
header. - In the url replace
{metadataKey}
with the metadata key you want to track costs for.
- Use the
page_size
parameter to control the number of results per response - Include additional filters by passing a stringified JSON object in the
metadata
field
- CTOs: Gain visibility into LLM costs across teams.
- DevOps: Monitor and optimize token usage.
- Product Teams: Track costs by feature to identify inefficiencies.
- Finance Teams: Automate cost allocation and reporting.
Step 5: Automate User Cost Tracking
Once you’ve integrated metadata tagging and the Analytics API, you can:- Trigger alerts when users exceed spending limits
- Embed LLM cost breakdowns into your SaaS billing dashboard
- Optimize feature costs by analyzing usage patterns
Next Steps
- Set Up Budget Limits for API Keys
- Implement Fallback Strategies for Cost Control
- Explore Portkey’s Prompt Management