API keys suck - they are hard to manage, easy to leak, and often tied to a single user or service account. Despite this, they are extremely prevalent in the AI space - so much so that AWS, which historically only offered SigV4 signing, even added API key support for Bedrock!

In this post, I want to explore how to authenticate to LLMs without relying on API keys at all using a gateway.

Let the client bring its own key

flowchart LR C[Client] -->|Provider API key| G[Agentgateway] G -->|Passthrough key| L[LLM backend]

The simplest option is for the client to keep using the provider key it already has and send that key to the gateway. The gateway isn't really doing anything here (with authentication anyways), and generally I don't see people doing this so not much more to say here.

"Virtual" keys

The usual first step is to introduce a "virtual" key. This is a key issued by the gateway itself rather than the underlying LLM provider.

flowchart LR C[Client] -->|Virtual API key| G[Agentgateway] G -->|Provider key| L[LLM backend]

The gateway authenticates the client using the virtual key, and then swaps it out for the actual provider key when making requests to the backend.

Each developer gets their own key, you can attach a budget to it, and revoking one doesn't break everyone else.

The Agentgateway config looks like this (docs):

llm:
  policies:
    apiKey:
      mode: strict
      keys:
      - key: "$GATEWAY_KEY"
        metadata:
          name: alice
  models:
  - name: "*"
    provider: openAI
    params:
      apiKey: "$OPENAI_API_KEY"

For getting a client working, this is hard to beat. Every client under the sun supports an API key (even if it insists on calling it OPENAI_API_KEY when it's really your gateway's key). But we have all the issues that API keys bring around management and security.

Have users log in

Before AI decided to use API keys everywhere, there was this obscure standard called OAuth that handled user authentication and authorization. What if we just used that? Everyone already has an identity provider, so we could leverage it for authentication instead of managing API keys.

sequenceDiagram participant C as Client participant I as Identity provider participant G as Agentgateway participant L as LLM backend C->>I: Sign in (browser) I->>C: JWT C->>G: JWT Note over G: Verify JWT against IdP's JWKS G->>L: provider API key

On the gateway side, this is easy: swap the apiKey policy for a jwtAuth one pointing at your issuer, audience, and JWKS URL (docs):

llm:
  policies:
    jwtAuth:
      mode: strict
      issuer: https://login.example.com/
      audiences: [llm-gateway]
      jwks:
        url: https://login.example.com/.well-known/jwks.json

Now every request carries a real user identity (jwt.sub or jwt.email), which you can use for logs, rate limits, and more. Offboarding is handled by the identity provider, like everything else.

Sounds great... except it doesn't work in almost any case. Doing the OAuth flow is great when we are a browser and can issue a redirect to a login page - most LLM clients can't handle that. So if you could get the (ephemeral) token to the client this would work great, but that piece is missing, making this option nearly infeasible.

MCP has first-class OAuth support and requires clients to handle the OAuth flow themselves, including opening a browser, following redirects, and refreshing tokens, so it doesn't have this issue.

Clients with built-in support

Very recently, some clients have added native OAuth support, which solves this problem.

Codex has a gateway_oauth table on custom model providers. Point the provider's base_url at the gateway, fill in the authorization URL, token URL, client ID, and scopes from your identity provider, and Codex will prompt you to sign in the next time you use it (and handle refreshes). See Codex and Entra setup.

Claude Desktop has something similar in its enterprise gateway settings, called Interactive sign-in. Give it the gateway URL and OIDC settings and it will log in through the browser (or an OS broker, on managed devices). All the gateway ever sees is a bearer token on the inference request. See Claude Desktop setup.

For both of these, though, access is often through a (heavily subsidized) subscription rather than API usage. So the ideal would actually be a dual-auth flow: the client logs in twice, once to the company identity provider (for the gateway) and once to the subscription (for the provider), and sends both credentials.

sequenceDiagram participant C as Client participant I as Identity provider participant G as Agentgateway participant L as LLM provider C->>I: Sign in (company account) I->>C: JWT C->>L: Sign in (subscription) L->>C: Subscription token C->>G: JWT + subscription token Note over G: Verify JWT, enforce policy G->>L: subscription token

The gateway still gets the user identity for logs and policy, but the actual usage is billed to the user's subscription. Codex explicitly supports this, while Claude does not.

Everything else: agentdesktop

If you have a bunch of developers using a bunch of different tools, waiting for each one to add OAuth support isn't great. agentdesktop handles the login and writes the client config for you.

For Claude Code, for example, it sets the gateway URL and an API key helper; the helper just asks the local agentdesktop daemon for a token. Claude Code thinks it has an API key, and the developer gets a browser login instead of a secret to paste into a config file (docs).

daemon:
  user: true
llmGateway:
  url: https://llm.example.com
  authentication:
    type: oidc
    issuer: https://login.example.com/
    clientId: agentdesktop
    redirectUri: http://127.0.0.1:51327/callback
programs:
  claudeCode:
    useLlmGateway: true

You might wonder, why not just write an apiKeyHelper script yourself? The problem is apiKeyHelper is really designed for a quick non-interactive script that returns a result, which doesn't go well with an interactive browser login form. You could try to make apiKeyHelper work with a browser login, but that would require building a custom helper to handle that - this is exactly what agentdesktop is.

Dropping the provider key

That takes care of one side. But the gateway still has a long-lived provider key sitting in it, and this is arguably the more important key to remove. While we have removed key sprawl, we now have a single point with all of our provider keys!

flowchart LR C[Client] -->|User JWT| G[Agentgateway] W[Workload identity] -->|Short-lived credentials| G G -->|Short-lived credentials| L[LLM backend]

