Guides

Guide

OpenAI cost tracking by customer, feature and team

Updated

Your OpenAI bill tells you how much you spent and on which models. It doesn't tell you which customer, product feature or team the spend served. This guide shows how to track OpenAI API costs at that level, check the figures against your OpenAI invoice, and cap the spend before it surprises you.

What does OpenAI's own usage data show, and what's missing?

OpenAI's usage and cost data breaks your spend down by project, API key and model. What it can't show is your own business: which of your customers a call served, which feature made it, or which team owns it. Those fields don't exist on OpenAI's side, so no export or report can add them later.

OpenAI knows that a key spent $4.02 on a model today, not whether that was one customer running a heavy report or a thousand customers sending one message each. The only moment you know the customer and the feature is when your code makes the call, so that is where tracking has to start.

How do you track OpenAI costs per customer, feature and team?

Tag each OpenAI call with the customer, feature and team when you make it. Token Meter offers two ways to do that: wrap the OpenAI client you already have with the SDK, or point the client at Token Meter's router by changing its base URL. Pick one per call path; you don't need both.

The SDK (Python or TypeScript). Wrap the client, then open an action() scope around the calls it covers. Usage is read from the response OpenAI already returned, so there is no proxy and no extra network hop, and the SDK never throws into your code.

report.ts
import OpenAI from "openai";
import { TokenMeterClient, wrapOpenAI, action } from "@tokenmeter/sdk";

const tl = new TokenMeterClient({ apiKey: process.env.TOKEN_METER_KEY! });
const openai = wrapOpenAI(new OpenAI(), tl);

await action("generate-report", { customerRef: "acme", feature: "reports", team: "growth" }, () =>
  openai.chat.completions.create({
    model: "gpt-4o-mini",
    messages: [{ role: "user", content: "Hello" }],
  }),
);

await tl.flush(); // a short script sends its usage before it exits

The wrapper covers the Chat Completions and Responses APIs, streaming included. On a streamed Chat Completions call it asks OpenAI for the usage itself (stream_options.include_usage); in Python, if you pass your own stream_options, include "include_usage": True yourself. Cached and reasoning tokens are reported by OpenAI as subsets, so they are subtracted before pricing and never counted twice. Events flush every 2 seconds and show up on the dashboard within a few seconds; short-lived scripts should call tokenmeter.flush() or await tl.flush() before exiting.

The router. If you'd rather not touch the client code, change its base URL to the router and use your Token Meter ingest key as the API key. For a model you name, the router forwards the request with your own OpenAI key, stored encrypted in the dashboard, and meters it. Attribution goes in the request body as top-level fields (action, customer_ref, feature, team, environment), which the router removes before forwarding. Before the first request, an Owner or Admin adds your OpenAI key under Router › Providers (Add provider key) and a capability record for each model you call (Advanced › Add capability). Without the key the router answers 424 provider_not_configured; without the capability record, 400 model_capability_not_configured.

report.ts
import OpenAI from "openai";

const openai = new OpenAI({ baseURL: "https://router.timewastevc.com/v1", apiKey: process.env.TOKEN_METER_KEY });

// Built as a variable, so TypeScript accepts the extra fields the OpenAI types don't list.
const body = {
  model: "gpt-4o-mini",
  messages: [{ role: "user" as const, content: "Hello" }],
  action: "generate-report",
  customer_ref: "acme",
  feature: "reports",
  team: "growth",
};
await openai.chat.completions.create(body);

The router's address is https://router.timewastevc.com; the OpenAI SDK needs /v1 at the end. Because the OpenAI SDKs read OPENAI_BASE_URL and OPENAI_API_KEY, many apps can switch with environment variables alone, though calls then arrive without a customer or feature until you add the body fields.

SDK wrapperRouter
ChangeWrap the client, add action() scopes.New base URL and API key, tags as body fields.
OpenAI APIsChat Completions and Responses.Chat Completions (/v1/chat/completions) and Responses (/v1/responses).
Network pathDirect to OpenAI; usage read in-process.Through the router, which calls OpenAI with your key for a model you name.
Can block a callNo; it records what already happened.Yes: rate limits, per-request cost caps, governance policies and an enforced budget (paid plans) are checked before OpenAI is called.
WorkspacesAll.All.

Don't send the same call through the router with a wrapped client: it is metered twice. A call with no tags still counts in every total; it just can't be split by customer. Details: Quickstart, Router and Providers. To turn per-customer cost into margin, see LLM cost per customer.

How do you reconcile OpenAI usage with the OpenAI invoice?

Connect your OpenAI organization with an Admin API key. Token Meter then imports what OpenAI actually billed through its Costs and Usage API, and compares it, day by day, with the cost it metered from your calls. A day matches when the two are within 2% of the invoice; anything further apart is flagged as drift.

  1. Open Settings › Connections and choose Connect an account, top right. Owners and admins can do this.
  2. Pick OpenAI as the Provider and paste an Admin API key. It starts with sk-admin-, and only an Owner of your OpenAI organization can create one (under Settings › Organization › Admin keys on platform.openai.com). A project key (sk-proj-) is refused.
  3. Optionally, under Advanced, add an OpenAI organization ID (only a label) and a ChatGPT workspace ID to also read Codex usage from the ChatGPT Enterprise analytics API. That API needs a ChatGPT workspace Admin key with the enterprise.analytics.usage.read permission, which an OpenAI organization Admin key usually doesn't have. Billing sync works the same without it.
  4. Choose Connect and sync. Token Meter checks the key with OpenAI before saving, stores it encrypted and queues the first import.

