Transactional

OpenAI

Using OpenAI models through AI Gateway.

Overview

AI Gateway provides full support for OpenAI's GPT models, including GPT-4o, GPT-4-turbo, o1, and GPT-3.5-turbo. All OpenAI features are supported including streaming, function calling, and vision.

Supported Models

ModelContext WindowMax OutputBest For
gpt-4o128K16KGeneral purpose, best quality
gpt-4o-mini128K16KFast, cost-effective
gpt-4-turbo128K4KComplex reasoning
o1200K100KAdvanced reasoning
o1-mini128K65KFast reasoning
gpt-3.5-turbo16K4KSimple tasks, lowest cost

Setup

1. Get OpenAI API Key

  1. Go to OpenAI Platform
  2. Create a new API key
  3. Copy the key (starts with sk-)

2. Add Key to AI Gateway

  1. Navigate to AI Gateway Settings
  2. Click Add Key under Provider Keys
  3. Select "OpenAI"
  4. Paste your API key
  5. Click Save

Usage

Basic Chat Completion

import OpenAI from 'openai';
 
const openai = new OpenAI({
  baseURL: 'https://api.transactional.dev/ai/v1',
  apiKey: process.env.GATEWAY_API_KEY,
});
 
const response = await openai.chat.completions.create({
  model: 'gpt-4o',
  messages: [
    { role: 'system', content: 'You are a helpful assistant.' },
    { role: 'user', content: 'What is the capital of France?' }
  ],
});

With Streaming

const stream = await openai.chat.completions.create({
  model: 'gpt-4o',
  messages: [{ role: 'user', content: 'Tell me a story' }],
  stream: true,
});
 
for await (const chunk of stream) {
  process.stdout.write(chunk.choices[0]?.delta?.content || '');
}

With Function Calling

const response = await openai.chat.completions.create({
  model: 'gpt-4o',
  messages: [{ role: 'user', content: 'What is the weather in Paris?' }],
  tools: [
    {
      type: 'function',
      function: {
        name: 'get_weather',
        description: 'Get the current weather for a location',
        parameters: {
          type: 'object',
          properties: {
            location: { type: 'string', description: 'City name' }
          },
          required: ['location']
        }
      }
    }
  ],
});

With Vision (GPT-4o)

const response = await openai.chat.completions.create({
  model: 'gpt-4o',
  messages: [
    {
      role: 'user',
      content: [
        { type: 'text', text: 'What is in this image?' },
        {
          type: 'image_url',
          image_url: { url: 'https://example.com/image.jpg' }
        }
      ]
    }
  ],
});

With JSON Mode

const response = await openai.chat.completions.create({
  model: 'gpt-4o',
  messages: [
    { role: 'system', content: 'You output JSON.' },
    { role: 'user', content: 'List 3 fruits' }
  ],
  response_format: { type: 'json_object' },
});

Parameters

All standard OpenAI parameters are supported:

ParameterTypeDescription
modelstringModel ID (required)
messagesarrayChat messages (required)
temperaturenumberSampling temperature (0-2)
max_tokensnumberMaximum tokens to generate
top_pnumberNucleus sampling parameter
frequency_penaltynumberFrequency penalty (-2 to 2)
presence_penaltynumberPresence penalty (-2 to 2)
stopstring/arrayStop sequences
streambooleanEnable streaming
toolsarrayFunction definitions
tool_choicestring/objectTool selection mode
response_formatobjectJSON mode

Pricing

AI Gateway passes through OpenAI's pricing. Current rates (as of Jan 2025):

ModelInputOutput
gpt-4o$2.50/1M$10.00/1M
gpt-4o-mini$0.15/1M$0.60/1M
gpt-4-turbo$10.00/1M$30.00/1M
o1$15.00/1M$60.00/1M
gpt-3.5-turbo$0.50/1M$1.50/1M

Migration from Direct OpenAI

Migrating is simple - just change two lines:

// Before
const openai = new OpenAI({
  apiKey: process.env.OPENAI_API_KEY,
});
 
// After
const openai = new OpenAI({
  baseURL: 'https://api.transactional.dev/ai/v1',
  apiKey: process.env.GATEWAY_API_KEY,
});
 
// All your existing code works unchanged!

Troubleshooting

Model not found

Ensure you've added your OpenAI API key in Settings.

Rate limits

OpenAI rate limits are passed through. Check your OpenAI account limits.

Invalid API key

Verify your OpenAI key is valid and has sufficient credits.

Next Steps