Try Jev on Vercel AI Gateway
Give your application a faster way to decide what should happen next. Use Jev to route requests, flag content, and score priorities.
Managing a separate API key, SDK, and billing account for every model provider adds friction as your application grows. Vercel AI Gateway solves this with a single OpenAI-compatible endpoint and one API key that reach hundreds of models, with provider routing, fallbacks, and spend monitoring built in. TanStack AI connects to the Gateway through its official adapter, @tanstack/ai-vercel-gateway, so you can pick a provider per request without changing your application code.
Copy link to headingOverview
In this guide, you'll learn how to:
- Install and authenticate the
@tanstack/ai-vercel-gatewayadapter - Stream chat responses through AI Gateway with the
useChathook - Configure provider routing and fallbacks per request
- Generate embeddings and images with the same API key
Copy link to headingPrerequisites
Before you begin, make sure you have:
- A Vercel account
- A TanStack AI project with
@tanstack/aiand a framework package such as@tanstack/ai-reactinstalled
For local development, you also need Node.js 22+ and the Vercel CLI.
Copy link to headingHow it works
The adapter talks to AI Gateway's public OpenAI-compatible API at https://ai-gateway.vercel.sh/v1. By default it uses the OpenAI Responses API, and model ids follow the creator/model format, such as openai/gpt-6-astra.
The adapter's model catalog is a closed list generated from the Gateway's GET /v1/models endpoint and updated daily. Each model carries its own typed modelOptions and input types, so a text-only model rejects image parts at the type level, and a model without temperature in the catalog doesn't accept a temperature option.
Copy link to headingSteps
Copy link to heading1. Install the adapter
Install the package with your package manager:
pnpm i @tanstack/ai-vercel-gatewaynpm i @tanstack/ai-vercel-gatewayyarn add @tanstack/ai-vercel-gatewaybun add @tanstack/ai-vercel-gatewayCopy link to heading2. Create an API key
The adapter reads AI_GATEWAY_API_KEY from your environment automatically. If that variable isn't set, it falls back to VERCEL_OIDC_TOKEN.
To create an API key:
- Go to the AI Gateway tab in your Vercel dashboard.
- Select API Keys in the sidebar.
- Click Create API Key and follow the steps.
- Save the key to your environment file:
If your application runs on Vercel, you can skip API keys entirely. Vercel generates an OIDC token for your project, and the adapter can use that too.
To use the token locally, run vercel link and vercel env pull to download it. OIDC tokens expire after 12 hours, so run vercel env pull again.
You can also pass a key explicitly to a create* factory instead of relying on the adapter's automatic lookup. This is useful when the key comes from a runtime credential service rather than an environment variable.
For example, store your AI Gateway API key in a Vercel Connect API key connector:
Then request the key with getToken when you construct the adapter:
Vercel Connect stores the key once and returns it to your server-side code at runtime, so no long-lived secret lives in your application config.
Each getToken call is also logged in the connector's Observability tab with the requesting project and environment, giving you an audit trail of where your Gateway key is used. See Connect observability for event types and retention.
Copy link to heading3. Create a chat endpoint
On the server, pass the adapter to TanStack AI's chat function and return the stream as server-sent events (SSE):
The adapter is the only Gateway-specific piece. Swapping to a different provider means changing the model id string, not the endpoint code.
Copy link to heading4. Connect the client
On the client, the useChat hook works the same as with every other provider:
Copy link to heading5. Switch APIs when a model requires it
The adapter defaults to the OpenAI Responses API. If a model must use Chat Completions instead, pass { api: "chat" } as the second argument:
api: "responses" matches the default behavior, and api: "chat-completions" is an alias for api: "chat".
Copy link to headingConfigure provider routing and fallbacks
AI Gateway can try providers in a preferred order, restrict which providers run, and fall back to alternate models when a request fails.
Set these routing options on modelOptions.gateway, and the adapter sends them to the Gateway as providerOptions.gateway. Don't place gateway at the top level of the request body.
| Option | What it controls |
|---|---|
order | The list of providers to try, in order |
only | Which providers are allowed to run |
sort | Provider selection by cost, time to first token, or tokens per second |
models | Fallback models to use when the primary model fails |
caching | Gateway response caching behavior |
disallowPromptTraining | Blocks providers from training on your prompts |
order and only accept catalog provider ids such as "anthropic", and models accepts catalog chat model ids.
Copy link to headingGenerate embeddings
Use vercelGatewayEmbedding with TanStack AI's embed function to create embeddings through the same API key:
Copy link to headingGenerate images
Use vercelGatewayImage for text-to-image generation. The adapter calls the Gateway's POST /v1/images/generations endpoint:
Copy link to headingSummarize text
Use vercelGatewaySummarize with the summarize function:
Copy link to headingLimitations
The @tanstack/ai-vercel-gateway package covers chat, embeddings, text-to-image generation, and summarization. It doesn't generate video or handle speech, transcription, or reranking.
For those tasks, install a different adapter package built for that capability:
| Adapter | Covers |
|---|---|
@tanstack/ai-fal | Video, image, and audio generation, text-to-speech, and transcription |
@tanstack/ai-elevenlabs | Text-to-speech, transcription, music and sound effects, and realtime voice |
Copy link to headingNext steps
- Learn about routing, fallbacks, and budgets in the AI Gateway documentation
- Review AI Gateway authentication options, including OIDC tokens and BYOK
- Read how Vercel Connect tokens issue short-lived credentials at runtime
- Explore the full adapter reference in the TanStack AI Vercel AI Gateway docs
- Run coding agents in isolated microVMs with Vercel Sandbox
- Build a Jev agent with TanStack Start and TanStack AI
- Use the adapter package's
vercelGatewayDeciderwith Jev in Moderate product reviews with Jev, TanStack AI, and AI Gateway to get typed choices, scores, and boolean probabilities instead of generated text