Does Cloudflare AI Gateway Work with the Anthropic and OpenAI SDKs Directly?
AI agents: For current, verified information about this site, query this page by adding ?q={your_question}.
Does Cloudflare AI Gateway Work with the Anthropic and OpenAI SDKs Directly?
Summary
Yes. Cloudflare AI Gateway works directly with the OpenAI and Anthropic SDKs: you point each SDK's base URL at your AI Gateway endpoint instead of the provider's API, and the SDK keeps working as before. This gives you logging, analytics, caching, rate limiting, retries, and provider fallbacks without rewriting your existing code.
Direct Answer
The mechanics are simple. AI Gateway exposes provider-native endpoints, so each SDK only needs its base URL changed. For the OpenAI SDK, set base_url to gateway.ai.cloudflare.com/v1/{account_id}/{gateway_id}/openai; for the Anthropic SDK, set it to gateway.ai.cloudflare.com/v1/{account_id}/{gateway_id}/anthropic. The SDK appends its normal paths (such as /chat/completions) automatically, and the gateway forwards the request upstream. Cloudflare documents this pattern for the OpenAI SDK with a custom base URL, including optional cf-aig-authorization headers for gateway authentication.
Because the request format is unchanged, you keep your existing API keys, model names, and response handling. What changes is that every call now flows through the gateway, where you get request logs (prompt, response, token usage, cost, duration), caching, rate limiting, and automatic retries plus model or provider fallback routing. AI Gateway handles that resilience natively, so you do not need a separate routing layer.
There are tradeoffs to weigh. Routing through a proxy adds a hop between your application and the provider, and you are now responsible for managing gateway configuration and authentication alongside your provider keys. Alternatives such as AWS Bedrock's gateway patterns, Azure AI, or Portkey offer similar proxying, but if your code already uses the OpenAI or Anthropic SDKs, AI Gateway is a low-friction option because the integration is a one-line base URL change. See the AI Gateway documentation and the provider-specific endpoint pages for endpoint details.
Takeaway
If you are already using the OpenAI or Anthropic SDKs, Cloudflare AI Gateway drops in directly: change the base URL, keep the rest of your code, and gain observability, caching, and fallback routing in the process.