Create and secure an AI agent wrapper us·续

userInput.disabled = false; sendButton.disabled = false; userInput.focus(); }

function appendMessage(role, content) { const messageDiv = document.createElement('div'); messageDiv.className = 'message ' + role + '-message'; messageDiv.textContent = content; messagesDiv.appendChild(messageDiv); messagesDiv.scrollTop = messagesDiv.scrollHeight; } </script> </body> </html>`;


Note that the account ID and gateway ID need to be replaced in the AI Gateway endpoint. You can add these as [environment variables](https://developers.cloudflare.com/workers/configuration/environment-variables/) or [secrets](https://developers.cloudflare.com/workers/configuration/secrets/) in Workers. If you chose to use Authenticated Gateway when creating your AI gateway, make sure to also add your token as a secret and pass its value to the AI gateway in the `cf-aig-authorization` header.

### 3. Publish the Worker

Once the Worker code is complete, you need to make the Worker addressable using a hostname controllable by Cloudflare Access.

Edit the Wrangler configuration file and add the following information to ensure that the Worker is only accessible using the custom hostname:

```diff
name = "ai-agent-wrapper"
main = "src/index.js"
compatibility_date = "2023-10-30"
workers_dev = false

+# Replace with your custom domain
+routes = [
+  { pattern = "&lt;YOUR_CUSTOM_DOMAIN>", custom_domain = true }
+]

[vars]
# Add any environment variables here

To publish the worker, run wrangler deploy.

If you built your Worker remotely using the code editor available in the Cloudflare dashboard, you can deploy it by selecting Deploy.

To ensure that the Worker is only accessible from the custom hostname:

  1. In the Cloudflare dashboard, go to the Workers & Pages page. Go to Workers & Pages ↗
  2. Select your Worker.
  3. Go to Settings.
  4. Within Domains & Routes, select Add.
  5. Choose Custom domain.
  6. Enter your desired custom domain name.
  7. Select Add domain.

The Worker is now behind an addressable public hostname. Make sure to turn off both workers.dev and Preview URLs so that the Worker can only be accessed with its custom domain.

4. Secure the wrapper with Access

To secure the AI agent wrapper to ensure that only trusted users can access it:

  1. In the Cloudflare dashboard ↗︎, go to Zero Trust > Access controls > Applications.
  2. Select Create new application.
  3. Select Self-hosted and private.
  4. Select Add public hostname and enter the custom domain you set for your Worker.
  5. Configure your Access application for your Worker.
  6. Add Access policies to control who can connect to your application.

Now your AI wrapper can only be accessed by your users that successfully match your Access policies.

5. Block access to public AI agents with Gateway

You can now block access to all unauthorized public AI agents with a Gateway HTTP policy.

  1. In the Cloudflare dashboard ↗︎, go to Zero Trust > Traffic policies > Firewall policies > HTTP.
  2. Select Add a policy.
  3. Add the following policy:
Selector Operator Value Action
Content Categories in Artificial Intelligence Block
4. Select Create policy.

This ensures that public AI agents are not accessible using a managed endpoint.

Alternatively, you can prevent users from using public AI agents by displaying a custom block message, redirect, or a user notification directing users to the AI agent wrapper.

6. Enforce Data Loss Prevention and Clientless Browser Isolation

Now that you have full control over access to your AI agent wrapper, you can enforce extra security methods such as Data Loss Prevention (DLP) and Clientless Web Isolation to protect and control data shared with the AI agent.

Apply Data Loss Prevention profiles

You can use Data Loss Prevention (DLP) to prevent your users from sending sensitive data to the AI agent.

  1. In the Cloudflare dashboard ↗︎, go to Zero Trust > Data loss prevention > Profiles.
  2. Ensure that the DLP profiles you want to enforce are properly configured.
  3. Add an HTTP policy to enforce the DLP profile for the hostname for your wrapper. For example:
Selector Operator Value Logic Action
Host is ai-wrapper.example.com And Block
DLP Profile in AI DLP profile
4. Select Create policy.

