For the complete documentation index, see llms.txt. Markdown versions of all docs pages are available by appending .md to any docs URL.
API key authentication
Authenticate requests using API keys with configurable validation modes.
Note
Agentgateway supports more than one configuration style. Where a feature can also be configured in the simplified llm or mcp modes, the examples on this page show each option in tabs. For more information, see Routing-based configuration.
API keyAPI KeyA secret token used to authenticate API requests. Agentgateway can validate API keys and attach metadata to authenticated requests. authenticationAuthentication (AuthN)The process of verifying the identity of a user or service. Agentgateway supports various authentication methods including JWT, API keys, and basic authentication. enables authenticating requests based on a user-provided API key.
Tip
This policy is about authenticating incoming requests. For attaching API keys to outgoing requests, see Backend authentication.
Agentgateway removes a validated API key before it forwards the request, unless the policy reads the key with a CEL expression. For LLM traffic, give clients virtual keys and keep provider keys on the gateway. See Virtual keys.
Configure API key authentication
API Key authentication involves configuring a list of valid API keys, with associated metadata about the key (optional).
Additionally, authentication can run in three different modes:
- Strict: A valid API key must be present.
- Optional (default): If an API key exists, validate it.
Warning: This allows requests without an API key! - Permissive: Requests are never rejected. This setting is useful for usage of claims in later steps such as authorization or logging.
Warning: This allows requests without an API key!
# yaml-language-server: $schema=https://agentgateway.dev/schema/config
llm:
policies:
apiKey:
mode: strict
keys:
- key: sk-testkey-1
metadata:
user: test
role: admin
models:
- name: "*"
provider: openAI
params:
apiKey: "$OPENAI_API_KEY"Later policies can now operate on the metadata associated with the API key. For example, you can set a custom x-authenticated-user header with the authenticated user from the API key metadata by adding a route-level transformation.
# yaml-language-server: $schema=https://agentgateway.dev/schema/config
llm:
policies:
apiKey:
mode: strict
keys:
- key: sk-testkey-1
metadata:
user: test
role: admin
transformations:
request:
set:
x-authenticated-user: apiKey.user
models:
- name: "*"
provider: openAI
params:
apiKey: "$OPENAI_API_KEY"Store a hash of the key
Use the keyHash field to keep API keys out of the configuration file. The keyHash field takes the SHA-256 digest of a key instead of the key itself. Write the digest as sha256: followed by the 64 hexadecimal characters of the digest. Clients still send the key itself. Agentgateway hashes the key that the client sends and compares the two digests, so a client that sends the digest as the key is rejected.
To compute the keyHash value of a key, run the following command. The command prints the digest with the sha256: prefix.
printf '%s' 'sk-testkey-1' | shasum -a 256 | awk '{print "sha256:" $1}'Example output:
sha256:ca7dd5b04082ec2bf02bd273b75030ef8318f57cf2faba1f56926c12145b3541Then, replace the key entry with a keyHash entry. A keyHash entry takes the same metadata as a key entry.
apiKey:
mode: strict
keys:
- keyHash: "sha256:ca7dd5b04082ec2bf02bd273b75030ef8318f57cf2faba1f56926c12145b3541"
metadata:
user: test
role: adminWhen agentgateway loads the configuration, agentgateway rejects a keyHash value in another format, such as a digest without the sha256: prefix. To find the error before you apply a change, run agentgateway --validate-only -f config.yaml.
Read the key from another location
By default, agentgateway reads the API key from the Authorization header. The key is the part of the header value after the Bearer prefix. After validating the key, agentgateway removes the whole Authorization header before forwarding the request. To read the key from another place, set the location field of the apiKey policy to exactly one of the following options.
| Option | Description |
|---|---|
header | Read the key from the header that name sets. To remove a prefix from the header value, such as Bearer , set prefix. The prefix comparison is case-insensitive. If the header value does not start with the prefix, agentgateway treats the request as a request without a key. Without prefix, the whole header value is the key. |
queryParameter | Read the key from the query parameter that name sets. |
cookie | Read the key from the cookie that name sets. |
expression | Read the key from the result of a CEL expression. Agentgateway does not remove a key that a CEL expression returns, as described in Remove a key that a CEL expression reads. |
When you set location, agentgateway reads the key only from the configured location and ignores a key in the Authorization header. After validating the key, agentgateway removes the key from the configured location before forwarding the request. The following example reads the key from the x-api-key header.
apiKey:
mode: strict
location:
header:
name: x-api-key
keys:
- key: sk-testkey-1Remove a key that a CEL expression reads
If the apiKey policy reads the key with a CEL expression location, agentgateway does not remove the key, and the backend receives the key. To remove the key, read it from a header location, or remove the header with a request transformation. For an example, see Credentials that a CEL expression reads.
LLM budgets and model access
To generate hashed LLM keys and manage their restrictions at runtime, see Set up virtual keys (agctl). The agctl standalone keys command manages the keys in llm.policies.apiKey.keys. An API key policy that you attach directly to an HTTP route or to the MCP configuration stays part of the resource that contains it.
An API key entry can carry two fields that apply to LLM traffic. The budgets field sets a per-key budget that caps what the key spends. The allowedModels field limits the top-level model that the key can request. Provider server-side tools, such as the Anthropic advisor tool, can use models outside the list. Both fields work with a key entry and with a keyHash entry.
# yaml-language-server: $schema=https://agentgateway.dev/schema/config
config:
database:
url: sqlite://budgets.db
llm:
policies:
apiKey:
mode: strict
keys:
- key: sk-testkey-1
metadata:
name: team-a
user: test
allowedModels:
- "gpt-5*"
- claude-sonnet-5
budgets:
- name: daily-spend
limit:
unit: USD
amount: 50
window:
rolling: 24h
onBudgetExceeded: Block
models:
- name: "*"
provider: openAI
params:
apiKey: "$OPENAI_API_KEY"| Setting | Description |
|---|---|
allowedModels | Model name patterns that can match the request’s top-level model. Each entry is an exact name or a pattern with one * wildcard. Omit the field to leave the key unconstrained, or set an empty list to deny every model. The field does not check models that provider server-side tools use. You cannot combine * with another pattern. |
budgets | List of budgets that are charged independently after each LLM response. |
budgets[].name | Names the budget within its key. The name must be unique among that key’s budgets. |
budgets[].limit.unit | USD to cap realized cost, or Tokens to cap token usage. |
budgets[].limit.amount | The maximum usage for the budget. A Tokens amount must be a whole number. A USD amount takes up to nine decimal places. |
budgets[].window | Optional rolling-window block. Omit this block when the budget must keep accumulating usage and never reset. |
budgets[].window.rolling | Length of the fixed usage window, such as 1h, 24h, or 30d. Windows are aligned to the Unix epoch rather than to the key’s first request. |
budgets[].onBudgetExceeded | Block to reject requests with a 429 after the limit is passed, or Audit to record the overage and allow the request. |
Two requirements apply to budgets only, and agentgateway refuses to start if either is unmet:
- The configuration must set
config.database.url, because agentgateway stores budget counts in a database. Settingconfig.logging.database.urlinstead does not satisfy this requirement, because that field configures request logging only. For more information, see Configuration storage. - Every key that has a budget must set
metadata.name, which identifies the key in budget counts, logs, and the admin API.
The allowedModels field has neither requirement, so you can use it without a database.
For a walkthrough that enforces both fields and checks the results, see Per-key dollar or token budgets.