# AI Gateway Overview

URL: https://www.elchi.io/docs/ai-gateway/overview

> Elchi AI Gateway puts one governed entry point for LLM providers and MCP tools on your edges: provider keys stay in the control plane, consumers get their own keys and quotas, and every request is measured.

Elchi AI Gateway is the LLM and MCP gateway of Elchi, a licensed add-on to Elchi Core. Applications call your edge, not the model providers. The edge picks a **gateway** by the request's `Host`, checks the caller's key and quotas, sends the request to the provider that serves the requested model with the provider's own credential, and records what happened.

![The AI Gateway page — gateways, providers, consumers and listeners of a project](https://www.elchi.io/docs/img/docs/aigw-gateways-light.webp)

You configure everything in the Elchi UI (**Infrastructure → AI Gateway**) or through the REST API under `/api/v3/ai-gateway`. Nothing is edited by hand on the edge.

## What it is

- **A project-scoped configuration** in the control plane: shared **providers** (credentials, endpoints, prices), any number of **gateways** (hosts, models, limits, request-log policy), **consumers** with virtual keys and quotas, MCP servers, a sensitive-data policy, and the **AI listeners** that serve the gateways.
- **Generated Envoy resources.** The control plane generates the clusters, routes and filters each AI listener needs, per Envoy version. You never write Envoy configuration for it.
- **An edge sidecar, `elchi-ai-gateway`.** One instance per edge (client) VM, shared by every AI listener on that VM. It supervises the engine, turns the delivered configuration into the engine's config bundle, answers the key and quota checks, scans for sensitive data, and exports usage and the request log to ClickHouse.
- **The engine: Agent Router extproc** (formerly Envoy AI Gateway). Elchi runs the upstream extproc binary as the sidecar's child process. It parses OpenAI- and Anthropic-style requests, extracts the model, translates the request to the provider's API, injects the provider credential and counts tokens.

## What it is not

- **The sidecar is not in the request path.** Envoy's `ext_proc` filters connect straight to the extproc's Unix socket (`/run/elchi-ai-gateway/extproc.sock`); the supervisor only manages that process. Key and quota checks go to the sidecar's own socket (`/run/elchi-ai-gateway/authz.sock`).
- **Not a reimplementation of the engine.** Request parsing, schema translation and token counting are the upstream extproc's. Elchi pins the version it renders for (v1.3.0).
- **Not a model host.** It routes to providers you configure: public APIs (OpenAI, Anthropic, Azure OpenAI, AWS, Google Cloud, Cohere, TypeSafe) or an OpenAI-compatible server of your own.
- **Not a WAF.** Shield ([Shield overview](https://www.elchi.io/docs/shield/overview)) and the AI gateway can run on the same HTTP connection manager; they do different jobs.

## The pieces

```mermaid
flowchart LR
  UI[Elchi UI / REST API<br/>/api/v3/ai-gateway] --> CP[Controller<br/>project configuration<br/>+ generated Envoy resources]
  CP -- xDS --> Envoy[Envoy on the edge<br/>AI listener]
  CP -- AI_GATEWAY deploy<br/>conf.d file set --> Agent[elchi-client<br/>agent]
  Agent -- validate, then write<br/>/etc/elchi/elchi-ai-gateway/conf.d --> Sup[elchi-ai-gateway<br/>supervisor]
  Sup -- starts, reloads --> Ext[Agent Router extproc]
  Envoy -- ext_proc<br/>extproc.sock --> Ext
  Envoy -- ext_authz, access log<br/>authz.sock --> Sup
  Envoy --> Prov[LLM providers]
  Envoy -- 127.0.0.1:10088<br/>MCP egress --> MCP[MCP servers]
  Sup -- usage, request log,<br/>decisions, MCP calls --> CH[(ClickHouse)]
  CH --> CP
```

1. **Control plane.** Stores the project's configuration in MongoDB (providers' credentials sealed), validates every change with the same rules the edge uses, and generates the Envoy resources for each Envoy version of the project.
2. **Agent courier.** Each change queues an `AI_GATEWAY_DEPLOY` job. The agent (`elchi-client`) on every connected edge that runs the AI gateway receives the file set (`00-providers.yaml`, `00-consumers.yaml`, `gw-<name>.yaml`), has the gateway validate it, writes it, and confirms the exact configuration version the gateway reports. See [Operations](https://www.elchi.io/docs/ai-gateway/operations).
3. **Edge sidecar.** Hot-reloads `conf.d`, renders the engine's config bundle, runs the extproc, serves the authorization socket, and keeps the last accepted configuration when a new one is refused.
4. **Envoy.** The AI listener's HTTP connection manager carries the AI filters; each provider is an `elchi-aigw-<provider>` cluster.
5. **ClickHouse.** Usage, the request log, authorization decisions, provider answers and MCP calls go there; the control plane reads them for the Usage, Request log and MCP calls tabs and for quotas shared across edges.

## One request through the filter chain

An AI listener's HTTP connection manager runs six AI filters before the router, in this order. Each provider cluster has its own upstream filter chain.

```mermaid
flowchart TB
  C([Client: POST /v1/chat/completions<br/>Host: llm.example.com<br/>Authorization: Bearer sk-elchi-…]) --> F1
  F1["1 elchi-ai-gateway-credential<br/>header_to_metadata: x-elchi-provider-key → metadata,<br/>header always removed"] --> F2
  F2["2 elchi-ai-gateway-header<br/>the matched gateway's virtual host sets<br/>x-elchi-ai-gateway = gateway name (overwrites)"] --> F3
  F3["3 elchi-ai-gateway-authz-key<br/>ext_authz on authz.sock: key → consumer,<br/>sets x-elchi-consumer (off on gateways without a key)"] --> F4
  F4["4 elchi-ai-gateway<br/>ext_proc on extproc.sock: reads the body,<br/>sets x-ai-eg-model"] --> F5
  F5["5 elchi-ai-gateway-authz-model<br/>ext_authz: model allowed? quotas, cost,<br/>sensitive-data scan of the body"] --> F6
  F6["6 elchi-ai-gateway-mcp-guard<br/>ext_proc: only on /mcp of a gateway whose<br/>policy scans MCP results; inert elsewhere"] --> R
  R["router: route per model (x-ai-eg-model)<br/>→ elchi-aigw-PROVIDER cluster"] --> U1
  U1["upstream ext_proc: translate to the provider's API,<br/>inject the provider credential"] --> U2
  U2["header_mutation: content-length"] --> U3["upstream_codec"] --> P([Provider])
```

- A `Host` no gateway of the listener claims goes to the listener's **default gateway**; without a default it gets `404` with code `gateway_not_found`.
- A model the gateway does not list gets `404` with code `model_not_found` and reaches no provider. `GET /v1/models` on the gateway's host lists the models it serves.
- Envoy answers `/v1/models` and `/anthropic/v1/models` itself from the gateway's model list.
- The engine's answer streams back through the same filters; the sidecar records the request log row from the engine's trace span and the serving provider from Envoy's access log.

Details of each refusal and its code: [Error codes](https://www.elchi.io/docs/ai-gateway/reference/error-codes).

## Features

| Feature | What it gives you | Page |
|---|---|---|
| Providers | Ten provider types, credentials sealed in the control plane, custom endpoints, a client's own key, request body changes | [Providers](https://www.elchi.io/docs/ai-gateway/providers) |
| Gateways and models | Host-selected gateways, the models each serves, several providers per model with weights, priority levels and failover | [Gateways and models](https://www.elchi.io/docs/ai-gateway/gateways-and-models) |
| AI listeners | HTTPS and/or plain HTTP on one listener, 308 redirect to HTTPS, gateways bound per listener, a default gateway | [Listeners](https://www.elchi.io/docs/ai-gateway/listeners) |
| Consumers and keys | Virtual keys (`sk-elchi-…`), allowlists of gateways, providers and models, disable without deleting | [Consumers and keys](https://www.elchi.io/docs/ai-gateway/consumers-and-keys) |
| Quotas, limits and costs | Token, request and cost quotas per consumer, CEL token limits, model prices | [Quotas, limits and costs](https://www.elchi.io/docs/ai-gateway/quotas-limits-and-costs) |
| Calling the gateway | Endpoints, auth headers, OpenAI and Anthropic SDKs, coding agents | [Calling the gateway](https://www.elchi.io/docs/ai-gateway/calling-the-gateway) |
| Sensitive data | Flag or block personal data and secrets in prompts and MCP traffic | [Sensitive data](https://www.elchi.io/docs/ai-gateway/sensitive-data) |
| Request log | One row per request; optional content capture, masked and encrypted on the edge | [Request log](https://www.elchi.io/docs/ai-gateway/request-log) |
| Usage and costs | Tokens, cost and provider health per gateway, provider, model and consumer | [Usage and costs](https://www.elchi.io/docs/ai-gateway/usage-and-costs) |
| MCP | Agents reach MCP servers' tools through the gateway with per-consumer grants, quotas and a call log | [MCP overview](https://www.elchi.io/docs/ai-gateway/mcp/overview) |
| Operations | Deploys, Sync, per-edge status, last-good, console commands | [Operations](https://www.elchi.io/docs/ai-gateway/operations) |
| Security model | Where secrets live, what each socket and port accepts | [Security model](https://www.elchi.io/docs/ai-gateway/security-model) |

## Supported provider types

| Type | Typical use |
|---|---|
| `openai` | OpenAI, and any OpenAI-compatible service or local server (set its host under Endpoint) |
| `anthropic` | The native Anthropic Messages API, for Anthropic SDK clients (`/anthropic/v1/messages`) |
| `azure-openai` | Azure OpenAI, with an API key or Microsoft Entra ID |
| `aws-bedrock`, `aws-anthropic`, `aws-openai` | Amazon Bedrock models, Claude on Bedrock, AWS's OpenAI-compatible endpoint |
| `gcp-vertexai`, `gcp-anthropic` | Google Vertex AI models, Claude on Vertex AI |
| `cohere` | Cohere rerank (`/cohere/v2/rerank`) |
| `typesafe` | TypeSafe SystemOne (`/typesafe/v1/systemone`) |

Which client API reaches which provider type is fixed by the engine. The common trap: `/v1/chat/completions` does not reach a native `anthropic` provider (`422 unsupported API schema`); OpenAI-format clients reach Claude through `aws-anthropic` or `gcp-anthropic`. The full matrix is on [Providers](https://www.elchi.io/docs/ai-gateway/providers) and [Calling the gateway](https://www.elchi.io/docs/ai-gateway/calling-the-gateway).

## What it needs

- **Elchi Core** with `ELCHI_JWT_SECRET` set on the controller. With it empty every AI gateway API call answers `503 AI gateway is disabled: ELCHI_JWT_SECRET is empty`.
- **An Owner or Admin** of the project to change the configuration. Members can read gateways, providers (credentials masked) and usage; only Owners and Admins see the Request log and MCP calls tabs and the per-edge status.
- **An edge with the sidecar.** A Linux amd64 edge (client) VM with `elchi-client` registered to the project and `elchi-ai-gateway` installed next to it. On Elchi OS 0.8.0 and later it ships in the image. See [Installation](https://www.elchi.io/docs/ai-gateway/installation).
- **ClickHouse for usage and the request log.** The edge writes usage, the request log, authorization decisions, provider answers and MCP calls to the ClickHouse its `config.yaml` names. Without one the gateway still serves traffic, but the control plane shows no usage, cost or request log for that edge, and a consumer's quotas count only what that edge saw since it started.

## Where to go next

- [Getting started](https://www.elchi.io/docs/ai-gateway/getting-started): a provider, a gateway, a key and a listener, and the first request.
- [Installation](https://www.elchi.io/docs/ai-gateway/installation): the sidecar on Elchi OS and on a plain Linux edge.
- [Operations](https://www.elchi.io/docs/ai-gateway/operations): how a change reaches the edges and how to check it did.
