Skip to main content
Cloudflare serves models in two ways, and the SDK meters both with one recipe:
  • AI Gateway sits in front of other providers, such as OpenAI or Anthropic, at gateway.ai.cloudflare.com, with caching, fallbacks and routing.
  • Workers AI runs open-weight models, such as Llama, on Cloudflare’s own GPUs, at api.cloudflare.com.
Both report under the cloudflare vendor name, because Cloudflare is who charges you either way.

Set up

The SDK recognizes both Cloudflare addresses automatically, so you wrap the client as usual.
  • A gateway on a custom domain has no Cloudflare address for the SDK to recognize. Pass the recipe with its base URL: stigg.wrap(client, { recipe: cloudflareRecipe({ baseUrl }) }).
  • Ask for usage on streamed calls. Through the OpenAI client, usage is included in a stream only when the request sets stream_options: { include_usage: true }.
  • Do not stream ai.run. A streamed ai.run call returns raw bytes with no usage in them, so it is checked before it is sent but not metered. Call ai.run without streaming, or stream through the OpenAI client instead.

What is reported

Cloudflare forwards each upstream provider’s response as it is, so every call reports the token counts in that provider’s own format: OpenAI’s field names through the OpenAI routes, and Anthropic’s through the Anthropic route. For details, see Cloudflare’s AI Gateway and Workers AI docs. A meter that filters on one provider’s field names misses calls in the other’s format. If you use both the OpenAI and Anthropic routes, price both sets of metrics, or use normalized metrics. Each event also carries:
  • Cache hits are free. When AI Gateway answers from its cache, no provider was paid for the call, so nothing is reported. The call is still checked before it is sent, since whether it will hit the cache is not known in advance. A call through ai.run cannot see the gateway’s cache status, so it is always metered.
  • No cost. Neither AI Gateway nor Workers AI returns a price, so you can meter tokens but not what the call cost. Workers AI charges in Neurons, and no response reports a Neuron count.
Every usage field the vendor returns is also included, with a raw. prefix. See How usage is reported.