Monitoring OpenAI API Costs in Production
July 26, 2026 · 7 min read
How to monitor OpenAI API cost in real time: capture usage per call, attribute spend by feature and user, and catch a spike before it reaches the monthly invoice.
The OpenAI usage dashboard reports what you spent, account-wide, after the fact. For operating a product that's not enough. Effective OpenAI cost monitoring happens inside your own request path, in real time, attributed to the feature and user that caused the spend.
Capture usage on every call
Every OpenAI completion returns a usage block with token counts. Record it as the response comes back and you have the raw material for everything else:
const res = await openai.chat.completions.create({ model, messages });
record({
project: "support-bot",
user: currentUser.id,
model: res.model,
inputTokens: res.usage.prompt_tokens,
outputTokens: res.usage.completion_tokens,
costUsd: price(res.model, res.usage), // current per-token rates
});The dimensions worth slicing by
- Feature - which endpoint or workflow spends the most.
- User - spend is almost always long-tailed; a few users dominate.
- Model - GPT tiers differ by an order of magnitude; a silent model swap shows up here.
- Input vs. output - output is the pricier side; a rising output ratio is a tuning signal.
Watch for the OpenAI-specific gotchas
- Streaming responses don't always surface
usageunless you opt in - make sure you're still counting tokens when you stream. - Tool/function calls add tokens for the tool definitions on every request, whether or not a tool fires.
- Reasoning models bill hidden reasoning tokens as output - they can dwarf the visible answer.
The same discipline applies whether you're on GPT or Claude - the mechanics mirror monitoring Claude API costs, with different field names in the usage block.
From monitoring to action
Recording spend is step one. Turn it into control by adding attribution so you can see who and what, and budget alerts so a departure from baseline pages you in minutes rather than surfacing on the statement.