Skip to content
agentgateway has joined the Agentic AI FoundationLearn more

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

Page as Markdown

Authenticate requests using API keys with configurable validation modes.

Verified Code examples on this page have been automatically tested and verified.

Attaches to:

Route

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.

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"

LLM budgets and model access

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 which models the key can reach. 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"
SettingDescription
allowedModelsModel name patterns that the key can reach. 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. You cannot combine * with another pattern.
budgetsList of budgets that are charged independently after each LLM response.
budgets[].nameNames the budget within its key. The name must be unique among that key’s budgets.
budgets[].limit.unitUSD to cap realized cost, or Tokens to cap token usage.
budgets[].limit.amountThe maximum usage in the window. A Tokens amount must be a whole number. A USD amount takes up to nine decimal places.
budgets[].window.rollingLength 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[].onBudgetExceededBlock 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. Setting config.logging.database.url instead 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.

Was this page helpful?
Agentgateway assistant

Ask me anything about agentgateway configuration, features, or usage.

Note: AI-generated content might contain errors; please verify and test all returned information.

Tip: one topic per conversation gives the best results. Use the + button in the chat header to start a new conversation.

Switching topics? Starting a new conversation improves accuracy.
↑↓ navigate select esc dismiss

What could be improved?

Your feedback helps us improve assistant answers and identify docs gaps we should fix.

Need more help? Join us on Discord: https://discord.gg/y9efgEmppm

Want to use your own agent? Add the Solo MCP server to query our docs directly. Get started here: https://search.solo.io/.