# Consumers and Keys

URL: https://www.elchi.io/docs/ai-gateway/consumers-and-keys

> Consumers of keyed gateways, their elchi virtual keys, the headers a key is accepted in, what each consumer may use, and what a missing, unknown or revoked key gets.

A **consumer** is who uses the gateways that **require a key**: an application, a team or a person. It has one or more **elchi virtual keys** (`sk-elchi-…`), lists of what it may use, a quota, and MCP tool grants. Requests are counted, priced and logged per consumer.

Consumers apply **only to gateways with Require a key on** ([Gateways and models](https://www.elchi.io/docs/ai-gateway/gateways-and-models#require-a-key)). A gateway without it takes no key: anyone who reaches the listener may use it, and its requests belong to no consumer.

The provider never sees a consumer's key. The gateway checks the key, removes it, and the provider gets the provider's own credential.

![A consumer's page: access, quota, keys and use](https://www.elchi.io/docs/img/docs/aigw-consumer-detail-light.webp)

## Create a consumer and its first key

Only Owners and Admins create consumers and keys. A consumer needs the project's AI gateway configuration (at least one provider).

1. Open **Infrastructure → AI Gateway → Consumers** and click **New consumer**.
2. **Consumer** section:
   - **Name** — lower-case letters, digits and hyphens, at most 63 characters, unique in the project; `new` is reserved. It is the `consumer` of usage rows and request-log rows. It cannot be changed later.
   - **Disabled** — every key of the consumer gets 401; its usage and keys stay.
3. **Access** section (empty allows all; see [What a consumer may use](#what-a-consumer-may-use)): **Gateways**, **Providers**, **Models**.
4. **MCP tools** section — tool grants and MCP call quotas, see [MCP grants and quotas](https://www.elchi.io/docs/ai-gateway/mcp/grants-and-quotas).
5. **Quota** section — see [Quotas, limits and costs](https://www.elchi.io/docs/ai-gateway/quotas-limits-and-costs#consumer-quotas).
6. Click **Save**. The page reloads as the consumer's page.
7. In **Keys**, click **New key**. A dialog shows the key **once**: `Copy it now: it is shown only this once.` Copy it and click **I have copied it**. The dialog cannot be closed any other way.

The key works on the edges **once the deploy that this creation queued completes** (usually seconds; see [Operations](https://www.elchi.io/docs/ai-gateway/operations)).

When no gateway requires a key yet, the **Access** section says `No gateway requires a key yet` and links the gateways where you can turn **Require a key** on.

## Keys

| Property | Detail |
|---|---|
| Format | `sk-elchi-` followed by 43 characters (32 random bytes, base64url). |
| Stored | elchi keeps only `sha256:<hex>` of the key. Edges receive only the hashes (`00-consumers.yaml`). A lost key cannot be shown again: create another and revoke the old one. |
| Listed as | Its prefix: `sk-elchi-` and the next 4 characters (`sk-elchi-AbCd…`), with when and by whom it was created. |
| Several keys | A consumer may have any number of keys. Rotate without downtime: create a new key, move the application to it, revoke the old one. |
| One owner | A key belongs to one consumer. |
| Revoke | The delete icon next to a key, then **Revoke**. The edges refuse it (**401**) once the configuration without it is deployed. A revoked key cannot be restored. |

A project's whole configuration (every consumer's key hashes included) must fit in 3 MiB on the edge; a save that makes it larger is refused with `the configuration is N bytes and an edge takes at most … : fewer consumers, keys or gateways`.

## Sending the key

The gateway reads the key from the first of these headers that carries one:

| Header | Used by |
|---|---|
| `Authorization: Bearer sk-elchi-…` | OpenAI SDKs, LangChain, curl, most clients. `Bearer` is matched case-insensitively. |
| `x-api-key: sk-elchi-…` | Anthropic SDKs and Claude Code. |
| `api-key: sk-elchi-…` | Azure OpenAI SDKs. |

On an admitted request the gateway removes `authorization`, `x-api-key`, `api-key` and `x-goog-api-key` before the request goes on: the provider gets only its own credential.

```bash
curl https://llm.example.com/v1/chat/completions \
  -H "Authorization: Bearer sk-elchi-..." \
  -H "Content-Type: application/json" \
  -d '{"model":"gpt-4o-mini","messages":[{"role":"user","content":"hello"}]}'
```

SDK and agent examples: [Calling the gateway](https://www.elchi.io/docs/ai-gateway/calling-the-gateway).

## What a consumer may use

| Field | Empty means | When set |
|---|---|---|
| **Gateways** | every gateway that requires a key | only these gateways. The list offers only keyed gateways; a gateway without a key shows `(requires no key)` and cannot be chosen. |
| **Providers** | every provider | only models served by these providers. A model served by several providers is allowed only when **every** provider of it is in the list. |
| **Models** | every model of the allowed gateways | only these model names, matched **exactly** (no wildcards). |

A request outside these lists gets **403** and reaches no provider. It uses no quota.

| Request | Answer |
|---|---|
| A gateway not in **Gateways** | **403** `gateway_not_allowed` — `consumer billing may not use gateway team-a` |
| A model or provider not allowed | **403** `model_not_allowed` — `consumer billing may not use model gpt-4o (provider openai)` |
| The consumer has a cost quota and the model has no price | **403** `model_not_priced` |
| The Files API on a keyed gateway | **403** `files_not_allowed` |

`/v1/models` still lists every model of the gateway, not only the consumer's.

The configuration page shows notes for settings that are accepted but likely wrong:

- `gateway "x" requires a key but no enabled consumer with a key may use it: every request gets 401`
- `consumer "x" lists gateway "y", which requires no key: anyone may use it, and its use is not counted for the consumer`
- `consumer "x" lists model "y", which no gateway requiring a key that it may use serves (names match exactly): the entry allows nothing`
- `consumer "x" may not use provider "y" of model "z" (gateway "g"): every provider of the model must be allowed, so the model is refused for it (403 model_not_allowed)`
- `consumer "x" has a cost quota but spec.pricing is empty: its cost stays 0`

## Missing, unknown, revoked and disabled keys

All of these get the same answer, in OpenAI's error shape, and reach no provider:

```
HTTP/1.1 401
content-type: application/json

{"error":{"message":"a valid elchi virtual key is required for gateway team-a","type":"authentication_error","code":"invalid_api_key"}}
```

- no key header at all,
- a key that is no consumer's (a typo, another project's key, a provider key sent by mistake),
- a revoked key (after the deploy),
- any key of a **Disabled** consumer.

A consumer that is over a quota gets **429** `quota_exceeded` instead ([Quotas, limits and costs](https://www.elchi.io/docs/ai-gateway/quotas-limits-and-costs)).

How denials are recorded:

- Every denial of a **known** consumer (403, 429) is written to the decision log (`elchi_ai_gateway_decisions`) with the consumer, gateway, model and code, and logged on the edge at info level at most once a minute per consumer and code.
- A denial that names **no** consumer (401 `invalid_api_key`, 503 `gateway_not_ready`, 404 `not_found`) is written at most 5 a second per gateway and code (bursts of 20); the rest are counted in `elchi_ai_gateway_decision_rows_suppressed_total{gateway, code}`. A client looping on a wrong key cannot fill the table.
- Every decision is counted in `elchi_ai_gateway_authz_decisions_total{result, gateway, code}` on the edge.

## The x-elchi-consumer header

When the gateway admits a keyed request, it sets `x-elchi-consumer: <consumer name>` on it. The engine labels the request's token usage with it, and the request log records it.

- A client cannot set it: every gateway removes a client's `x-elchi-consumer` before the authorization runs, so no one can bill another consumer, also on gateways that require no key.
- It never reaches the provider: Envoy removes it (and `x-elchi-ai-gateway`) just before the request leaves for the provider.

## Per-consumer usage

- The **Consumers** list has a **Quota used** bar per consumer: the tightest of its quotas in percent; hover it for every quota.
- The consumer's page has a **Use** section: **Requests today**, **Tokens today**, **Tokens this month**, and with prices **Cost today** and **Cost this month**, each against its limit. Days start at 00:00 UTC, months on the 1st; tokens are input plus output. Tokens of models without a price are counted separately (`N tokens this month are of models without a price: they are not in the cost.`).
- The **Usage** tab filters by consumer and breaks usage down by consumer; requests of gateways that require no key are grouped as **no key**. See [Usage and costs](https://www.elchi.io/docs/ai-gateway/usage-and-costs).
- The **Request log** filters by consumer: [Request log](https://www.elchi.io/docs/ai-gateway/request-log).

## Delete a consumer

**Delete** on the consumer's page or the delete icon in the list: `Its keys stop working on every edge with the deploy. Its usage history stays.` Every key of the consumer goes with it.

## REST API

| Method and path | Body / query |
|---|---|
| `GET /api/v3/ai-gateway/consumers?project=` | Every consumer, keys listed by `id` and `prefix` only. |
| `GET /api/v3/ai-gateway/consumers/:name?project=` | One consumer. |
| `PUT /api/v3/ai-gateway/consumers/:name` | `{project, consumer, expected_version, create}` |
| `DELETE /api/v3/ai-gateway/consumers/:name?project=` | The consumer and all its keys. |
| `POST /api/v3/ai-gateway/consumers/:name/keys` | `{project}` → the new key, shown this once. |
| `DELETE /api/v3/ai-gateway/consumers/:name/keys/:key_id?project=` | Revokes one key. |

`consumer` is `{name, gateways?, providers?, models?, quota?, disabled?, mcp?}`; keys sent in it are ignored (they change only through the keys endpoints). `create: true` refuses a name already used (**409**); a stale `expected_version` is **409**.

```bash
# create the consumer
curl -s -X PUT "$ELCHI/api/v3/ai-gateway/consumers/billing" \
  -H "Authorization: Bearer $TOKEN" -H "Content-Type: application/json" \
  -d '{"project":"'"$PROJECT"'","create":true,
       "consumer":{"name":"billing","gateways":["team-a"],"models":["gpt-4o-mini"],
                   "quota":{"requestsPerMinute":60,"tokensPerDay":2000000}}}'

# give it a key (the answer is the only place the key appears)
curl -s -X POST "$ELCHI/api/v3/ai-gateway/consumers/billing/keys" \
  -H "Authorization: Bearer $TOKEN" -H "Content-Type: application/json" \
  -d '{"project":"'"$PROJECT"'"}'
# {"data":{"id":"…","prefix":"sk-elchi-AbCd","created_at":"…","created_by":"…","key":"sk-elchi-…"},"deploy":{"enqueued":true,…}}

# revoke it
curl -s -X DELETE "$ELCHI/api/v3/ai-gateway/consumers/billing/keys/<key_id>?project=$PROJECT" \
  -H "Authorization: Bearer $TOKEN"
```

## Verify

```bash
# without a key: 401 invalid_api_key
curl -s https://llm.example.com/v1/models

# with the consumer's key: the gateway's model list
curl -s https://llm.example.com/v1/models -H "Authorization: Bearer sk-elchi-..."
```

- A new key still gets 401: the deploy has not reached the edge yet, or failed there ([Operations](https://www.elchi.io/docs/ai-gateway/operations)); or the consumer is disabled; or the gateway is not in its **Gateways**.
- 403 `model_not_allowed` for a model you expected: check **Providers** (every provider of a multi-provider model must be allowed) and **Models** (exact names).
