Secure AI Requests with Cloudflare Gateway
Route AI requests through Cloudflare AI Gateway for caching, rate limiting, and analytics. Optimize LLM performance and cost.
Why it matters
Enhance your AI request security and performance by routing them through Cloudflare AI Gateway. This asset helps implement caching, rate limiting, and analytics for your AI applications.
Outcomes
What it gets done
Route AI requests through Cloudflare AI Gateway
Implement caching for AI responses
Enforce rate limiting on AI API calls
Monitor AI usage with analytics
Install
Add it to your toolbox
Run in your project directory:
curl -fsSL https://spark.entire.vc/get/pfoo-cloudflare-gateway | bash Capabilities
What this chain does
Reviews permissions and logs to flag unauthorized activity.
Labels or categorizes text, files, or data points.
Sends alerts or messages via email, Slack, or other channels.
Overview
Cloudflare Gateway
What it does
This example shows how to direct AI requests through Cloudflare's AI Gateway. It allows for the implementation of caching to speed up responses, rate limiting to control usage, and analytics to monitor AI interactions.
How it connects
Utilize this when you need to manage and optimize AI API calls, particularly for applications with significant traffic or cost concerns. It's beneficial for ensuring consistent performance and security for your AI integrations.
Source README
yaml-language-server: $schema=https://promptfoo.dev/config-schema.json
Cloudflare AI Gateway Example
This example demonstrates routing AI requests through Cloudflare AI Gateway
for caching, rate limiting, and analytics.
Required environment variables:
CLOUDFLARE_ACCOUNT_ID - Your Cloudflare account ID
CLOUDFLARE_GATEWAY_ID - Your AI Gateway ID
Optional environment variables:
CF_AIG_TOKEN - Required if your gateway has "Authenticated Gateway" enabled
Provider API keys (choose based on which providers you use):
OPENAI_API_KEY - OpenAI API key
ANTHROPIC_API_KEY - Anthropic API key
GROQ_API_KEY - Groq API key
Note: If you've configured BYOK (Bring Your Own Keys) in Cloudflare dashboard,
you can omit the provider API keys - Cloudflare will use the stored keys.
Run with:
npx promptfoo eval -c examples/cloudflare-gateway/promptfooconfig.yaml
description: Cloudflare AI Gateway comparison
prompts:
- 'Explain {{topic}} in simple terms, in about 2-3 sentences.'
providers:
Route OpenAI requests through Cloudflare AI Gateway
- id: cloudflare-gateway:openai:gpt-4o-mini
config:
accountId: '{{env.CLOUDFLARE_ACCOUNT_ID}}'
gatewayId: '{{env.CLOUDFLARE_GATEWAY_ID}}'Uncomment if using authenticated gateway:
cfAigToken: '{{env.CF_AIG_TOKEN}}'
temperature: 0.7
max_tokens: 200
Route Anthropic requests through Cloudflare AI Gateway
- id: cloudflare-gateway:anthropic:claude-haiku-4-5-20251001
config:
accountId: '{{env.CLOUDFLARE_ACCOUNT_ID}}'
gatewayId: '{{env.CLOUDFLARE_GATEWAY_ID}}'Uncomment if using authenticated gateway:
cfAigToken: '{{env.CF_AIG_TOKEN}}'
max_tokens: 200
Route Groq requests through Cloudflare AI Gateway
- id: cloudflare-gateway:groq:llama-3.1-8b-instant
config:
accountId: '{{env.CLOUDFLARE_ACCOUNT_ID}}'
gatewayId: '{{env.CLOUDFLARE_GATEWAY_ID}}'Uncomment if using authenticated gateway:
cfAigToken: '{{env.CF_AIG_TOKEN}}'
temperature: 0.7
max_tokens: 200
tests:
vars:
topic: machine learning
assert:- type: contains-any
value:- algorithm
- data
- learn
- pattern
- type: contains-any
vars:
topic: quantum computing
assert:- type: contains-any
value:- qubit
- quantum
- superposition
- computer
- type: contains-any
vars:
topic: blockchain
assert:- type: contains-any
value:- block
- chain
- ledger
- decentralized
- type: contains-any
Discussion
Questions & comments · 0
Sign In Sign in to leave a comment.