# AI Gateway

> The AI Gateway is a proxy layer between applications and AI model providers that adds routing, caching and monitoring.

## What is an AI gateway and why use one?

The proxy that sits between an application and model APIs and provides a single interface to many providers. Adds routing, fallbacks, caching, rate limiting, cost tracking and logging.

- Topic: APIs & Integrations
- Type: tool

## Example

Wrong:

```js
const res = await fetch(PROVIDER_URL, {
  headers: { Authorization: 'Bearer ' + process.env.PROVIDER_KEY },
  body: JSON.stringify({ model: 'model-a', messages }),
});
```

Right:

```js
const res = await fetch(process.env.GATEWAY_URL, {
  headers: { Authorization: 'Bearer ' + process.env.GATEWAY_APP_KEY },
  body: JSON.stringify({ model: 'chat-default', messages }),
});
```

The app calls the gateway with its own key and a model alias, so provider keys and fallback rules live in one place.

## Common mistakes

- Caching responses for prompts that include user-specific data, leaking answers between users.

## Key points

- Provider API keys live only in the gateway, and apps authenticate to the gateway with their own keys.
- Fallback rules name which model to try next and on which errors, such as timeouts or '429'.
- Logs store request metadata and redact secrets and personal data from prompts.

## Interview questions

### AI gateway vs API gateway: what is the difference?

An AI gateway is an API gateway specialized for model traffic: it understands tokens, model fallbacks, prompt caching and per-model costs.

### Does an AI gateway add latency?

A little, since every request goes through one more hop. It is usually small compared to model response time and can be offset by caching.

### Can I build my own AI gateway?

Yes, a simple one is a server that holds provider keys, forwards requests and logs usage. Open-source gateways add routing and fallbacks ready-made.

## Related cards

- [Model Routing](https://wonderium.dev/ai/model-routing.md)
- [Rate Limits](https://wonderium.dev/ai/rate-limits.md)
- [AI Observability](https://wonderium.dev/ai/ai-observability.md)
- [Proxy](https://wonderium.dev/web/proxy.md)
- [LLM API](https://wonderium.dev/ai/llm-api.md)

---

- Level: [Artificial Intelligence](https://wonderium.dev/ai.md)
- HTML version: https://wonderium.dev/ai/ai-gateway
- Last updated: 2026-11-25
