Article directory
Why is my Cloudflare AI base URL always returning a 404 error?
This article teaches you in seconds how to quickly obtain and configure a Cloudflare-compatible OpenAI base URL. It includes perfect templates for official REST API, AI Gateway format, and third-party clients (Dify/One-API), helping you get free AI APIs up and running with a single click!
Last week, a friend who does independent development suddenly contacted me and sent me a screenshot showing a screen full of 401 errors. He said, "Brother, I've been trying to debug Cloudflare's AI API all day, but it just won't work."
I glanced at his code. The Base URL was set to /ai/v1, but the request body was formatted as the official REST API, so the two didn't match at all.
He said he just wanted to use OpenAI's SDK to debug a model on Workers AI, is there any problem?
This is a big problem.
Cloudflare's AI services use three completely different base URLs depending on the scenario. Each has its own format and applicable scenarios. Although there is documentation, it's scattered across different pages, making it difficult to know which one to refer to on your first try. Using the wrong URL will result in a 401 or 400 error, preventing you from accessing the service.
When I first encountered this problem, I tried several times before figuring it out. I was speechless looking at the screen full of error messages.
Today I'll clearly draw these three roads for you, so you won't make any more mistakes after reading this.

OpenAI Compatible Interface Base URL
Let's start with the most commonly used one.
If you are using the OpenAI SDK, or tools that support custom OpenAI formats such as LangChain, Dify, NextChat, or One-API, then this is the correct approach.
https://api.cloudflare.com/client/v4/accounts/{你的account_id}/ai/v1
This is something Cloudflare specifically designed to align with the OpenAI interface format. Add `/chat/completions` after the complete dialogue endpoint. Replace your original OpenAI Base URL with this, and replace the API Key with your Cloudflare token, and it should run directly. I've tried models like `@cf/deepseek-ai/deepseek-r1-distill-qwen-32b` and `@cf/meta/llama-3.1-8b-instruct`, and they all work fine.
However, it should be noted that this only supports text-based dialogue models.
Cloudflare's official native REST API
If you need to use Stable Diffusion for graphing, Whisper for audio transcription, or any non-text model, take the second route, which is the Cloudflare official native REST API.
https://api.cloudflare.com/client/v4/accounts/{你的account_id}/ai/run
The model name can be written in the path, such as /ai/run/@cf/bytedance/stable-diffusion-xl-lightning, or it can be placed in the `model` field of the request body. I prefer to put it in the field; the path is shorter and cleaner. I was confused about this detail at first too. Some models accept both methods, while others only accept one. Pay attention to this when checking the documentation.
AI Gateway Base URL
There is a third way: AI Gateway.
This scenario is slightly different. If you've set up an AI Gateway in the Cloudflare backend to proxy third-party APIs, such as forwarding OpenAI requests through the Cloudflare gateway for caching, monitoring, and rate limiting, then the Base URL would be...
https://gateway.ai.cloudflare.com/v1/{你的account_id}/{你的gateway_id}/openai
Adding `/chat/completions` after the complete endpoint ensures all traffic goes through the Cloudflare gateway, and the log panel shows the latency and result of each request. This feature is incredibly useful as the team grows with more projects, eliminating the need to search through data from various vendors' backends one by one.
How to choose: Three sets of URLs explained in one sentence.
Okay, that covers the three paths. In summary, there's just one principle.
The path you take depends on the tools you use.
To use the OpenAI SDK to call a dialogue model, go to `/ai/v1`. To call an image/audio model, go to `/ai/run`. For unified management via a gateway, go to `/gateway/`. The authentication method is the same: put `Authorization: Bearer` followed by your API Token in the header. Create the Token in the Cloudflare backend, and remember to check the `Workers AI: Edit` permission.
Where to find the account_id? Log in to your Cloudflare admin panel, scroll down to the API section in the right sidebar, or check your browser's address bar; that 32-character alphanumeric string is it.
Honestly, after I helped my friend get it done that day, he said something to me that I think is quite right: it wasn't that I didn't know how, it was that the documentation hadn't explained it clearly to me.
Many technical configurations are like this. It's not that they're particularly difficult, but there are too many entry points, making it easy to get lost on your first try. If you've encountered various inexplicable errors when configuring Cloudflare AI, it's most likely due to an incorrect Base URL or a misformatted format. Keep these three sets of URLs handy; you can easily find the correct one next time.
You're welcome.
So, since you've read this far, if you found it helpful, please like and share it. If you want to receive updates first, you can also follow me! ⭐
Thank you for reading my article. See you next time.
Hopefully, the article "Stop Reporting Errors! Easily Configure Cloudflare for OpenAI Compatibility: Base URL and Interface" shared on Chen Weiliang's blog ( https://www.chenweiliang.com/ ) will be helpful to you.
Feel free to share this article's link: https://www.chenweiliang.com/cwl-34252.html
