Skip to main content

OpenAI Provider Guide

Access GPT-5.4, GPT-5, GPT-4o, o-series reasoning models, and embedding models through the OpenAI API


Overview

OpenAI provides API access to the GPT model family, including the latest GPT-5.4 series, GPT-5 series, GPT-4o multimodal models, and o-series reasoning models. NeuroLink integrates with OpenAI via the @ai-sdk/openai package, providing generation, streaming, tool calling, vision, and embedding capabilities.

Key Benefits

  • GPT-5.4 Series: Newest flagship models (March 2026) with 400K context windows
  • GPT-5 Series: Flagship models with up to 400K context windows
  • GPT-4.1 Series: 1M context window models for large document processing
  • GPT-4o: Multimodal model with vision support
  • o-Series Reasoning: o3, o3-pro, and o4-mini for deep reasoning tasks
  • Embeddings: text-embedding-3-small and other embedding models
  • Tool/Function Calling: Full support for agent workflows
  • Streaming: Real-time streaming responses with tool execution
  • Proxy Support: Route requests through HTTP/HTTPS/SOCKS proxies

Provider Aliases

You can reference this provider using any of the following names:

AliasUsage
openaiCanonical provider name
gptShort alias for convenience
chatgptAlternative alias

These aliases are registered in src/lib/factories/providerRegistry.ts.


Quick Start

1. Get Your API Key

  1. Visit platform.openai.com/api-keys
  2. Sign in or create an account
  3. Click Create new secret key
  4. Copy your new API key (starts with sk-)

2. Configure Environment

Add to your .env file:

# Required: Your OpenAI API key
OPENAI_API_KEY=sk-your-key-here

# Optional: Override default model (defaults to gpt-4o-mini)
OPENAI_MODEL=gpt-4o

3. Test the Setup

import { NeuroLink } from "@juspay/neurolink";

const ai = new NeuroLink();

const result = await ai.generate({
input: { text: "Explain quantum computing in simple terms" },
provider: "openai",
model: "gpt-4o",
});

console.log(result.content);

Supported Models

Available Models (from OpenAIModels enum)

Enum KeyModel IDSeriesContext WindowNotes
GPT_5_4gpt-5.4GPT-5.4400KNew (March 2026)
GPT_5_4_MINIgpt-5.4-miniGPT-5.4400KNew (March 2026)
GPT_5_4_NANOgpt-5.4-nanoGPT-5.4400KNew (March 2026)
GPT_5_3_CODEXgpt-5.3-codexGPT-5.3400K
GPT_5_2gpt-5.2GPT-5.2400K
GPT_5_2_CHAT_LATESTgpt-5.2-chat-latestGPT-5.2128K
GPT_5_2_PROgpt-5.2-proGPT-5.2400K
GPT_5_2_CODEXgpt-5.2-codexGPT-5.2400K
GPT_5_1gpt-5.1GPT-5.1400K
GPT_5_1_CHAT_LATESTgpt-5.1-chat-latestGPT-5.1128K
GPT_5_1_CODEXgpt-5.1-codexGPT-5.1400K
GPT_5_1_CODEX_MAXgpt-5.1-codex-maxGPT-5.1400K
GPT_5_1_CODEX_MINIgpt-5.1-codex-miniGPT-5.1400K
GPT_5gpt-5GPT-5400K
GPT_5_MINIgpt-5-miniGPT-5400K
GPT_5_NANOgpt-5-nanoGPT-5400K
GPT_5_PROgpt-5-proGPT-5400K
GPT_5_CHAT_LATESTgpt-5-chat-latestGPT-5128K
GPT_5_CODEXgpt-5-codexGPT-5400K
GPT_OSS_120Bgpt-oss-120bGPT OSS128K
GPT_OSS_20Bgpt-oss-20bGPT OSS128K
GPT_4_1gpt-4.1GPT-4.11M
GPT_4_1_MINIgpt-4.1-miniGPT-4.11M
GPT_4_1_NANOgpt-4.1-nanoGPT-4.11M
GPT_4Ogpt-4oGPT-4o128K
GPT_4O_MINIgpt-4o-miniGPT-4o128KDefault model
O3o3O-Series200K
O3_MINIo3-miniO-Series200K
O3_PROo3-proO-Series200K
O4_MINIo4-miniO-Series200K
O1o1O-Series200K
O1_PREVIEWo1-previewO-Series128KDeprecated
O1_MINIo1-miniO-Series128KDeprecated
GPT_4gpt-4GPT-4 Legacy8K
GPT_4_TURBOgpt-4-turboGPT-4 Legacy128K
GPT_3_5_TURBOgpt-3.5-turboLegacy16K

