Open History to see the last commit
1---
2title: LLM Gateway
3id: llmgateway-adapter
4description: "Reach hundreds of models from OpenAI, Anthropic, Google, Moonshot, DeepSeek, and more through one OpenAI-compatible LLM Gateway endpoint with @tanstack/ai-llmgateway. Hosted or self-hosted."
5keywords:
6 - tanstack ai
7 - llm gateway
8 - llmgateway
9 - multi-provider
10 - unified api
11 - model router
12 - self-hosted
13 - adapter
14---
15
16`llmgatewayText` calls one OpenAI-compatible endpoint that routes across providers, falls back on failure, and records usage and cost.
17
18## 1. Install
19
20<!-- ::start:tabs variant="package-manager" mode="install" -->
21
22react: @tanstack/ai-llmgateway
23vue: @tanstack/ai-llmgateway
24solid: @tanstack/ai-llmgateway
25svelte: @tanstack/ai-llmgateway
26preact: @tanstack/ai-llmgateway
27angular: @tanstack/ai-llmgateway
28vanilla: @tanstack/ai-llmgateway
29octane: @tanstack/ai-llmgateway
30
31<!-- ::end:tabs -->
32
33## 2. Set the API key
34
35Get a key from the [LLM Gateway dashboard](https://llmgateway.io). Put it in the environment of your server:
36
37```bash
38LLM_GATEWAY_API_KEY=llmgtwy_your-api-key
39```
40
41`llmGatewayText` and `llmGatewaySummarize` read `LLM_GATEWAY_API_KEY`. To pass the key in code, use the `create*` factory of the same adapter:
42
43```typescript
44import { createLLMGatewayText } from "@tanstack/ai-llmgateway";
45
46const adapter = createLLMGatewayText("gpt-5.6-terra", process.env.LLM_GATEWAY_API_KEY!);
47```
48
49## 3. Stream a chat from the server
50
51```typescript
52import {
53 chat,
54 chatParamsFromRequest,
55 toServerSentEventsResponse,
56} from "@tanstack/ai";
57import { llmGatewayText } from "@tanstack/ai-llmgateway";
58
59export async function POST(request: Request) {
60 const { messages, threadId, runId } = await chatParamsFromRequest(request);
61
62 const stream = chat({
63 adapter: llmGatewayText("gpt-5.6-terra"),
64 messages,
65 threadId,
66 runId,
67 });
68
69 return toServerSentEventsResponse(stream);
70}
71```
72
73## 4. Render the chat
74
75The client is the same for every provider. Point `useChat` at your route:
76
77```tsx
78import { useState } from "react";
79import { useChat, fetchServerSentEvents } from "@tanstack/ai-react";
80
81export function Chat() {
82 const [input, setInput] = useState("");
83 const { messages, sendMessage } = useChat({
84 connection: fetchServerSentEvents("/api/chat"),
85 threadId: "support-chat",
86 });
87
88 return (
89 <form
90 onSubmit={(event) => {
91 event.preventDefault();
92 sendMessage(input);
93 setInput("");
94 }}
95 >
96 {messages.map((message) => (
97 <p key={message.id}>
98 {message.parts
99 .map((part) => (part.type === "text" ? part.content : ""))
100 .join("")}
101 </p>
102 ))}
103 <input value={input} onChange={(event) => setInput(event.target.value)} />
104 </form>
105 );
106}
107```
108
109For Vue, Solid, Svelte, Angular, and the other frameworks, see [Quick Start](../getting-started/quick-start#3-render-the-chat).
110
111You now have a streaming chat through LLM Gateway. To try another model, change the id, for example to `claude-opus-5` or `kimi-k3`.
112
113## Pick a model and a provider
114
115Any model on [llmgateway.io/models](https://llmgateway.io/models) works. Pass its id as the model name:
116
117- A bare id lets the gateway pick the best provider that is available.
118- A `provider/` prefix always sends the request to that provider.
119
120```text
121model: "gpt-5.6-terra" // the gateway picks the provider
122model: "claude-sonnet-5" // the gateway picks the provider
123model: "moonshot/kimi-k3" // always sent to Moonshot
124model: "fireworks/kimi-k3" // always sent to Fireworks
125```
126
127## Tools
128
129Tools work the same as with every adapter:
130
131```typescript
132import {
133 chat,
134 chatParamsFromRequest,
135 toServerSentEventsResponse,
136 toolDefinition,
137} from "@tanstack/ai";
138import { llmGatewayText } from "@tanstack/ai-llmgateway";
139import { z } from "zod";
140
141const getWeatherDef = toolDefinition({
142 name: "get_weather",
143 description: "Get the current weather",
144 inputSchema: z.object({
145 location: z.string(),
146 }),
147});
148
149const getWeather = getWeatherDef.server(async ({ location }) => {
150 return { location, temperature: 72, conditions: "sunny" };
151});
152
153export async function POST(request: Request) {
154 const { messages, threadId, runId } = await chatParamsFromRequest(request);
155
156 const stream = chat({
157 adapter: llmGatewayText("gpt-5.6-terra"),
158 messages,
159 threadId,
160 runId,
161 tools: [getWeather],
162 });
163
164 return toServerSentEventsResponse(stream);
165}
166```
167
168See [Tools](../tools/tools).
169
170## Summarization
171
172```typescript
173import { summarize } from "@tanstack/ai";
174import { llmGatewaySummarize } from "@tanstack/ai-llmgateway";
175
176const result = await summarize({
177 adapter: llmGatewaySummarize("gpt-5.4-mini"),
178 text: "Long article text...",
179 threadId: "thread-1",
180 stream: false,
181});
182
183console.log(result.summary);
184```
185
186## Models and options
187
188### Model options
189
190The gateway takes the standard Chat Completions parameters and sends them to the provider that it picks. The gateway removes the parameters that a provider does not support. Sampling options go in `modelOptions`:
191
192```typescript
193import { chat } from "@tanstack/ai";
194import { llmGatewayText } from "@tanstack/ai-llmgateway";
195
196const stream = chat({
197 adapter: llmGatewayText("kimi-k3"),
198 messages: [{ role: "user", content: "Hello!" }],
199 threadId: "thread-1",
200 modelOptions: {
201 temperature: 0.7,
202 max_completion_tokens: 4096,
203 },
204});
205```
206
207### Reasoning
208
209Set `reasoning` on `chat()`:
210
211```typescript
212import { chat } from "@tanstack/ai";
213import { llmGatewayText } from "@tanstack/ai-llmgateway";
214
215const stream = chat({
216 adapter: llmGatewayText("kimi-k3"),
217 messages: [{ role: "user", content: "Plan a database migration." }],
218 threadId: "thread-1",
219 reasoning: "high",
220});
221```
222
223It goes out as `reasoning_effort`. The types take the levels of each model, and no `budgetTokens`:
224
225| Model | Levels |
226| --- | --- |
227| `kimi-k3`, `glm-5.2`, `deepseek-v4-pro`, `qwen3.7-max` | `off` to `max` |
228| `gpt-5.6-terra` | `off`, `low` to `max` |
229| `gpt-5.5`, `gpt-5.4-mini` | `off`, `low` to `xhigh` |
230| `claude-opus-5`, `claude-sonnet-5`, `claude-haiku-4-5`, `minimax-m2.5` | `low` to `max` |
231| `gemini-pro-latest`, `grok-4-5` | `low` to `high` |
232| `gemini-3.6-flash` | `minimal` to `high` |
233
234Reasoning models stream their thinking as `reasoning_content` deltas. The adapter turns them into AG-UI `REASONING_*` events. See [Reasoning](../chat/reasoning).
235
236### Models
237
238`LLMGATEWAY_CHAT_MODELS` is a curated set of flagship models. These models have type data for their input kinds and provider options, with autocomplete in the editor. Other ids still work. They get text-only input and the generic options.
239
240`adapter.inputModalities` lists the input kinds of the selected model. It is `undefined` for a model that this package does not know. See [Check what a model accepts](../advanced/extend-adapter#check-what-a-model-accepts).
241
242## Advanced
243
244### Self-hosted gateway
245
246To use your own deployment, set `baseURL`:
247
248```typescript
249import { createLLMGatewayText } from "@tanstack/ai-llmgateway";
250
251const adapter = createLLMGatewayText("gpt-5.6-terra", process.env.LLM_GATEWAY_API_KEY!, {
252 baseURL: "https://api.llmgateway.io/v1", // the hosted default. Set your own URL here.
253});
254```
255
256The adapter works the same against a self-hosted gateway.
257
258Need a header on each request, or a log of each model call? This adapter supports [`wrapFetch`](../advanced/middleware#change-the-http-requests-of-a-call).
259
260### API reference
261
262| Factories | Creates |
263| --- | --- |
264| `llmGatewayText(model, config?)` / `createLLMGatewayText(model, apiKey, config?)` | Chat adapter |
265| `llmGatewaySummarize(model, config?)` / `createLLMGatewaySummarize(model, apiKey, config?)` | Summarization adapter |
266
267The short factory reads `LLM_GATEWAY_API_KEY`. The `create*` factory takes an explicit API key.