How to Track Claude API Token Usage Per User
If you're building a product on top of the Claude API with multiple users, teams, or customers, you'll quickly hit a problem: Anthropic's API reports token usage per request, not per user. There's no built-in dashboard that tells you "user_42 consumed 180,000 input tokens this month." You have to build that layer yourself, or use a proxy that already does it.
This guide covers both approaches — how to track usage per user by logging the usage object from every Claude response, and how to get the same visibility without writing a tracking system, using per-key metering.
Why Claude's API doesn't do this for you
Every Claude API response includes a usage object with input_tokens and output_tokens:
{
"id": "msg_01...",
"model": "claude-sonnet-4-5",
"usage": {
"input_tokens": 512,
"output_tokens": 210
}
}
This tells you the cost of that one call, nothing more. If 50 different users are hitting your backend with a single shared API key, Anthropic has no concept of who "user_42" is — that's your application's identity model, not theirs. So per-user tracking is always something you build on top, either in your own database or via an API layer that assigns distinct keys or metadata per user.
Option 1: Log usage yourself at the call site
The straightforward approach: wrap every Claude API call and persist the usage data alongside a user ID.
async function callClaude(userId, messages) {
const res = await fetch("https://api.anthropic.com/v1/messages", {
method: "POST",
headers: {
"x-api-key": process.env.ANTHROPIC_API_KEY,
"anthropic-version": "2023-06-01",
"content-type": "application/json"
},
body: JSON.stringify({
model: "claude-sonnet-4-5",
max_tokens: 1024,
messages
})
});
const data = await res.json();
await db.usageLogs.insert({
userId,
model: data.model,
inputTokens: data.usage.input_tokens,
outputTokens: data.usage.output_tokens,
createdAt: new Date()
});
return data;
}
From there you aggregate with a SQL query:
SELECT user_id,
SUM(input_tokens) AS total_input,
SUM(output_tokens) AS total_output
FROM usage_logs
WHERE created_at >= date_trunc('month', now())
GROUP BY user_id;
This works, but it's extra infrastructure you now own: a table, a write on every request, retry logic if the insert fails, and eventually a dashboard or export job so you can actually look at the numbers. For an internal tool this is fine. For a product billing customers by usage, it becomes a liability — one missed log and a customer's invoice is wrong.
Option 2: Separate API keys per user or per app
A lighter-weight pattern is to issue a distinct API key per user, team, or application instead of sharing one key across your whole backend. Then usage tracking becomes "usage per key" instead of "usage per log row you wrote yourself," which is much harder to get wrong.
This is exactly how SubToAPI is built: it sits between your application and your Claude access, and every request goes through an application key (sub_live_...) that you create per user, per customer, or per internal service. Each key's usage — input tokens, output tokens, and request count — is tracked automatically and shown in the dashboard, broken down by key.
curl https://api.subtoapi.app/v1/messages \
-H "Authorization: Bearer $SUBTOAPI_KEY" \
-H "content-type: application/json" \
-d '{
"model": "claude-sonnet-4-5",
"max_tokens": 1024,
"messages": [{"role": "user", "content": "Summarize this ticket."}]
}'
If sub_live_user42 is the key you generated for that specific user, every call made with it is automatically attributed to them — no manual logging step, no table to maintain. You get the per-request usage object back exactly like the native Claude API returns (see /docs/messages), plus aggregate numbers in the dashboard.
For a SaaS with hundreds of users, issuing one key per user and reading usage straight from the dashboard is far less code than building and maintaining your own metering pipeline.
Choosing between the two approaches
| Approach | Good for | Tradeoff | |---|---|---| | Log usage yourself | Full control, custom reporting, on-prem requirements | You own the pipeline: storage, retries, dashboards | | Per-key tracking (e.g. SubToAPI) | SaaS with many users/teams, fast setup | Tied to whatever the proxy's dashboard exposes |
A hybrid is common: use per-key tracking for the raw numbers, and periodically pull them (manually or via export) into your own billing or analytics system if you need deeper reporting.
Setting per-user limits, not just tracking
Tracking usage is step one — most teams eventually want to cap it too, so one user can't burn through your whole Claude budget. If you're logging usage yourself, this means checking cumulative tokens before each call and rejecting requests past a threshold — more code, more edge cases (what happens mid-stream if a user crosses the limit?).
If you're on a per-key setup, this is simpler to reason about since each key already isolates one user's traffic — you can monitor that key's usage trend and revoke or regenerate it if needed. Start with a free trial to see the per-key usage view, or check /pricing for Solo, Team, and Scale plans if you're deciding how to structure keys across a team.
Getting started quickly
If you want per-user tracking without building the logging table and dashboard yourself:
- Create an account and generate one API key per user, team, or service — see /docs/quickstart.
- Point your existing Claude integration at
https://api.subtoapi.app/v1/messageswith the new key. - Check the dashboard for per-key token totals, no extra code required.
questions
Does the Claude API have a native way to track usage per user? No. Anthropic's API returns input_tokens and output_tokens per response, but it has no concept of "users" — that attribution has to be built by your application or by a proxy layer sitting in front of the API.
What's the fastest way to track usage per user without writing custom logging? Issue a separate API key per user or team instead of sharing one key, then read usage per key from a dashboard. SubToAPI does this automatically for every sub_live_... key — see /docs/quickstart.
Should I track input and output tokens separately? Yes. Output tokens are typically more expensive per unit and vary more with prompt design (long completions vs. short ones), so tracking them separately gives you a much more accurate picture of real per-user cost than a combined total.