Custom endpoint routing
Requests must pass through an internal gateway or OpenAI-compatible proxy.
AI Tools · Function
createOpenAI creates a customized OpenAI provider for the AI SDK. Configure credentials, endpoint, headers, or transport, then use the provider to create OpenAI models.
createOpenAI builds an OpenAI provider instance from optional settings such as credentials, endpoint, headers, and fetch implementation. The resulting provider is callable with a model ID and also exposes provider-specific model factories such as chat when needed.
createOpenAI(options?: { apiKey?: string; baseURL?: string; headers?: Record<string, string>; fetch?: typeof fetch }): OpenAIProvider| Name | Type | Description |
|---|---|---|
options | object | Optional provider configuration, including baseURL, apiKey, headers, and fetch. |
A configured OpenAI provider instance that can create models from model IDs.
createOpenAI from @ai-sdk/openai when the default provider instance is not sufficient.baseURL to route requests through a proxy or another OpenAI-compatible endpoint.OPENAI_BASE_URL and then to the documented OpenAI endpoint.Authorization header.OPENAI_API_KEY.fetch implementation as well as a custom base URL.chat factory when the endpoint only supports Chat Completions.openai.chat('model-id') rather than the default model factory.import { createOpenAI } from '@ai-sdk/openai';
const openai = createOpenAI({
apiKey: process.env.OPENAI_API_KEY,
});
const model = openai('gpt-5');Creates a configured OpenAI provider and then obtains a model instance by model ID.
Requests must pass through an internal gateway or OpenAI-compatible proxy.
Different deployments need separate credentials or request headers.
A runtime needs instrumentation, retries, or a WebSocket-backed request implementation.
createOpenAI provide?It returns a configured OpenAI provider instance. Configuration can override the API key, base URL, headers, and fetch implementation, while unspecified credentials and the standard endpoint can come from environment variables or defaults.
Follow-up: How would you target a proxy that implements only Chat Completions? Use baseURL for the proxy and create the model through the provider's chat factory.
The factory is imported from @ai-sdk/openai and accepts an optional settings object. The returned provider can be called with a model ID, for example openai('model-id').
Follow-up: How can the provider use a custom transport? Supply a custom fetch function in the factory settings.
The API key setting is sent in the Authorization header. If omitted, the provider uses OPENAI_API_KEY; the base URL similarly defaults through OPENAI_BASE_URL and then the standard OpenAI v1 endpoint.
Follow-up: What should be checked when a proxy behaves unexpectedly? Verify both the configured base URL and whether it supports the API selected by the model factory.
openaiopenai.chat@ai-sdk/openaistreamTextgenerateText