The first sync imports the last 7 days; the rest of the history, up to 90 days, follows in the background. After that the connection syncs about once an hour. Days are UTC, the current day isn't requested, and the newest days can lag while OpenAI settles them. Reconciliation runs at 07:00 UTC each day and compares a day once OpenAI's 1-day settlement delay has passed. On Analytics › Bill, OpenAI gets one status for the date range; the main ones are:

StatusMeaning
MatchedEvery reconciled day in the range is within 2%.
Drift detectedThere are invoice lines, but at least one day differs by more than 2%.
Not reconciled yetInvoice lines are imported, but no day in the range has been compared yet. A fresh import shows this until the next 07:00 UTC run after the settlement delay.
Not billed yetThe metered spend in the range falls on days the invoice doesn't cover yet.
Awaiting invoiceNo OpenAI invoice lines are imported for the range.

Illustrative numbers

A made-up day: OpenAI bills $412.30 and metered cost is $405.10. Drift is $7.20, 1.7% of the invoice, so the day matches. On another day OpenAI bills $380.00 against $350.00 metered: $30.00 is 7.9%, so OpenAI shows Drift detected.

Drift usually means some OpenAI calls aren't going through a wrapped client or the router yet. If you pay OpenAI a negotiated rate, open Settings › Connections, expand Advanced and choose Add a private rate, so metered cost is priced the way you are billed; a start date up to 365 days back reprices usage from that day.

The connection feeds more than the check. On Analytics › Bill, once OpenAI invoice lines exist for a range, the invoice is what counts toward your total AI spend, never the invoice plus metered cost. Codex used on an OpenAI API key is already inside that invoice, so it is disclosed, not added again. Data health › Provider usage (the Attribution coverage view) shows, per model, OpenAI's total next to what per-call metering saw and how much of it carries a team or a person. See AI Bill and billing imports, and tracking AI spend across providers if OpenAI is one of several bills.

What about Azure OpenAI?

Azure OpenAI is a separate provider in Token Meter, with its own wrapper and its own billing connection. Wrap the Azure client (tokenmeter.wrap() in Python, wrapAzureOpenAI in TypeScript), and connect the subscription through Azure Cost Management with a service principal; that connection syncs once a day.

Two differences matter. Azure's Responses API returns your deployment name instead of the model id, so pass a deployment-to-model map when you wrap the client, or those calls show as unpriced. And the Azure token rules are inferred from OpenAI's shape, not yet verified against live calls. Subscriptions billed in a currency other than US dollars aren't reconciled.

How do you cap OpenAI spend?

Set a budget: a monthly spend cap on the whole workspace or on one customer, feature, team, environment, provider or model, with alerts as spend climbs. Budgets are checked every 60 seconds against month-to-date spend for the UTC calendar month, from every connected usage source. On the Free plan you can set one advisory, workspace-wide budget; scoped budgets, router enforcement, alert rules and notification channels need a paid plan.

  • Budgets. Thresholds default to 50%, 80% and 100%, each firing once a month, and a projection alert warns when the month's linear burn will go over. Owners and admins create them under Governance › Budgets.
  • Enforced on the router. A workspace-wide budget can be enforced on router traffic: a call that doesn't fit what is left gets 402 monthly_budget_exceeded, unless its routing policy has a cheaper fallback with a quality-approved downgrade. SDK-reported usage and imported bills are counted and alerted on, but can't be blocked.
  • Per-request caps. Through the router, a request can carry max_cost_usd and is refused when its worst-case cost is above it.
  • Alert rules. Under Alerts, a Threshold rule fires when spend for a scope, optionally limited to OpenAI or one model, reaches an amount within the UTC day or month. A Provider spike rule fires when today's OpenAI spend so far reaches a set multiple of yesterday's, scaled to the part of the day that has passed.

Alerts go to email, Slack or Discord channels. With the Admin API key connected, you can also add a provider control (Governance › Rules, under Advanced) that lists your approved OpenAI projects and keys, and usage from anything else is recorded as a violation. See Governance and alerts.

What doesn't this cover?

  • OpenAI calls that aren't metered appear only in the imported OpenAI totals, with no customer or feature.
  • The router stores no responses: on /v1/responses, previous_response_id is refused, so send the whole conversation each time. On /v1/chat/completions it refuses fields it doesn't know, plus n and service_tier.
  • Invoice figures are not live: today is never imported, and the newest days can lag while OpenAI settles them.

Start tracking OpenAI costs

Create a workspace, add an ingest key, wrap your OpenAI client and tag one call with a customer and feature. Then connect an OpenAI Admin API key to check the metered cost against your bill.