# AI Gateways

One object knows where AI requests go and how to sign them. Agents, Decisions, and your routes share it.

```ts [server/api/answer.get.ts]
import { generateText } from 'ai'
import { defineEventHandler } from 'h3'
import { openrouter } from 'vite-hub/gateways'
import { languageModel } from 'vite-hub/gateways/ai-sdk'

// Reads OPENROUTER_API_KEY when it sends the request
const model = languageModel(openrouter(), 'google/gemini-3-flash')

export default defineEventHandler(async () => {
  const { text } = await generateText({ model, prompt: 'What is a Server Primitive?' })
  return { text }
})
```

```ts [server/agents/review/agent.ts]
import { codexDriver, defineAgent } from 'vite-hub/agent'
import { cliproxy } from 'vite-hub/gateways'

// Codex sends its model requests through your CLIProxyAPI
export default defineAgent({
  driver: codexDriver({ model: 'gpt-5.5', gateway: cliproxy() }),
})
```

```ts [server/utils/label.ts]
import { ask, typesafe } from 'vite-hub/gateways'

// Reads TYPESAFE_API_KEY. The answer types come from the questions.
export function label(transaction: string) {
  return typesafe().ask(transaction, {
    category: ask.choice('Which category fits this bank transaction?', {
      groceries: 'Food and household shopping.',
      eatingOut: 'Restaurants and cafes.',
    }),
    recurring: ask.if('Is it a recurring payment?'),
  })
}
```

### [Use it with the AI SDK](https://vitehub.dev/docs/gateways/get-started#send-a-prompt)

`languageModel()` returns an AI SDK model that sends its requests through the gateway.

### [Route Codex and Claude Code](https://vitehub.dev/docs/gateways/agents)

Set `driver.gateway`, and the Agent Driver sends its model requests through your proxy.

### [Typed data from one call](https://vitehub.dev/docs/gateways/server-api#get-typed-data-from-a-model)

`ask(prompt, { model, schema })` returns data that your schema checked, without an Agent. Without `schema`, it returns text.

### [Ask typed questions](https://vitehub.dev/docs/gateways/server-api#ask-questions)

`ask()` sends questions to TypeSafe Jev, and TypeScript infers each answer type.

### [A preset for each provider](https://vitehub.dev/docs/gateways/configure#presets)

Vercel, Cloudflare, OpenRouter, LiteLLM, CLIProxyAPI, Ollama, OpenAI, Anthropic, and TypeSafe. The type tells what each one serves.

### [Keys stay on the server](https://vitehub.dev/docs/gateways/configure#credentials)

A preset reads its key for each request, so a rotated key applies at once.

### [No framework code](https://vitehub.dev/docs/gateways/server-api)

The package has no Agent, Vite, or Nitro code. It also runs in a plain Node script or a Worker.

Use AI Gateways when server code sends model requests or asks typed questions. For one model call that returns data in a fixed shape, use `ask(prompt, { model, schema })` with `languageModel()`, not an Agent. For one call that returns text, use `ask(prompt, { model })`, not `generateText()`. Import presets from `vite-hub/gateways` and `languageModel()` from `vite-hub/gateways/ai-sdk`. Install `@vite-hub/gateways`, plus `advocaat` for `ask()` and the AI SDK provider package for `languageModel()`.

Pick a preset whose type serves the use: a Codex or Claude Code URL for a Driver, an OpenAI-compatible URL for `languageModel()`, and `ask: true` for `ask()`. Never import a gateway in browser code. Keys are read on the server for each request.

Prove it with a real request and a real key. Without a key, show that the request fails with `GATEWAYS_R0002` and the variable that it names.

<!-- vitehub-docs:section-links -->
## In this section

- [Overview](https://vitehub.dev/raw/docs/gateways.md) (this page)
- [Get started](https://vitehub.dev/raw/docs/gateways/get-started.md)
- [Configure](https://vitehub.dev/raw/docs/gateways/configure.md)
- [Server API](https://vitehub.dev/raw/docs/gateways/server-api.md)
- [Limits and errors](https://vitehub.dev/raw/docs/gateways/limits-and-errors.md)
- [Use with Agents](https://vitehub.dev/raw/docs/gateways/agents.md)

Next: [GATEWAYS_R0001](https://vitehub.dev/raw/docs/gateways/diagnostics/gateways_r0001.md)

All pages: [llms.txt](https://vitehub.dev/llms.txt)