Context window sizes are sourced from src/lib/constants/contextWindows.ts. Models without explicit entries use the provider default of 128K.

Default Model

The default model when no model is specified is gpt-4o-mini (set via OpenAIModels.GPT_4O_MINI in the provider registry). This can be overridden with the OPENAI_MODEL environment variable.

Note: When using NeuroLink SDK/CLI, the default is gpt-4o-mini. When instantiating OpenAIProvider directly without setting OPENAI_MODEL, the internal fallback is gpt-4o.

Model Selection by Use Case

import { NeuroLink } from "@juspay/neurolink";

const ai = new NeuroLink();

// Fast, cost-effective responses
const quickResult = await ai.generate({
input: { text: "Summarize this text..." },
provider: "openai",
model: "gpt-4o-mini",
});

// Balanced multimodal performance
const balancedResult = await ai.generate({
input: { text: "Analyze this code..." },
provider: "openai",
model: "gpt-4o",
});

// Latest flagship model
const flagshipResult = await ai.generate({
input: { text: "Design a distributed system" },
provider: "openai",
model: "gpt-5.4",
});

// Deep reasoning with o-series
const reasoningResult = await ai.generate({
input: { text: "Prove this mathematical theorem..." },
provider: "openai",
model: "o3",
});

// Large context with GPT-4.1 (1M tokens)
const largeContextResult = await ai.generate({
input: { text: "Analyze this entire codebase..." },
provider: "openai",
model: "gpt-4.1",
});

Multimodal Capabilities

Models listed in VISION_CAPABILITIES for the openai provider support image analysis. This includes the GPT-5 family, GPT-4.1 family, GPT-4o family, and o-series models.

Image Analysis

const result = await ai.generate({
input: {
text: "Describe what you see in this image",
images: ["data:image/jpeg;base64,..."],
},
provider: "openai",
model: "gpt-4o",
});
# From file path (CLI)
pnpm run cli -- generate "Describe this image" \
--provider openai \
--model gpt-4o \
--image ./photo.jpg

The provider supports up to 10 images per request (defined in IMAGE_LIMITS in src/lib/adapters/providerImageAdapter.ts).


Embedding Support

The OpenAI provider implements both embed() and embedMany() methods for generating vector embeddings.

Default Embedding Model

The default embedding model is text-embedding-3-small. This can be overridden with the OPENAI_EMBEDDING_MODEL environment variable.

Note: The OPENAI_EMBEDDING_MODEL env var is read by getDefaultEmbeddingModel(), but the public embed()/embedMany() methods fall back to text-embedding-3-small directly when no model argument is passed. To use a custom embedding model, pass it as the modelName parameter to embed(text, modelName).

Single Embedding

const ai = new NeuroLink();

// Generate a single embedding
const result = await ai.generate({
input: { text: "Hello world" },
provider: "openai",
});

// Or use the provider directly
const embedding = await provider.embed("Hello world");
// Returns: number[] (e.g., 1536-dimensional vector)

Batch Embeddings

const embeddings = await provider.embedMany([
"First document",
"Second document",
"Third document",
]);
// Returns: number[][] (array of embedding vectors)

Custom Embedding Model

// Use a specific embedding model
const embedding = await provider.embed("Hello world", "text-embedding-3-large");

Server Endpoints

Embeddings are also available via server routes:

  • POST /api/agent/embed -- Single text embedding
  • POST /api/agent/embed-many -- Batch text embeddings

Tool / Function Calling

The OpenAI provider fully supports tool use (supportsTools() returns true). Tools are validated and filtered for OpenAI compatibility before being sent to the API.

import { z } from "zod";
import { tool } from "ai";

const weatherTool = tool({
description: "Get current weather for a location",
parameters: z.object({
location: z.string().describe("City name"),
}),
execute: async ({ location }) => {
return { temperature: 72, condition: "sunny", location };
},
});

