Skip to content
Docs

TanStack AI with AI Gateway

Connect TanStack AI to AI Gateway using the @tanstack/ai-vercel-gateway adapter. You can switch models by changing the model ID in your application.

Use an existing JavaScript or TypeScript project with Node.js 22 or later and a Vercel account.

Install TanStack AI and its AI Gateway adapter in your project:

Terminal
pnpm i @tanstack/ai @tanstack/ai-vercel-gateway
Terminal
yarn add @tanstack/ai @tanstack/ai-vercel-gateway
Terminal
npm i @tanstack/ai @tanstack/ai-vercel-gateway
Terminal
bun add @tanstack/ai @tanstack/ai-vercel-gateway

Create an AI Gateway API key and save it in a .env file:

.env
AI_GATEWAY_API_KEY=your_api_key_here

The adapter reads AI_GATEWAY_API_KEY automatically. When that variable is absent, the adapter uses VERCEL_OIDC_TOKEN. See OpenID Connect authentication for token setup and local development.

Create an entry file that passes vercelGatewayText to TanStack AI's chat function. Run this code on the server to keep your credentials private:

index.mts
import { chat } from '@tanstack/ai';
import { vercelGatewayText } from '@tanstack/ai-vercel-gateway';
 
const stream = chat({
  adapter: vercelGatewayText('anthropic/claude-opus-5'),
  messages: [
    {
      role: 'user',
      content: 'Write a one-sentence bedtime story about a unicorn.',
    },
  ],
});
 
for await (const chunk of stream) {
  console.log(chunk);
}

Run the TypeScript example with tsx, loading your API key from .env:

terminal
pnpm dlx tsx --env-file=.env index.mts

For JavaScript, run node --env-file=.env index.mjs. Your terminal displays stream events containing the generated story.

The adapter uses the OpenAI Responses API by default. To use Chat Completions, pass { api: 'chat' } as the second argument to vercelGatewayText.

Use TanStack AI's decide() function with vercelGatewayDecider from @tanstack/ai-vercel-gateway to answer typed questions about application state with Jev through AI Gateway. Jev returns typed choices, scores, and boolean probabilities that your code can use to classify content and decide what happens next.

Follow Moderate product reviews with Jev and TanStack AI to build a moderation workflow in TanStack Start that publishes clear reviews and holds flagged or uncertain reviews for a moderator.

Last updated October 5, 2026

Was this helpful?

supported.