Track AI API spend per client, project and end user
Track AI API spend per client, project and end user by attaching user identifiers, routing through scoped keys and querying grouped usage data.
On this page 8 sections
You track AI API spend per client, project and end user by scoping calls with project keys, tagging requests with user identifiers, and querying grouped usage data via the API or CLI. Every completion and generation records its cost in micro-dollars alongside the model that ran. You can inspect spend in real time on every response, aggregate costs across any time window, and enforce daily spending caps at each tier.
Whether you run a multi-tenant application or manage multiple clients, unbundled AI costs make it difficult to bill accurately or prevent runaway workloads. thelastapi provides access to over 400 models across major labs (including OpenAI, Anthropic, Google, DeepSeek, and Moonshot AI) through an OpenAI-compatible gateway at /v1/ai. See how a unified AI API works for routing details. Every call is metered, logged, and attributable.
Why isolate spend across clients, projects and users
Tracking AI spend across multiple clients requires visibility at three distinct levels:
- Client level: If you build for multiple clients, you need clear separation between their workloads to invoice them accurately without sorting through mixed logs.
- Project level: Within a client, separate projects (such as an internal classifier versus a customer-facing bot) have different budgets and risk profiles. A test loop in staging should never interrupt production.
- End user level: In applications where users submit open-ended prompts, individual accounts can consume an outsized share of tokens. Tracking by user ID lets you spot spikes, measure unit economics, and stop abuse before it drains your credits.
How to tag calls with projects, keys and user identifiers
Attributing AI spend starts by sending the right metadata with each request. thelastapi supports multiple ways to partition traffic.
Isolating by project and API key
You can separate clients by assigning each one its own project or named API key:
- Project AI keys (
vz_ai_…): Added withvibiz add ai. Calls made with a project key are billed directly to that project. - Account keys (
vz_sk_…): When calling from a shared worker or agent, pass the client's project ID in theX-Vibiz-Projectheader:
curl https://thelastapi.dev/v1/ai/chat/completions \
-H "Authorization: Bearer $VIBIZ_API_KEY" \
-H "X-Vibiz-Project: prj_client_acme" \
-H "Content-Type: application/json" \
-d '{"model":"vibiz/fast","messages":[{"role":"user","content":"Summarize status."}]}'You can also create named keys with vibiz keys create <name> (such as vibiz keys create client-sync) and later group spend by api_key. Details on permissions and key types are in the Keys and environments guide.
Tagging individual end users
To break down spend per user, pass a pseudonymous user identifier (like an internal database ID, not an email address).
In the standard OpenAI SDK, set the user property:
import OpenAI from 'openai';
const openai = new OpenAI({
baseURL: process.env.VIBIZ_AI_BASE_URL,
apiKey: process.env.VIBIZ_AI_API_KEY,
});
const response = await openai.chat.completions.create({
model: 'vibiz/smart',
messages: [{ role: 'user', content: 'Draft a proposal.' }],
user: 'usr_98a7f1',
});With plain HTTP, send user in the JSON body or use the X-Vibiz-User header:
curl https://thelastapi.dev/v1/ai/chat/completions \
-H "Authorization: Bearer $VIBIZ_AI_API_KEY" \
-H "X-Vibiz-User: usr_98a7f1" \
-H "Content-Type: application/json" \
-d '{"model":"vibiz/fast","messages":[{"role":"user","content":"Extract tasks."}]}'When building an application using @vibiz/app, pass the user ID to vibiz.user(id) to scope all downstream calls and events automatically:
import { vibiz } from '@vibiz/app';
import OpenAI from 'openai';
const scopedApp = vibiz.user('usr_98a7f1');
const openai = new OpenAI(scopedApp.ai);
const res = await openai.chat.completions.create({
model: 'vibiz/fast',
messages: [{ role: 'user', content: 'Summarize meeting.' }],
});How to read per-call cost in real time
Every successful JSON response from /v1/ai/chat/completions returns two metadata fields:
cost_micro_usd: The exact cost of the call in micro-dollars (1 micro-dollar = $0.000001 USD).served_model: The exact model that processed the request (e.g.openai/gpt-6-luna).
{
"id": "chatcmpl_abc123",
"object": "chat.completion",
"model": "vibiz/fast",
"served_model": "openai/gpt-6-luna",
"cost_micro_usd": 65,
"choices": [{ "message": { "role": "assistant", "content": "Done." } }],
"usage": { "prompt_tokens": 150, "completion_tokens": 100, "total_tokens": 250 }
}A cost_micro_usd value of 65 is $0.000065. If you stream answers, a mid-stream failure arrives as a final SSE event with data: {"error": {…}}. Because each call reports its cost, you can log expenses directly to your database without waiting for aggregated reports.
How to query aggregated usage
thelastapi records every billable event once, storing the raw infrastructure cost (cost) and the billed charge (billed, reflecting standard platform markup). You can query aggregated data using the CLI or the API.
Querying from the CLI
Run vibiz usage to view spending across different dimensions:
# Spend over the last 7 days grouped by tool
vibiz usage --period 7d --by tool
# Spend broken down by end user for a project
vibiz usage --period 30d --by end_user --project prj_client_acme
# Spend grouped by API key
vibiz usage --period 7d --by api_key
# Open the hosted dashboard
vibiz usage --openQuerying with the API
Query GET /v1/usage to ingest numbers into client dashboards or invoicing pipelines. The endpoint accepts group_by (up to three of capability, tool, model, alias, project, api_key, end_user, day, outcome) and filters for project, model, tool, capability, end_user, and api_key:
curl "https://thelastapi.dev/v1/usage?period=7d&group_by=end_user&project=prj_client_acme" \
-H "Authorization: Bearer $VIBIZ_API_KEY"The response includes totals and grouped breakdown rows:
{
"object": "usage",
"period": "7d",
"totals": { "cost_usd": 14.25, "billed_usd": 14.25, "units": { "input_tokens": 4200000 } },
"data": [
{ "end_user": "usr_98a7f1", "cost_usd": 4.12, "billed_usd": 4.12 },
{ "end_user": "usr_44b2c8", "cost_usd": 2.85, "billed_usd": 2.85 }
],
"limits": {
"resets_at": "2026-10-26T00:00:00.000Z",
"daily_spend": { "spent_usd": 14.25, "limit_usd": 50, "remaining_usd": 35.75 }
}
}Review the Usage and limits reference for all available filters.
Model prices and tiers
Choosing the right tier keeps client spend under control. thelastapi provides named aliases mapping to specific models, billed per million tokens:
| thelastapi model | Runs on | Good for | Price per million tokens (in / out) |
|---|---|---|---|
vibiz/fast | GPT-6 Luna (OpenAI) | Chat, tool calls, JSON | $0.10 / $0.50 |
vibiz/lite | DeepSeek V4.1 Flash | Low-cost chat, 1M context | $0.14 / $0.42 |
vibiz/smart-mini | Claude Haiku 5.5 (Anthropic) | Fast coding and drafting | $0.10 / $0.50 |
vibiz/balanced | Gemini 3.8 Flash (Google) | Balanced quality, 1M context | $0.825 / $4.125 |
vibiz/reasoning | GPT-6.1 Terra (OpenAI) | Planning, logic, math | $1.00 / $5.00 |
vibiz/smart | Claude Sonnet 5.5 (Anthropic) | Coding, agents, writing | $2.00 / $10.00 |
vibiz/pro | Claude Opus 5.5 (Anthropic) | Long agent runs | $4.00 / $20.00 |
You can also call lab models directly by id, such as moonshotai/kimi-k3.1-pro. Read calling Kimi models with an OpenAI client for setup examples.
How to set hard spending caps
Visibility helps you bill, but hard limits prevent unexpected overages. thelastapi provides caps at three levels:
| Level | Default | How to adjust | Error when reached |
|---|---|---|---|
| Account safety cap | $5 / day | vibiz billing limit <dollars> or PATCH /v1/billing | 429, daily_spend |
| Project AI budget | $1 / day | vibiz ai budget <dollars> --project <id> or PATCH /v1/projects/:id/ai | 429, project_budget |
| End-user daily budget | None | PATCH /v1/projects/:id/ai {"end_user_daily_budget_usd": 0.50} | 429, end_user_budget |
Setting budgets
Adjust your account safety limit to cap all AI and action spend across all projects:
vibiz billing limit 50Set a dedicated daily budget on a specific client's project:
vibiz ai budget 10 --project prj_client_acmeTo stop an individual user from exhausting a client's project budget, set end_user_daily_budget_usd on the project:
curl -X PATCH https://thelastapi.dev/v1/projects/prj_client_acme/ai \
-H "Authorization: Bearer $VIBIZ_API_KEY" \
-H "Content-Type: application/json" \
-d '{"end_user_daily_budget_usd": 0.50}'When an end user hits that limit within a UTC day, their subsequent calls fail with 429 rate_limited and details.reason: "end_user_budget". Other users in that project continue unhindered. Learn more about implementation in per-user AI budgets.
Balance requirements and limit alerts
Daily limits reset at midnight UTC. Owners receive an alert email when any cap reaches 80% and 100% of its allocation, and the console displays a warning banner. Because accounts are prepaid, each call is pre-authorized against your credit balance; see Billing and credits for balance rules.
Next steps
Start tracking your client spend by passing project headers and user IDs on your next completion. To test models and check the full catalog, explore the AI documentation.
Frequently asked questions
How do I find out what an individual AI API request cost?
Inspect the `cost_micro_usd` field in the JSON response from `/v1/ai/chat/completions`, which returns the exact cost in micro-dollars ($0.000001 USD per unit). In text-to-speech calls, check the `X-Vibiz-Served-Model` header.
What error is returned when an end user or project exceeds their daily budget?
The API returns HTTP status `429 rate_limited` with a structured `details.reason` field specifying `end_user_budget`, `project_budget`, or `daily_spend`. Responses also include `limits.resets_at` indicating when the cap resets at midnight UTC.
Can I track end-user spend using the official OpenAI SDK?
Yes. Set the `user` property in the chat completions request body to your pseudonymous user identifier. You can then group or filter usage by `end_user` through the API or with `vibiz usage --by end_user`.