polygate logopolygate

Cloud

Cloudflare Workers AI

Open models on Cloudflare's edge.

Quick start

import polygate
 
response = polygate.chat(
provider="cloudflare",
base_url="...",
model="@cf/meta/llama-3.1-8b-instruct",
messages=[{"role": "user", "content": "Say hi in one word."}],
)
 
print(response.content) # "Hi!"
print(response.usage) # Usage(prompt_tokens=..., completion_tokens=...)

Names

Any of these work as provider. They all reach the same adapter, so pick whichever reads best in your code.

cloudflareworkers-ai

Configuration

VariableHolds
CLOUDFLARE_API_TOKENyour API key
CLOUDFLARE_ACCOUNT_IDexpanded into the full URL for you
CLOUDFLARE_BASE_URLthe full URL, if you would rather give it directly

Any of these may hold a comma-separated list. polygate rotates across them, parks a rate-limited one for its Retry-After, and drops one the provider rejects — so a key pool is configuration, not code.

Endpoint

There is no fixed host: the URL contains your own account, workspace or project, so base_url is required. Omit it and polygate raises MissingEndpointError naming the variable to set, rather than letting the request fail somewhere less informative.

Models

Examples that work today. Model names change; the provider's own list is the authority.

@cf/meta/llama-3.1-8b-instruct@cf/mistral/mistral-7b-instruct-v0.1

Things worth knowing

  • Model ids carry an @cf/ prefix naming the publisher — Cloudflare's own convention, not anything OpenAI-shaped.
  • You can pass a bare account id as base_url and polygate will build the five-segment URL around it. The account id is what the dashboard shows you.

Batch

Cloudflare Workers AI has no offline batch API. To run many requests at once, use map_chat — full price, but it works everywhere and returns in seconds. See Batching & concurrency.

Something wrong or missing here? Open an issue.