const result = await ai.generate({
input: { text: "What's the weather in Tokyo?" },
provider: "openai",
model: "gpt-4o",
tools: { get_weather: weatherTool },
});

Tool Limits

The provider enforces a maximum tool count (default: 150, configurable via the OPENAI_MAX_TOOLS environment variable). Tools exceeding this limit are silently truncated.

Tool Validation

The provider performs OpenAI-specific validation on each tool before sending:

  • Tools must have a description (string) and execute (function)
  • Parameters must be either a Zod schema or a valid JSON schema with type: "object"
  • Invalid tools are filtered out with a warning log

Streaming Responses

const stream = await ai.stream({
input: { text: "Write a detailed article about AI" },
provider: "openai",
model: "gpt-4o",
});

for await (const chunk of stream.stream) {
process.stdout.write(chunk.content);
}

The streaming implementation uses the Vercel AI SDK streamText with fullStream for handling both text and tool call chunks. Multi-step tool execution is supported with configurable maxSteps.

CLI Streaming

pnpm run cli -- stream "Write a story about a robot" \
--provider openai \
--model gpt-4o

Proxy Support

The OpenAI provider uses createProxyFetch() to route API requests through a proxy when configured. The proxy is detected from standard environment variables:

# HTTPS proxy (recommended for OpenAI API calls)
HTTPS_PROXY=http://proxy.example.com:8080

# HTTP proxy
HTTP_PROXY=http://proxy.example.com:8080

# Catch-all proxy
ALL_PROXY=http://proxy.example.com:8080

# SOCKS proxy
SOCKS_PROXY=socks5://proxy.example.com:1080

# Bypass proxy for specific hosts
NO_PROXY=localhost,127.0.0.1,.internal.example.com

Priority order: protocol-specific (HTTPS_PROXY / HTTP_PROXY) > ALL_PROXY > SOCKS_PROXY.

Both the generation/streaming requests and embedding requests use proxy-aware fetch.


Configuration Reference

Environment Variables

VariableDescriptionDefaultRequired
OPENAI_API_KEYAPI key for authentication-Yes
OPENAI_MODELDefault model to usegpt-4o-miniNo
OPENAI_EMBEDDING_MODELDefault embedding modeltext-embedding-3-smallNo
OPENAI_MAX_TOOLSMaximum number of tools per request150No
HTTPS_PROXYHTTPS proxy URL-No
HTTP_PROXYHTTP proxy URL-No
ALL_PROXYCatch-all proxy URL-No
NO_PROXYComma-separated list of proxy bypass hosts-No

CLI Provider Options

FlagValuesDescription
--provider / -popenai, gpt, chatgptUse OpenAI provider
--model / -mmodel ID stringSpecific model to use
--temperature0.0 - 2.0Sampling temperature
--max-tokensintegerMaximum output tokens

Error Handling

The OpenAI provider maps errors to specific error types:

Error TypeCondition
AuthenticationErrorInvalid API key (API_KEY_INVALID or invalid_api_key)
RateLimitErrorRate limit exceeded (rate_limit_error)
InvalidModelErrorModel not found (model_not_found)
NetworkErrorTimeout errors
ProviderErrorAll other OpenAI API errors

Common Issues

"Invalid OpenAI API key"

# Verify key is set
echo $OPENAI_API_KEY | head -c 10
# Expected: sk-xxxxxxxx...

# Get new key at https://platform.openai.com/api-keys

"Rate limit exceeded"

  1. Wait and retry (the error message includes timing guidance)
  2. Reduce request frequency
  3. Use a smaller model (e.g., gpt-4o-mini instead of gpt-4o)
  4. Request a rate limit increase from OpenAI

"Model not found"

Verify the model ID matches one of the values in the OpenAIModels enum. Model IDs are case-sensitive.


Best Practices

Security

  • Never commit API keys to version control
  • Use environment variables or secrets management
  • Rotate API keys periodically
# Use .env file (not committed to git)
echo "OPENAI_API_KEY=sk-..." >> .env

# Add to .gitignore
echo ".env" >> .gitignore

Cost Optimization

  • Use gpt-4o-mini for routine tasks (significantly cheaper than gpt-4o)
  • Use gpt-5-nano for simple classification or extraction tasks
  • Reserve gpt-5.2-pro and o3-pro for tasks requiring maximum capability
  • Monitor token usage via the OpenAI dashboard


Additional Resources