How to Track OpenAI API Costs in Your Application
Step-by-step tutorial on tracking OpenAI API costs in production. Monitor GPT-4o usage, track spending by feature, and get real-time cost visibility.
You're using the OpenAI API in production. Your monthly bill is growing. But which features are driving costs? This tutorial shows you how to track OpenAI API costs at the feature level.
What You'll Build
By the end of this tutorial, you'll have:
- Per-feature cost tracking for all OpenAI API calls
- Real-time visibility into token usage and spending
- Environment separation (development vs. production)
Prerequisites
- An existing app using the OpenAI API
- Node.js/TypeScript (Python version coming soon)
- 5 minutes
Step 1: Install the Orbit SDK
npm install @with-orbit/sdkOr with yarn:
yarn add @with-orbit/sdkStep 2: Get Your API Key
- Sign up at app.withorbit.io/signup
- Go to Settings → API Keys
- Create a new key (starts with
orb_live_) - Add it to your environment variables
# .env
ORBIT_API_KEY=orb_live_your_key_hereStep 3: Wrap Your OpenAI Client
Replace your OpenAI client initialization with the Orbit wrapper:
Before:
import OpenAI from 'openai';
const openai = new OpenAI();
const response = await openai.chat.completions.create({
model: 'gpt-4o',
messages: [{ role: 'user', content: 'Hello!' }]
});After:
import OpenAI from 'openai';
import { Orbit } from '@with-orbit/sdk';
const orbit = new Orbit({ apiKey: process.env.ORBIT_API_KEY });
const openai = orbit.wrapOpenAI(new OpenAI(), {
feature: 'chat-assistant',
environment: 'production'
});
// Use exactly like before - tracking is automatic
const response = await openai.chat.completions.create({
model: 'gpt-4o',
messages: [{ role: 'user', content: 'Hello!' }]
});Step 4: Tag Features
Create different wrapped clients for each feature in your app:
// Chat feature
const chatClient = orbit.wrapOpenAI(new OpenAI(), {
feature: 'customer-chat',
environment: process.env.NODE_ENV
});
// Document summarization
const summaryClient = orbit.wrapOpenAI(new OpenAI(), {
feature: 'doc-summary',
environment: process.env.NODE_ENV
});
// Code generation
const codeClient = orbit.wrapOpenAI(new OpenAI(), {
feature: 'code-assistant',
environment: process.env.NODE_ENV
});Now every API call is automatically tagged with its feature and environment.
Step 5: Add User Context (Optional)
Track costs per user or customer for billing attribution:
const client = orbit.wrapOpenAI(new OpenAI(), {
feature: 'chat-assistant',
environment: 'production',
user_id: currentUser.id,
customer_id: currentUser.organizationId
});Step 6: View Your Dashboard
Once you've deployed, go to app.withorbit.io to see:
Filter by feature to see exactly where your costs are going:
- customer-chat: $800/month (65%)
- doc-summary: $300/month (24%)
- code-assistant: $134/month (11%)
What Gets Tracked
For every OpenAI API call, Orbit automatically captures:
- Model — gpt-4o, gpt-4o-mini, etc.
- Tokens — Input, output, and total
- Cost — Calculated from current OpenAI pricing
- Latency — Response time in milliseconds
- Status — Success or error
- Feature — Your custom tag
- Environment — Production, staging, development
Next Steps
Now that you're tracking OpenAI costs:
- Review weekly — Check which features cost the most
- Optimize — Try GPT-4o-mini for high-cost, simple tasks
- Set alerts — Get notified when costs spike
- Track errors — Failed requests still cost tokens
Start Tracking OpenAI Costs
Get visibility into your OpenAI API spending in 5 minutes. Free tier includes 10,000 events/month.
- One-line SDK integration
- Per-feature cost tracking
- Real-time dashboards
- No code changes to existing calls
Related Articles
How to Track Agentic AI Workflows: Task & Customer Attribution
Learn how to track multi-step AI agent workflows with task_id and customer_id. Group LLM calls, measure total costs per task, and attribute AI spend to customers.
How to Monitor AI API Usage and Billing
Set up AI API monitoring to track your usage and costs. Monitor spending across OpenAI, Anthropic, and Gemini with real-time dashboards.
How to Track OpenAI API Costs by Feature
Learn how to track OpenAI API costs at the feature level, not just totals. Understand which parts of your app are driving spend.