Last updated: 2026-04-20

OpenAI Integration

Route OpenAI API calls through SpendLil for spend tracking.

SpendLil works with all OpenAI API endpoints. Swap api.openai.com for openai.gateway.spendlil.ai and add X-SpendLil-Key. Your existing Authorization header is passed through unchanged — the official OpenAI SDKs work without modification.

URL Pattern

diff Before → After
- POST https://api.openai.com/v1/chat/completions
+ POST https://openai.gateway.spendlil.ai/v1/chat/completions

Supported Endpoints

  • /v1/chat/completions — Chat models (GPT-4o, GPT-4o-mini, o1, o3-mini)
  • /v1/embeddings — Embedding models
  • /v1/images/generations — DALL-E
  • /v1/audio/transcriptions — Whisper
  • /v1/moderations — Content moderation

OpenAI SDK (Node.js)

javascript npm install openai
import OpenAI from 'openai';

const client = new OpenAI({
  apiKey: process.env.OPENAI_API_KEY,
  baseURL: 'https://openai.gateway.spendlil.ai/v1',
  defaultHeaders: {
    'X-SpendLil-Key': process.env.SPENDLIL_KEY,
    'X-SpendLil-Tag': 'my-chatbot', // optional
  },
});

const response = await client.chat.completions.create({
  model: 'gpt-4o',
  messages: [{ role: 'user', content: 'Hello' }],
});
console.log(response.choices[0].message.content);

OpenAI SDK (Python)

python pip install openai
import os
from openai import OpenAI

client = OpenAI(
    api_key=os.environ['OPENAI_API_KEY'],
    base_url='https://openai.gateway.spendlil.ai/v1',
    default_headers={
        'X-SpendLil-Key': os.environ['SPENDLIL_KEY'],
        'X-SpendLil-Tag': 'my-chatbot',  # optional
    },
)

response = client.chat.completions.create(
    model='gpt-4o',
    messages=[{'role': 'user', 'content': 'Hello'}],
)
print(response.choices[0].message.content)

cURL

bash Direct HTTP request
curl https://openai.gateway.spendlil.ai/v1/chat/completions \
  -H "Authorization: Bearer $OPENAI_API_KEY" \
  -H "X-SpendLil-Key: $SPENDLIL_KEY" \
  -H "Content-Type: application/json" \
  -d '{
    "model": "gpt-4o",
    "messages": [{"role": "user", "content": "Hello"}]
  }'

Cost Tracking

SpendLil extracts prompt_tokens and completion_tokens from OpenAI's response.usage object and calculates cost using current model pricing.

ModelInput (per 1M tokens)Output (per 1M tokens)
gpt-4o$2.50$10.00
gpt-4o-mini$0.15$0.60
o1$15.00$60.00
o3-mini$1.10$4.40
Key safety

SpendLil never stores your OpenAI API key. It passes through in the Authorization header to OpenAI. We store only a SHA-256 hash for identification and the last 4 characters as a visual hint in your dashboard.