Prompt Chain

Secure AI Requests with Cloudflare Gateway

A promptfoo example routing OpenAI, Anthropic, and Groq requests through Cloudflare AI Gateway.

Works with cloudflare

91
Spark score
out of 100
Updated 12 days ago
Version 0.121.19
Models
gpt 4oclaudellama 3

Add to Favorites

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

01

Route AI requests through Cloudflare AI Gateway

02

Implement caching for AI responses

03

Enforce rate limiting on AI API calls

04

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

Overview

Cloudflare Gateway

This promptfoo example routes the same prompt through Cloudflare AI Gateway to OpenAI, Anthropic, and Groq models, testing gateway-based multi-provider access. Use it when routing LLM traffic through Cloudflare AI Gateway and evaluating multiple providers through that gateway in one promptfoo config.

What it does

This promptfoo example compares OpenAI, Anthropic, and Groq models routed through Cloudflare AI Gateway, demonstrating gateway-based caching, rate limiting, and analytics on top of multiple providers. The same "explain a topic simply" prompt runs against gpt-4o-mini, claude-haiku-4-5-20251001, and llama-3.1-8b-instant, each addressed via a cloudflare-gateway:<provider>:<model> provider ID.

When to use - and when NOT to

Use this example when you already route (or plan to route) LLM traffic through Cloudflare AI Gateway and want a promptfoo eval that exercises multiple providers through the same gateway, or when you want to compare gateway-routed provider outputs on a shared prompt. It is not useful if you call providers directly without Cloudflare AI Gateway, since it depends entirely on gateway configuration (CLOUDFLARE_ACCOUNT_ID, CLOUDFLARE_GATEWAY_ID, and optionally CF_AIG_TOKEN for authenticated gateways).

Inputs and outputs

Each provider config supplies accountId and gatewayId from environment variables, plus optional cfAigToken for authenticated gateways and per-model generation settings (temperature, max_tokens). Provider API keys (OPENAI_API_KEY, ANTHROPIC_API_KEY, GROQ_API_KEY) can be set directly, or omitted entirely if Bring Your Own Keys (BYOK) is configured in the Cloudflare dashboard. Tests supply a topic variable and assert the response contains at least one expected keyword:

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}}'
      temperature: 0.7
      max_tokens: 200

Run it with npx promptfoo eval -c examples/cloudflare-gateway/promptfooconfig.yaml.

Integrations

Routes through Cloudflare AI Gateway to OpenAI (gpt-4o-mini), Anthropic (claude-haiku-4-5-20251001), and Groq (llama-3.1-8b-instant), and supports Cloudflare's BYOK feature so provider keys can live in the Cloudflare dashboard instead of local environment variables.

Who it's for

Teams already using Cloudflare AI Gateway for caching, rate limiting, or analytics across multiple LLM providers who want a promptfoo eval template that exercises that gateway setup directly.

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
  • vars:
    topic: quantum computing
    assert:

    • type: contains-any
      value:
      • qubit
      • quantum
      • superposition
      • computer
  • vars:
    topic: blockchain
    assert:

    • type: contains-any
      value:
      • block
      • chain
      • ledger
      • decentralized

FAQ

Common questions

Discussion

Questions & comments · 0

Sign In Sign in to leave a comment.