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:
pnpm i @tanstack/ai @tanstack/ai-vercel-gatewayyarn add @tanstack/ai @tanstack/ai-vercel-gatewaynpm i @tanstack/ai @tanstack/ai-vercel-gatewaybun add @tanstack/ai @tanstack/ai-vercel-gatewayCreate an AI Gateway API key and save it in a .env file:
AI_GATEWAY_API_KEY=your_api_key_hereThe 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:
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:
pnpm dlx tsx --env-file=.env index.mtsFor 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.
- Authenticate OAuth-protected MCP tools with TanStack AI and Vercel Connect.
- Follow the Using TanStack AI with Vercel AI Gateway guide to connect a chat UI and generate embeddings and images.
- Configure routing through
modelOptions.gatewayusing the TanStack AI adapter reference. The adapter translates these options toproviderOptions.gateway. - Explore provider routing and model fallbacks.
- Connect to an MCP server on Vercel to make its tools available to TanStack AI.
Was this helpful?