Getting Started

IntroductionQuick Start

Integrations

OpenAIAnthropicMCP ServerSlack

Features

Smart RoutingPrompt ClassifierTeam ManagementBudget & AlertsCaching & PerformanceAnalytics & Reports

Reference

SDKREST APIErrors
Docs/Integrations/OpenAI

OpenAI Integration

Track per-request costs across GPT-5.5, GPT-5.4, and GPT-5.4-mini. Attribute spend by developer and feature.

Installation

npm install costlens openai

Quick Start

Wrap your OpenAI client for automatic tracking and cost optimization:

import { CostLens } from 'costlens';
import OpenAI from 'openai';

const openai = new OpenAI({ apiKey: process.env.OPENAI_API_KEY });
const costlens = new CostLens({ 
  apiKey: process.env.COSTLENS_API_KEY 
});

const tracked = costlens.wrapOpenAI(openai);

// Use exactly like normal — tracking is automatic
const result = await tracked.chat.completions.create({
  model: 'gpt-5.4',
  messages: [{ role: 'user', content: 'Hello!' }]
});
No timing code needed. Automatic error tracking, built-in retries, and optional caching included.

Instant Mode

Works without a CostLens API key for development and testing:

const costlens = new CostLens();
const ai = costlens.wrapOpenAI(openai);

Tagging Requests

Attribute costs to specific features or users:

const result = await tracked.chat.completions.create({
  model: 'gpt-5.4',
  messages: [{ role: 'user', content: 'Review this PR' }]
}, {
  promptId: 'code-review',
  userId: 'dev_alice',
});

Streaming

Streaming responses are tracked automatically after completion:

const stream = await tracked.chat.completions.stream({
  model: 'gpt-5.4',
  messages: [{ role: 'user', content: 'Tell me a story' }]
});

for await (const chunk of stream) {
  process.stdout.write(chunk.choices[0]?.delta?.content || '');
}

Caching

const costlens = new CostLens({ 
  apiKey: process.env.COSTLENS_API_KEY,
  enableCache: true
});

const tracked = costlens.wrapOpenAI(openai);

const result = await tracked.chat.completions.create(
  { model: 'gpt-5.4', messages: [...] },
  { cacheTTL: 3600000 }  // 1 hour
);

Batch Processing

Process multiple requests in a single call for better performance:

const requests = [
  { provider: 'openai', model: 'gpt-5.4', tokens: 150, latency: 1200 },
  { provider: 'openai', model: 'gpt-5.4-mini', tokens: 100, latency: 800 },
];

await costlens.trackBatch(requests);

Supported Models

ModelInputOutput
GPT-5.4$2.50/1M$15/1M
GPT-5.4-mini$0.75/1M$4.50/1M
GPT-5.5$5/1M$30/1M
You are responsible for verifying pricing accuracy. Check openai.com/pricing for current rates. Custom rates can be set in Settings.

What Gets Tracked

  • Model name and provider
  • Token usage (input + output)
  • Cost (tokens × model price)
  • Latency (response time in ms)
  • Success/failure status
  • Prompt ID and user attribution

Next

Anthropic

SDK Reference

Error Handling

Previous

← Quick Start

Next

Anthropic →