Skip to content
agentgateway has joined the Agentic AI Foundation — Learn more

For the complete documentation index, see llms.txt. Markdown versions of all docs pages are available by appending .md to any docs URL.

Provider API keys

Page as Markdown

Provide and rotate the API keys that agentgateway uses to authenticate to your LLM providers.

Provider API keys authenticate agentgateway to LLM providers such as OpenAI or Anthropic. A leaked key lets attackers run requests at your expense.

Clients can authenticate with virtual keys. Agentgateway replaces each virtual key with the provider key before forwarding the request.

This guide covers inline keys, environment variables, files, and Kubernetes secrets or passthrough tokens. To replace an existing key, see Rotate a provider key.

Before you begin

Install the agentgateway binary.

Configure your agentgateway proxy

You can set the provider API key in the UI or in your configuration file.

  1. Start agentgateway with a configuration that has an llm section. You add the model and its API key in the UI.

    cat <<'EOF' > config.yaml
    # yaml-language-server: $schema=https://agentgateway.dev/schema/config
    llm:
      models: []
    EOF
    agentgateway -f config.yaml
  2. Open LLM > Models in the UI, such as http://localhost:15000/ui/llm/models, and click Add model.

  3. From the Provider dropdown, select OpenAI.

  4. Keep the default openai/* in the Incoming model match field, and keep Strip openai/ for the Outgoing model.

  5. For the Provider API key, select one of the following options.

    • API key: Enter the key itself, such as sk-proj..... The UI writes the key inline in the params.apiKey field. This option is the least secure. Only use this option for quick tests.
    • Env var: Enter the name of the environment variable, such as OPENAI_API_KEY. The UI writes apiKey: $OPENAI_API_KEY. The variable must be set in the environment that agentgateway runs in, or the save fails.
    • File: Enter the path to a file that holds the key, such as /etc/agentgateway/openai-key.txt. The UI writes apiKey: {file: <path>}. Agentgateway reads this file only when it loads the configuration, so a changed key takes effect after the next configuration reload or restart. This option differs from the File steps in the Config file tab, which use auth.key.value.file and pick up a changed key on the next request.
    • Unset: Configure no provider credential.
    Add model drawer with the OpenAI provider, the default openai/* match, the OPENAI_API_KEY environment variable, and Strip openai/ as the outgoing model
    Add model drawer with the OpenAI provider, the default openai/* match, the OPENAI_API_KEY environment variable, and Strip openai/ as the outgoing model
  6. Click Save model.

Whether the UI can save depends on your storage mode. For more information, see Save changes from the UI.

Rotate a provider key

Rotate provider keys regularly, when your provider requires it, or after a suspected leak. Clients that use virtual keys need no configuration changes.

  1. Create a new API key in the console of your LLM provider. Keep the old key active for now.

  2. Replace the key on the gateway using the method that matches your configuration.

    OptionHow to replace the keyRestart needed
    File (auth.key.value.file)Replace the file contents. Agentgateway uses the new key for the next request.No
    InlineEdit the key in the configuration file. Agentgateway reloads the file when it changes.No
    Environment variableSet the variable to the new key and restart agentgateway.Yes

    For file references in params.apiKey, agentgateway reads the key only when loading the configuration. After replacing the key file, save the configuration file to trigger a reload, or restart agentgateway.

  3. Send a request through agentgateway, and verify that it succeeds with the new key. If your gateway requires virtual keys, add an Authorization: Bearer <virtual-key> header.

    curl -s http://localhost:4000/v1/chat/completions \
      -H "Content-Type: application/json" \
      -d '{"model": "openai/gpt-4o-mini", "messages": [{"role": "user", "content": "Hello!"}]}' | jq .

    Example output:

    {
      "choices": [{
        "message": {
          "role": "assistant",
          "content": "Hello! How can I help you today?"
        }
      }]
    }
  4. Revoke the old key in the console of your LLM provider.

Next steps

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/.