2027 setup guide · TypeScript AI apps

Vercel AI SDK with an OpenAI-compatible API

The AI SDK has two OpenAI-shaped providers. One assumes OpenAI's Responses API; the other is built for compatible endpoints. Picking the wrong one is the usual failure.

Abstract illustration of a TypeScript app streaming model output
Illustration: a web app streaming from a compatible endpoint. Plugsky Editorial Team.

The Vercel AI SDK is a TypeScript toolkit for streaming chat, generating structured output and building agents. It separates the application from the model through a provider interface, so the same route handler can call OpenAI, a gateway or a private endpoint. That separation is the point: the UI code should not know which host answered the request.

For OpenAI-shaped endpoints the SDK offers two paths. @ai-sdk/openai is the first-party OpenAI provider. @ai-sdk/openai-compatible is a separate package for providers that implement the OpenAI API. They look similar in a code sample and behave differently when the endpoint does not implement OpenAI's newer Responses API.

AI SDK provider choices, checked 23 November 2026
PathUse whenCaveat
@ai-sdk/openai-compatibleThe endpoint implements OpenAI Chat CompletionsUse createOpenAICompatible with a name, key and baseURL.
@ai-sdk/openai with createOpenAIYou need OpenAI-specific features or the Responses APIThe default model factory calls /responses; a Chat Completions-only endpoint will fail.
@ai-sdk/openai with openai.chat()You want the OpenAI provider but a chat-only endpointForces the chat model factory instead of the Responses factory.
AI GatewayYou want a managed gateway across providersThe gateway does not expose every API in the OpenAI package.

The createOpenAICompatible pattern

Install @ai-sdk/openai-compatible, import createOpenAICompatible, and create a provider with a name, an API key and a baseURL. For Plugsky the baseURL is https://api.plugsky.com/v1. The provider object is then used to create a model, and the model is passed to generateText or streamText.

The package exposes optional settings that matter in production: headers for custom routing, queryParams for endpoints that expect them, a custom fetch for proxying or testing, and includeUsage to include usage information in streaming responses. Turn on includeUsage only if the endpoint returns usage; otherwise the field may be absent or empty.

Keep the provider construction in one module. If every route creates its own provider with its own key and baseURL, a key rotation becomes a search across the repository.

  • Package: @ai-sdk/openai-compatible
  • Function: createOpenAICompatible
  • name: a label for logs and telemetry
  • apiKey: a secret reference
  • baseURL: https://api.plugsky.com/v1
  • includeUsage: true only when the endpoint supports it

Why Chat Completions endpoints break with createOpenAI

The AI SDK's OpenAI provider documentation is explicit: the default OpenAI model factory uses the Responses API. If a custom base URL only supports Chat Completions, the docs say to create chat models with openai.chat() or use the OpenAI-compatible provider.

That is the trap. A developer sets baseURL to a compatible endpoint, calls openai('model-id'), and gets a 404 or a schema error because the endpoint never implemented /responses. The code looks correct, the key is valid, and the error points at the model rather than the provider choice.

Use createOpenAICompatible for a generic endpoint. Use createOpenAI with openai.chat() when you specifically want the OpenAI provider's chat model factory and the endpoint implements the chat shape. Use the default factory only when the endpoint really implements the Responses API.

Streaming, tools and structured output

Streaming is the AI SDK's default strength, and it is also where compatible endpoints diverge. Some gateways buffer the response, so the client receives one chunk at the end. Test with the browser network panel, not just the terminal; a buffered stream looks fine in a server log.

Tool calling depends on the model. The SDK passes tools in the OpenAI shape, and a model that does not support them will answer in prose or ignore the tool. Test one tool call per model before building a multi-step agent.

Structured output is the same story. A schema that works with one model can fail with another that returns a slightly different JSON shape. Validate the output in the application, not only in the SDK's type system.

Use streamText for chat and generateText for a background job, and set an abort signal for requests that can run longer than the user will wait. A stream that is never aborted holds a connection and a token budget. The SDK exposes the stream; the application still owns the timeout policy.

What to check before deploying

Check where the key lives. A provider created in a client component can expose the key in the bundle. Construct the provider on the server and call it from a route handler or a server action.

Check the runtime. A long streaming response can hit a timeout that a local test never sees, and the limit depends on the hosting platform. Test the deployment target, not only the development server.

Check usage and cost reporting. If you rely on the SDK's usage object, confirm that the endpoint returns usage in the stream and that the numbers match the gateway's records. If they do not, pick one source of truth before the finance team asks.

Check the fallback path. A model that is unavailable should produce a visible error or a deliberate fallback, not an empty assistant message. The SDK gives you the hooks; the application has to decide the behaviour. Pin the SDK version in the lockfile, because provider behaviour and package defaults change between releases.

Handle the SDK error types explicitly. A 404 from a missing route, a 401 from a bad key and a 429 from a rate limit should not all surface as a generic model failure. The user needs to know whether to retry, and the operator needs to know which layer to inspect.

When a competitor wins

The first-party @ai-sdk/openai provider wins when you need OpenAI's Responses features, built-in tools or the newest OpenAI model behaviour. The AI Gateway wins when a managed gateway across providers is the priority. Calling the OpenAI SDK directly wins when the app is small and the extra abstraction is not worth it.

Plugsky wins when the app is standard Chat Completions, the team wants a flat monthly plan and the deployment has to move from shared API to private VPC or air-gapped infrastructure without rewriting the provider module. The limit is catalogue size: 39 public model IDs, so confirm the chat and embedding models your routes need are available.

Verdict

Use createOpenAICompatible with the Plugsky baseURL for a generic endpoint. If you use @ai-sdk/openai, choose openai.chat() for a Chat Completions-only endpoint and reserve the default factory for a real Responses API. Test streaming, tools and usage against the deployed runtime.

Choose Plugsky when the AI SDK app needs a predictable bill and a movable deployment boundary behind one OpenAI-compatible provider. Choose the first-party OpenAI provider or the AI Gateway when OpenAI-specific features or cross-provider routing are the priority.

Frequently asked questions

Which AI SDK package should I use for a compatible endpoint?

Use @ai-sdk/openai-compatible and createOpenAICompatible for an endpoint that implements OpenAI Chat Completions. Use @ai-sdk/openai when you need OpenAI-specific features.

Why does my compatible endpoint fail with openai('model')?

The default OpenAI model factory uses the Responses API. A Chat Completions-only endpoint will fail. Use openai.chat('model') or the compatible provider.

How do I get usage data in a stream?

Set includeUsage to true in createOpenAICompatible, and confirm that the endpoint actually returns usage in streaming responses.

Can I use the AI SDK in an edge runtime?

Yes, but test the deployed runtime. Long streams can time out in ways a local server does not, and the limits depend on the hosting platform.

Where should the API key live?

On the server. Construct the provider in server code and call it from a route handler or server action so the key is not bundled into client JavaScript.

Related

Sources

  1. Vercel AI SDK: OpenAI-compatible providers · 23 Nov 2026
  2. Vercel AI SDK: OpenAI provider · 23 Nov 2026
  3. Plugsky API quickstart · 23 Nov 2026