Skip to content

Cloudflare Access

Last updated View as MarkdownAgent setup

Protect your gateway with Cloudflare Access so users authenticate with your identity provider before they can send requests. Putting AI Gateway behind Access gives you identity-aware control over your AI traffic: you decide who can reach the gateway, tie each request to a verified user, and govern usage per user without building your own authentication layer or passing user IDs from the client application.

To put AI Gateway behind Access, you must first set a custom domain on your gateway and have Cloudflare Access enabled on your account.

How it works

When a request to a custom domain includes a valid Cloudflare Access JWT, AI Gateway accepts the Access JWT as the request credential. The client does not need to send an AI Gateway token for that request.

AI Gateway also adds the verified Access user ID to request metadata as cf.user_id. This value is the Access JWT sub claim, not the user's email address. You can then filter logs, analytics, and spend by the authenticated user.

Before AI Gateway forwards the request to the upstream provider, it removes Cloudflare-only credentials such as the Access JWT and AI Gateway authorization headers.

Once a custom domain is protected by Access, every request to that domain must pass an Access policy. Requests that only include an AI Gateway token, without a valid Access token, are blocked by Access before they reach the gateway. Update existing integrations to authenticate through Access, or keep sending gateway-token traffic to the default gateway.ai.cloudflare.com endpoint, which is not protected by Access.

Set up Access on a gateway

  1. Set up a custom domain for the gateway you want to protect.
  2. In the Cloudflare dashboard, go to AI > AI Gateway.
  3. Select the gateway you configured with a custom domain.
  4. Go to the Access tab and set up Cloudflare Access on the gateway.
  5. Add Access policies that define which users can call the gateway.

Setting up Access from the Access tab configures the Access application for you, so coding agents and other non-browser clients can authenticate by sending the Access token as a bearer token.

After setup, users can make requests to the custom domain after authenticating through Access. Requests with a valid Access user subject include cf.user_id in AI Gateway metadata.

Make a request

After the user authenticates to Access, send requests to the custom domain without the account ID or gateway ID in the path:

curl -X POST "https://ai.example.com/openai/v1/chat/completions" \
  --header "Content-Type: application/json" \
  --data '{
    "model": "gpt-4.1-mini",
    "messages": [
      {
        "role": "user",
        "content": "What is Cloudflare?"
      }
    ]
  }'

If you call the custom domain from a non-browser client, include the Access token using the header or cookie format supported by Cloudflare Access. For example, cloudflared access curl can send the Access token for command-line requests.

For coding agents, refer to the per-agent setup under Coding agents — for example, Claude Code and OpenAI Codex.

Limitations

  • cf.user_id is only added when AI Gateway receives a valid Access JWT with a non-empty user subject.
  • Service-token requests do not include cf.user_id because they do not represent an individual Access user.
  • You may not supply metadata keys that begin with cf.. These keys are reserved and are not saved.

Was this helpful?