Skip to main content
Deploy the Portkey AI Gateway to Cloudflare Workers for global distribution with sub-millisecond latency from 300+ data centers worldwide.

Overview

Cloudflare Workers provides:
  • Global Edge Network - Deployed to 300+ cities worldwide
  • Zero Cold Starts - Instant response times
  • Auto-scaling - Handles any traffic volume
  • Built-in Security - DDoS protection and WAF
  • Generous Free Tier - 100,000 requests/day

Prerequisites

Quick Deployment

1

Clone the repository

Clone the Portkey AI Gateway repository:
2

Install dependencies

Install the required npm packages:
3

Authenticate with Cloudflare

Login to your Cloudflare account via Wrangler:
This opens a browser window for authentication.
4

Deploy to Cloudflare

Deploy the gateway:
Or directly with Wrangler:
After deployment, you’ll receive a URL like https://rubeus.YOUR_SUBDOMAIN.workers.dev.

Configuration

Wrangler Configuration

The gateway includes a wrangler.toml configuration file:
wrangler.toml

Environment Variables

Set environment variables using Wrangler:

Deployment Environments

Deploy to different environments:

Custom Domain

Add a custom domain to your Worker:
1

Add domain in Cloudflare Dashboard

  1. Navigate to Workers & Pages
  2. Select your Worker
  3. Go to Settings → Triggers
  4. Click “Add Custom Domain”
2

Configure DNS

Cloudflare automatically configures DNS for domains in your account.
3

Verify deployment

Access your gateway at your custom domain:

Using Wrangler

Add domains via Wrangler:

Development

Local Development

Run the gateway locally with Wrangler:
Or directly:
The local server runs at http://localhost:8787.

Development with Node.js

For faster iteration during development:
This uses Node.js instead of the Cloudflare Workers runtime.

Bindings

KV Namespace

Add KV storage for caching:
wrangler.toml
Create a KV namespace:

R2 Storage

Add R2 buckets for object storage:
wrangler.toml

D1 Database

Add D1 for SQL storage:
wrangler.toml

Monitoring and Logs

View Logs

Stream real-time logs:
With filters:

Analytics

View analytics in the Cloudflare dashboard:
  1. Navigate to Workers & Pages
  2. Select your Worker
  3. View the Analytics tab

Enable Logpush

For production environments, enable Logpush in wrangler.toml:
Configure destinations in the Cloudflare dashboard.

Resource Limits

Free Tier

  • 100,000 requests/day
  • 10ms CPU time per request
  • 128 MB memory
  • Unlimited requests ($0.50 per million)
  • 50ms CPU time per request
  • 128 MB memory

Best Practices

  • Use KV for caching to reduce compute time
  • Minimize external API calls
  • Use streaming responses for large payloads
  • Implement proper error handling

Deployment Strategies

Blue-Green Deployment

Deploy to a staging environment first:

Gradual Rollout

Use Cloudflare Workers’ percentage-based routing for gradual rollouts:
  1. Deploy new version with a different name
  2. Configure traffic splitting in the dashboard
  3. Gradually increase traffic to the new version

CI/CD Integration

GitHub Actions

Create .github/workflows/deploy.yml:
deploy.yml

GitLab CI

Create .gitlab-ci.yml:
.gitlab-ci.yml

Troubleshooting

Build Errors

If you encounter build errors:

Authentication Issues

Re-authenticate with Wrangler:

CPU Time Exceeded

Optimize your code or upgrade to a paid plan for longer CPU time limits.

Next Steps

Cloudflare Workers Docs

Learn more about Cloudflare Workers

Configuration

Configure the gateway for your needs