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
| Model | Context Window | Max Output | Best For |
|---|---|---|---|
gpt-4o | 128K | 16K | General purpose, best quality |
gpt-4o-mini | 128K | 16K | Fast, cost-effective |
gpt-4-turbo | 128K | 4K | Complex reasoning |
o1 | 200K | 100K | Advanced reasoning |
o1-mini | 128K | 65K | Fast reasoning |
gpt-3.5-turbo | 16K | 4K | Simple tasks, lowest cost |
Setup
1. Get OpenAI API Key
- Go to OpenAI Platform
- Create a new API key
- Copy the key (starts with
sk-)
2. Add Key to AI Gateway
- Navigate to AI Gateway Settings
- Click Add Key under Provider Keys
- Select "OpenAI"
- Paste your API key
- 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:
| Parameter | Type | Description |
|---|---|---|
model | string | Model ID (required) |
messages | array | Chat messages (required) |
temperature | number | Sampling temperature (0-2) |
max_tokens | number | Maximum tokens to generate |
top_p | number | Nucleus sampling parameter |
frequency_penalty | number | Frequency penalty (-2 to 2) |
presence_penalty | number | Presence penalty (-2 to 2) |
stop | string/array | Stop sequences |
stream | boolean | Enable streaming |
tools | array | Function definitions |
tool_choice | string/object | Tool selection mode |
response_format | object | JSON mode |
Pricing
AI Gateway passes through OpenAI's pricing. Current rates (as of Jan 2025):
| Model | Input | Output |
|---|---|---|
| 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
- Anthropic Integration
- Caching - Cache OpenAI responses
- Fallback - Fall back to other providers