Skip to main content

Vercel AI SDK

Vercel AI SDK is a TypeScript toolkit for building AI application flows, including text generation, streaming UI responses, tool calls, and structured output. In AISIX deployments, the application can keep using AI SDK helpers while the model provider configuration points to the gateway.

The OpenAI-compatible provider package lets Vercel AI SDK call a custom base URL with an API key. Use that provider when a TypeScript application should send model requests through AISIX.

This guide uses the AI SDK core package with @ai-sdk/openai-compatible. You will create an OpenAI-compatible provider that points at AISIX and use an AISIX model alias from application code.

Prerequisites

Before starting, prepare the following:

  • A TypeScript project for the application.
  • A running AISIX gateway with the proxy listener available.
  • An AISIX caller API key.
  • A model alias the caller API key can access through the OpenAI-Compatible API.

Configure Vercel AI SDK

Install the AI SDK packages if the application does not already include them:

npm install ai @ai-sdk/openai-compatible

Set the values that the application will use:

# Replace with your values
export AISIX_BASE_URL="http://127.0.0.1:3000/v1"
export AISIX_API_KEY="YOUR_CALLER_API_KEY"
export AISIX_MODEL="gpt-4o-prod"

Create an OpenAI-compatible provider for AISIX:

import { createOpenAICompatible } from "@ai-sdk/openai-compatible";
import { generateText } from "ai";

const aisix = createOpenAICompatible({
name: "aisix",
apiKey: process.env.AISIX_API_KEY,
baseURL: process.env.AISIX_BASE_URL,
});

async function main() {
const { text } = await generateText({
model: aisix(process.env.AISIX_MODEL ?? "gpt-4o-prod"),
prompt: "Write one sentence about model governance.",
});

console.log(text);
}

main();

The model value is the AISIX model alias, not the upstream provider model ID. AISIX governs the model request path: it authenticates the caller API key, resolves the alias, applies configured policies, records telemetry, and dispatches the request to the provider behind the alias. The application still owns UI streaming, tool orchestration, and response handling.

Verify the Route

Run the application from the shell where the AISIX environment variables are set.

When the request succeeds, verify the following results:

  • The application prints generated text.
  • AISIX records a successful POST /v1/chat/completions request for the selected model alias.

If you use AISIX Cloud or an on-premises managed control plane, verify the request in the managed gateway logs. For self-hosted gateways, use your configured logs, metrics, or upstream provider logs.

Handle Compatibility Issues

If a request fails, first confirm that the caller API key can access the selected model alias and that baseURL points to the AISIX proxy API root with /v1.

This guide uses chat-style generation. Validate any other AI SDK helper, such as streaming, tool calling, or structured output, before relying on gateway policy or telemetry for that path.

Next Steps

  • OpenAI-Compatible API: review gateway-facing request behavior.
  • Streaming: confirm streaming behavior before using streamed UI responses.
  • Tool Calling: review OpenAI-compatible tool-call behavior through AISIX.
API7.ai Logo

The digital world is connected by APIs,
API7.ai exists to make APIs more efficient, reliable, and secure.

Sign up for API7 newsletter

Product

API7 Gateway

SOC2 Type IIISO 27001HIPAAGDPRRed Herring

Copyright © APISEVEN PTE. LTD 2019 – 2026. Apache, Apache APISIX, APISIX, and associated open source project names are trademarks of the Apache Software Foundation