# AI Listeners

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

> Creating the Envoy listeners that serve the AI gateways — HTTPS and plain HTTP entries, the 308 redirect, binding gateways and a default gateway, deploying, live changes and removal.

An **AI listener** is where gateways are served: an Envoy listener that elchi builds and manages for the AI gateway. It has

- an **HTTPS** entry (TLS terminated with a Downstream TLS context you made) and/or a **plain HTTP** entry that serves the gateways or redirects to HTTPS,
- the **gateways it serves**, selected by the request's `Host`, and optionally a **default gateway** for hosts none of them claims,
- its own generated route (`elchi-aigw-l-<listener>`) that holds only its gateways: a `Host` is never a way into a gateway another listener serves.

Clients connect to the listener's address on the edges it is deployed on. The AI filters in the listener's HTTP connection manager hand every request to the `elchi-ai-gateway` sidecar on that edge (one per edge, shared by every AI listener there).

![An AI listener's page: ports, TLS context, gateways and where it runs](https://www.elchi.io/docs/img/docs/aigw-listener-detail-light.webp)

## Before you start

- At least one provider exists (**New listener** is disabled until then) and the gateways you want to serve exist.
- For HTTPS: a **Downstream TLS context** of the same Envoy version, made under **Building blocks → Transport Socket** (Downstream TLS context). Its certificates should cover the hosts of every gateway the listener serves. The listener only refers to it: it is never made, changed or removed with the listener, and a TLS context elchi generated is refused.
- The Envoy version you choose must have the AI gateway's default resources. The **Listeners** tab lists, per version, the defaults the project needs and what the deploys generated; a version with missing defaults is disabled in the form (`<version> — N defaults missing`) and no AI listener can be created on it until they are restored.
- The edges you deploy it on run `elchi-ai-gateway` ([Installation](https://www.elchi.io/docs/ai-gateway/installation)).

## Create a listener in the UI

1. Open **Infrastructure → AI Gateway → Listeners** and click **New listener**.
2. **Listener** section:
   - **Name** — lower-case letters, digits and `-`, at most 48 characters; may not end in `-mcp` or `-redirect`; `new` is reserved. It also names the listener's service and bootstrap. It cannot be changed later.
   - **Envoy version** — the newest version whose defaults are all present is preselected. It cannot be changed later on this form.
3. **HTTPS** (switch) — **Port** and **Downstream TLS context**. When the version has no Downstream TLS context yet, the field says so and links to **Transport Socket**.
4. **Plain HTTP** (switch, on for a new listener) — **Port** and **Requests**: **Serve the gateways** or **Redirect to HTTPS** (only with HTTPS on).
5. **Gateways** — a table of the project's gateways: tick **Serve** for each gateway this listener serves, and pick at most one **Default**. A gateway without hosts shows `none: only as the default`. **No default** clears the default.
6. Click **Create**.

Create makes everything the listener needs in one step: the stored listener configuration, its route, its HTTP connection manager(s) with the AI filters, and the managed Envoy listener with its bootstrap and service. **It does not deploy.** After **Create**, the listener's page opens with the deploy dialog: choose the clients to deploy it on.

What the form refuses before it saves, and what the API refuses the same way:

| Problem | Message |
|---|---|
| Neither entry on | `Serve HTTPS, plain HTTP or both.` |
| Both entries on one port | `HTTPS and plain HTTP need ports of their own.` |
| Port 10088 | `Port 10088 is the AI listeners' own MCP egress (on loopback): choose another.` |
| Redirect without HTTPS | `Plain HTTP can redirect only to this listener's HTTPS: turn HTTPS on.` |
| A served gateway without hosts that is not the default | `Gateway <name> has no hosts: it is reachable here only as the default gateway.` |
| A default that is not served | `The default gateway must be one this listener serves.` |
| HTTPS without a TLS context, or one not found on the version | `HTTPS needs a Downstream TLS context` / `Downstream TLS context "x" not found on Envoy <version>` |
| A name already used by a listener, service, bootstrap or HCM | **409**, naming the conflicting documents |

## HTTPS and plain HTTP

Both entries belong to **one** listener because one address serves one listener (service) of a project: deploying a second listener of the project on the address of one already deployed there is refused (**400**). The listener's Envoy document has an entry per port: `<listener>-https` (first) and `<listener>-http`.

| Plain HTTP **Requests** | What clients get |
|---|---|
| **Serve the gateways** | The gateways over plain HTTP, exactly as over HTTPS. |
| **Redirect to HTTPS** | **308 Permanent Redirect** to the listener's HTTPS port for every request. The method and body are kept (a POST stays a POST), the path and query are kept, and the `Location` names the HTTPS port explicitly. No AI filter runs before the redirect: nothing is buffered and no key is checked. |

The redirect is for people and tools that follow redirects. A key sent over plain HTTP has already crossed the network in clear before the redirect, and most LLM SDKs do not follow redirects. For keyed gateways prefer an HTTPS-only listener.

The listener page warns (**Worth a look**):

- `gateway <name> requires a key and is served over plain HTTP too: keys cross the network in clear`
- `plain HTTP redirects to HTTPS: a key sent over HTTP has crossed the network in clear before the redirect, and most LLM SDKs do not follow redirects`
- `TLS context <ctx> has no certificate for <host> (gateway <name>): clients that verify it refuse the connection` (checked when the context's certificates are inline or in TLS secrets elchi holds)
- `serves no gateway: every request gets 404`

## Gateways, default gateway and unclaimed hosts {#default-gateway-and-unclaimed-hosts}

The listener's route has one virtual host per served gateway, with that gateway's hosts as its domains. For a request on this listener:

| `Host` | Goes to |
|---|---|
| A host one of its served gateways claims (exact or wildcard) | That gateway. |
| Any other host — another listener's gateway host, an IP address, an unknown name | The listener's **default gateway**. With no default: **404**. |

The 404 of an unclaimed host comes from Envoy, with every AI filter off (nothing is buffered, no key is asked):

```json
{"error":{"message":"no AI gateway serves this host on listener ai-edge","type":"invalid_request_error","code":"gateway_not_found"}}
```

A gateway without hosts can be served only as a listener's default. The same gateway may be served by several listeners, as the default of some and by host on others.

Envoy ignores the port in `Host`, strips a trailing dot and matches case-insensitively.

## Deploy, change, undeploy, remove

**Deploy.** The listener is a managed listener: each client it is deployed on serves it on that client's address. Deploy from the dialog after **Create**, from the **Deploy** link in the **Deployed** column (when it runs nowhere yet), or from the listener page's **Deploy** button. To add more clients to a deployed listener, use its service page (**Service** link). The list's **Served on** column and the page's **Where it runs** section show the URLs, for example `https://10.0.0.5:443` and `http://10.0.0.5:80`.

**Change.** Open the listener, change ports, TLS context, HTTP mode, the served gateways or the default, and **Save**. On a deployed listener the change reaches its clients **live, without a restart** (`Its N clients get the change now, without a restart.`). Changing only the gateways or the default changes the listener's route alone. The name and the Envoy version do not change: `listener x runs Envoy v1.39.4; its version does not change (make a listener on the other version)`. A save made on a stale revision answers **409**; the page offers to load the stored values.

**Undeploy.** From the listener's service page (the **Service** link).

**Remove.** Only an **undeployed** listener can be removed (**Remove** in the list, or on its page). A deployed one answers **409** `the listener is deployed: undeploy it from its clients first`. Removing takes away the listener, its service and bootstrap, its HTTP connection managers and its route. The Downstream TLS context you chose, the gateways and the rest of the AI gateway configuration stay.

When a listener was moved to another Envoy version with the listener upgrade, or deleted from the generic Listeners page, the AI HCM it used is left behind; the **Listeners** tab lists such HCMs and they are removed with the AI gateway (or open one to delete it).

## What the listener's connection manager serves

The AI listener's HTTP connection manager (`<listener>-hcm`) routes **only** from the listener's generated route. Its documents belong to the AI gateway: the generic resource pages refuse to change or delete them. Do not plan to add your own routes to an AI listener; give LLM traffic listeners of its own.

If you turn the AI gateway toggle on in an HTTP connection manager of your own, every route of it that is not a gateway's is answered by the AI gateway's authorization with **404** `not_found` (`this route is not served by an AI gateway`).

## Request size and timeouts

- A gateway's request body may be up to **50 MiB** (the AI filters buffer the whole request); a larger one gets **413** from Envoy.
- Model routes have no overall timeout (answers stream) and an idle timeout of **15 minutes**: an answer silent that long is cut.
- Envoy's own header limit applies: a request with about 60 KiB of headers is closed by Envoy.

## The MCP egress entry

When the project has MCP servers (and every edge of the project runs a gateway version that supports MCP), each AI listener gets a third entry, `<listener>-mcp`, on **`127.0.0.1:10088`**: the loopback egress through which the engine calls MCP servers. It accepts only loopback sources (`127.0.0.0/8`), and the installer's firewall lets only the `elchi` group connect to it and only Envoy's user answer on it. That is why port 10088 is refused for a listener's own ports. The entry is added and removed automatically with the project's MCP servers. See [MCP overview](https://www.elchi.io/docs/ai-gateway/mcp/overview).

## REST API

| Method and path | Body / query |
|---|---|
| `GET /api/v3/ai-gateway/listeners?project=` | Every AI listener (configuration, `revision`, `addresses`, `clients`, `service_id`, `warnings`) and `leftovers` (unused AI HCMs). |
| `GET /api/v3/ai-gateway/listeners/:name?project=` | One listener. |
| `POST /api/v3/ai-gateway/listeners` | `{project, name, version, https?: {port, tls_context}, http?: {port, mode}, gateways, default?}`. Does not deploy. |
| `PUT /api/v3/ai-gateway/listeners/:name` | `{project, expected_revision, https?, http?, gateways, default?}`. Live on deployed clients. |
| `DELETE /api/v3/ai-gateway/listeners/:name?project=&version=` | Undeployed listeners only. |
| `GET /api/v3/ai-gateway/resources?project=` | Per Envoy version, the defaults and the generated resources. |

`http.mode` is `serve` (default) or `redirect`. `expected_revision: 0` skips the revision check.

```bash
curl -s -X POST "$ELCHI/api/v3/ai-gateway/listeners" \
  -H "Authorization: Bearer $TOKEN" -H "Content-Type: application/json" \
  -d '{
    "project": "'"$PROJECT"'",
    "name": "ai-edge",
    "version": "v1.40.2",
    "https": {"port": 443, "tls_context": "llm-example-com"},
    "http": {"port": 80, "mode": "redirect"},
    "gateways": ["team-a", "public"],
    "default": "public"
  }'
```

The answer names what was made: `{"data": {"listener": "ai-edge", "hcm": "ai-edge-hcm", "redirect_hcm": "ai-edge-redirect-hcm", "tls": "llm-example-com", "route": "elchi-aigw-l-ai-edge", "version": "v1.40.2", "config": {…}, "warnings": […]}}`. Deploy it on clients as any managed listener's service.

## Verify

```bash
# HTTPS, the gateway selected by Host
curl -s https://llm.example.com/v1/models

# the same listener by address, with an explicit Host
curl -s --resolve llm.example.com:443:10.0.0.5 https://llm.example.com/v1/models

# plain HTTP in redirect mode
curl -si http://llm.example.com/v1/models | head -3   # HTTP/1.1 308 …, location: https://llm.example.com:443/v1/models
```

- `gateway_not_found` — the host is not claimed by a gateway this listener serves and it has no default.
- Connection refused — the listener is not deployed on that client, or the port is wrong (see **Where it runs**).
- A TLS error in the client — the TLS context has no certificate for the host (the page warns about it).
- More: [Troubleshooting](https://www.elchi.io/docs/ai-gateway/troubleshooting).
