OpenAI API spend tracking
Track OpenAI API cost by customer and feature.
Keep the OpenAI SDK and model payloads you already use. Change the base URL, attach product metadata, and make every token traceable to the customer or feature that created it.
Summary
OpenAI API cost tracking is most useful when each request carries business metadata. Route the SDK through an OpenAI-compatible proxy, record actual input and output tokens, calculate model cost, and enforce project or key limits before the next request reaches OpenAI.
SDK change
Base URL only
Cost owner
Your product metadata
Limit action
HTTP block before upstream
Route the OpenAI SDK through one control point.
A proxy integration keeps cost logic outside product code. Your application still calls the OpenAI SDK, while Halvr becomes the place where authentication, attribution, policy, caching, and provider routing are applied consistently.
Use a Halvr project API key for the proxy request. Provider credentials remain encrypted server-side and are never shipped to the browser.
const openai = new OpenAI({
apiKey: process.env.HALVR_API_KEY,
baseURL: "https://api.halvr.io/v1",
});
await openai.chat.completions.create({
model: "gpt-4o-mini",
messages,
metadata: {
customerId: "customer_123",
feature: "ai_writer",
},
});Calculate cost from the response, not an estimate of prompt length.
Tokenizers and model-side behavior make character or word counts unreliable for billing. Use the provider response usage fields for input and output tokens, then apply the price for the exact model used after routing.
Store the resolved provider and model as well as the requested model. This matters when policy routes a request to a cheaper model or fails over to another provider.
Turn request logs into product unit economics.
Aggregate the same request records by customer to compare model cost with account revenue. Aggregate by feature to decide whether an AI workflow belongs in the base plan, needs a usage allowance, or should be redesigned.
The method is more durable than naming API keys after customers because customers can use several features and services. API keys remain useful as an additional operational boundary.
Set limits before an incident, not after the invoice.
Project budgets cap total exposure. API-key budgets contain one service or environment. Customer and feature budgets handle commercial limits and experiments. Halvr evaluates these rolling windows before the upstream fetch and records blocked requests with zero provider tokens.
- Warn the project webhook at 80% usage.
- Mark the budget exhausted at 100%.
- Reject later requests until the rolling window recovers or the limit changes.
- Keep blocked events distinct from threshold alerts.
Choose how much request content to retain.
Cost attribution does not require storing full prompts. Metadata-only retention preserves usage, cost, model, latency, and product identifiers without keeping request or response bodies. Redacted and full modes are available when debugging requirements justify them.
Try Halvr
Route a real request through Halvr.
Keep your current SDK. Change the base URL, attach customer and feature metadata, and set a spend limit.