Back to blog
Tutorial
January 27, 20267 min read

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/sdk

Or with yarn:

yarn add @with-orbit/sdk

Step 2: Get Your API Key

  1. Sign up at app.withorbit.io/signup
  2. Go to Settings → API Keys
  3. Create a new key (starts with orb_live_)
  4. Add it to your environment variables
# .env
ORBIT_API_KEY=orb_live_your_key_here

Step 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!' }]
});
No Code Changes Needed
The wrapped client has the exact same API as the original OpenAI client. All your existing code works unchanged.

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:

$1,234
Total spend
45M
Total tokens
12,345
API calls

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
Privacy First
Orbit only tracks metadata. We never see your prompts, responses, or API keys. Your data stays private.

Next Steps

Now that you're tracking OpenAI costs:

  1. Review weekly — Check which features cost the most
  2. Optimize — Try GPT-4o-mini for high-cost, simple tasks
  3. Set alerts — Get notified when costs spike
  4. 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
Get started free