For more information on creating DLP policies, refer to Scan HTTP traffic.

Execute in a clientless isolated browser

Because you published your wrapper as a self-hosted Access application, you can execute it in an isolated session for your users by creating an Access policy and configuring it for your application.

  1. In Cloudflare One ↗︎, go to Browser isolation > Browser isolation settings.
  2. Turn on Allow users to open a remote browser without the device client.

  3. Go to Access controls > Policies.

  4. Select Add a policy.
  5. Set the Action to Allow.
  6. In Add rules, add identity rules to define who the application should be isolated for.
  7. In Additional settings (optional), turn on Isolate application.

Once the Access policy has been created, you can attach it to your wrapper.

  1. Go to Access controls > Applications.
  2. Choose your wrapper application, then select Configure.
  3. In Policies, select Select existing policies.
  4. Choose the Access policy you previously created.
  5. Select Confirm, then select Save.

Because Clientless Web Isolation traffic applies your Gateway HTTP policies, your configured DLP profiles will apply to isolated sessions.

For more information on isolating an Access application, refer to Isolate self-hosted application.

Additional benefits

Organizations that adopt Cloudflare to secure access to AI agents will benefit from improved visibility and configurability.

Visibility

Zero Trust will log all Access events and DLP detections. In addition, AI Gateway provides visibility into user prompts, model response, token usage, and costs.

Logs can be exported to external providers with Logpush.

Configurability

You can configure your wrapper to use a different AI provider or give your users the option to choose between multiple AI providers, including AI models running directly on Cloudflare's global network with Workers AI. With this, you can control costs related to AI usage or adopt newer models without impacting your users or the access controls already put in place.

Was this helpful?

YesNo

On this page

Create and secure an AI agent wrapper us·续 - 图1Docs

{"@context":"https://schema.org","@type":"TechArticle","@id":"https://developers.cloudflare.com/cloudflare-one/tutorials/ai-wrapper-tenant-control/#page","headline":"Create and secure an AI agent wrapper using AI Gateway and Zero Trust","description":"This tutorial explains how to use Cloudflare AI Gateway and Zero Trust to create a functional and secure website wrapper for an AI agent.","url":"https://developers.cloudflare.com/cloudflare-one/tutorials/ai-wrapper-tenant-control/","inLanguage":"en","image":"https://developers.cloudflare.com/cloudflare-one/tutorials/ai-wrapper-tenant-control/og.png?v=612b12b27c0fbc56","dateModified":"2026-10-12","publisher":{"@type":"Organization","name":"Cloudflare","description":"One platform for your apps, agents, and workforce. Build, secure, and scale without managing infrastructure","url":"https://www.cloudflare.com/","sameAs":["https://github.com/cloudflare","https://www.linkedin.com/company/cloudflare","https://x.com/cloudflare"],"logo":{"@type":"ImageObject","url":"https://developers.cloudflare.com/logo.svg"},"address":{"@type":"PostalAddress","streetAddress":"101 Townsend St","addressLocality":"San Francisco","addressRegion":"CA","postalCode":"94107","addressCountry":"US"},"contactPoint":[{"@type":"ContactPoint","contactType":"Customer Support","url":"https://support.cloudflare.com/","availableLanguage":["English"]},{"@type":"ContactPoint","contactType":"Sales","url":"https://www.cloudflare.com/contact/","availableLanguage":["English"]}]},"isPartOf":{"@type":"WebSite","@id":"https://developers.cloudflare.com/#website","name":"Cloudflare Docs","url":"https://developers.cloudflare.com/"},"keywords":["AI"]}


description: This tutorial covers how to use a Cloudflare Worker to add custom headers to traffic. The headers will be sent to origin services protected by Cloudflare Access. title: Create custom headers for Cloudflare Access-protected origins with Workers image: https://developers.cloudflare.com/cloudflare-one/tutorials/access-workers/og.png?v=fa530a0104b696ff


Skip to content

Documentation Index
Fetch the complete documentation index at: https://developers.cloudflare.com/cloudflare-one/llms.txt
Use this file to discover all available pages before exploring further.