If Agentgateway is running somewhere that already gives it an identity (often called "workload identity"), which most places do these days, we can use that instead. All that's left is getting the provider to trust that identity. Fortunately, all of the major providers now support this!

Vertex AI, Azure OpenAI, and Bedrock

flowchart LR C[Client] -->|User JWT| G[Agentgateway] G -->|Google access token| V[Vertex AI] G -->|Entra access token| A[Azure OpenAI] G -->|SigV4 with temporary IAM credentials| B[Amazon Bedrock]

For the big cloud providers, this is really just an IAM exercise. Agentgateway picks up credentials from the environment by default for all three, so there isn't even an auth block to write (provider docs):

llm:
  models:
  - name: "vertex/*"
    provider: vertex
    params:
      vertexProject: my-project
      vertexRegion: us-central1
    transformation:
      model: llmRequest.model.stripPrefix("vertex/")
  - name: "azure/*"
    provider: azure
    params:
      azureResourceName: my-openai-resource
      azureResourceType: openAI
    transformation:
      model: llmRequest.model.stripPrefix("azure/")
  - name: "bedrock/*"
    provider: bedrock
    params:
      awsRegion: us-east-1
    transformation:
      model: llmRequest.model.stripPrefix("bedrock/")
  • GCP. On GKE, enable Workload Identity Federation and grant the workload access to Vertex AI (for example, roles/aiplatform.user). Application Default Credentials picks up the workload's credentials via the metadata server. GKE workload identity
  • Azure. On AKS, federate the Kubernetes service account with an Entra identity, give it the Cognitive Services OpenAI User role on the resource, and label the gateway pod azure.workload.identity/use: "true" so it gets the projected token and environment variables. AKS workload identity
  • AWS. On EKS, give the gateway pod an IAM role through IRSA. On ECS, use the task role. Give the role permission to invoke the models you're serving (including bedrock:InvokeModelWithResponseStream for streaming). Agentgateway discovers the temporary credentials and signs the Bedrock request. Agentgateway on AWS

Anthropic and OpenAI directly

flowchart LR C[Client] -->|User JWT| G[Agentgateway] F[Workload JWT file] -->|Workload JWT| G G -->|Exchange workload JWT| T[Provider token endpoint] T -->|Short-lived access token| G G -->|Bearer access token| L[LLM backend]

Until recently, talking to Anthropic or OpenAI directly meant an API key, full stop. Now both support workload identity federation: you hand them a JWT from an issuer they trust, and get a short-lived access token back.

Unlike the cloud providers, this requires some configuration on the provider side to register the workload identity issuer and set up the trust relationship. Once that is done, however, the process is about the same.

The setup is: have your workload identity provider write a rotating JWT file for each API (Kubernetes itself is a good option here!), register the issuer with each provider, and scope the trust rule down to just the identity running Agentgateway.

On Kubernetes, the JWT files can just be projected service account tokens. Each provider wants a different audience, so we project two tokens into the gateway pod:

apiVersion: apps/v1
kind: Deployment
metadata:
  name: agentgateway
spec:
  template:
    spec:
      serviceAccountName: agentgateway
      containers:
      - name: agentgateway
        volumeMounts:
        - name: wif
          mountPath: /var/run/secrets/wif
          readOnly: true
      volumes:
      - name: wif
        projected:
          sources:
          - serviceAccountToken:
              path: anthropic
              audience: https://api.anthropic.com
              expirationSeconds: 3600
          - serviceAccountToken:
              path: openai
              audience: https://api.openai.com/v1
              expirationSeconds: 3600

The kubelet rotates these automatically. The issuer to register with each provider is your cluster's service account issuer (which needs a publicly reachable OIDC discovery endpoint; managed clusters like EKS, GKE, and AKS provide one), and the subject to trust is system:serviceaccount:<namespace>:agentgateway.

On the gateway, this uses Enterprise Agentgateway's fileAccess.cel support: name each file, then reference it in the token exchange policy. Agentgateway watches the files for rotation, does the exchange, and uses the resulting access token for inference.

config:
  fileAccess:
    cel:
      anthropicToken: /var/run/secrets/wif/anthropic
      openaiToken: /var/run/secrets/wif/openai

llm:
  models:
  - name: "anthropic/*"
    provider: anthropic
    transformation:
      model: llmRequest.model.stripPrefix("anthropic/")
    auth:
      oauthTokenExchange:
        host: https://api.anthropic.com/v1/oauth/token
        grantType: jwtBearer
        subjectToken:
          source:
            expression: files.anthropicToken
        additionalParams:
          organization_id: '"YOUR_ANTHROPIC_ORGANIZATION_ID"'
          federation_rule_id: '"YOUR_ANTHROPIC_FEDERATION_RULE_ID"'

  - name: "openai/*"
    provider: openAI
    transformation:
      model: llmRequest.model.stripPrefix("openai/")
    auth:
      oauthTokenExchange:
        host: https://auth.openai.com/oauth/token
        grantType: tokenExchange
        subjectToken:
          tokenType: urn:ietf:params:oauth:token-type:jwt
          source:
            expression: files.openaiToken
        additionalParams:
          identity_provider_id: '"YOUR_OPENAI_IDENTITY_PROVIDER_ID"'
          service_account_id: '"YOUR_OPENAI_SERVICE_ACCOUNT_ID"'

And that's it: no API keys on the laptops, and no API keys in the gateway.

I really need a provider API key

If your provider only supports API keys, there are some interesting options around making that usage and storage more secure - check out this post from my colleague exploring this more.