In Development
Fixing OpenRouter Latency with Cloudflare Worker Region Pinning
Welcome in another technical note where I write my problem of doing things, and write the solution to capture the trail!
So basically, I ran into an issue when my team reported that generating placeholders with Pensil Ajaib sometimes failed. It was not rare; enough requests we dropping that I got affected too, so I started debugging. At first I thought maybe the rate limiter was too strict or there was a bug on the backend. But it wasn’t!
After inspecting the logs, I realized the problem was that Gemini did not support the region that Cloudflare used, and the Worker was routing requests through Hong Kong.
The Routing Problem
When I connected OpenRouter through a Cloudflare Worker, the routing started misbehaving because of the issue unsupported region, but using other models works fine. Cloudflare’s Smart Placement feature automatically move the worker to a region that didn’t play well with my AI provider setup, which made the request flow unstable. Sometime the requests worked, sometime failed, and there wasn’t a clear pattern from the user’s side. It is just felt it occasionally broken.
[placement]
mode = "smart"The Fix
The solution was to stop fully relying on automatic placement. Instead, I configured the Worker to use a specific region, so the deployment behavior became predictable. That made the routing more stable and reduced the failed requests.
Since my team is all based in Indonesia, picking a fixed region that sits closer to our traffic and backend also made sense. But for systems that need the absolute lowest latency and can tolerate a bit more volatility, automatic smart placement might still be the better call. I am not sure, but from I read this is the correct approach.
What Stayed the Same
My overall setup didn’t change:
- Cloudflare Worker as the API layer
- OpenRouter as the model router
- Gemini 3.1 Flash Lite for the default model for Pensil Ajaib figma plugin
The only change was the placement strategy that went from let Cloudflare pick to just to set it exact region I want this Worker to live.
Why this Important
For AI-powered products, infrastructure decision can affect reliability just as much as the code itself. A mismatch in the routing like where Worker being place in a region where the upstream doesn’t support it can be enought to break a request and make user experience feel inconsistent.
In my case, using fixed region gave me more control over the path between OpenRouter and Gemini, which made the process more stable!
Takeaway
Cloudflare’s Smart Placement is powerful and usually helps reduce latency across mixed workloads. But it’s not always the best fit for every API workflow, especially when the backend is sensitive to routing behavior or has region‑specific constraints.
Read next
In Development
Flexbox Bukan Jawaban untuk Semua Masalah Layout
Satu dua keluarga kodok, tiap hari bikin layout pakai Flexbox Semenjak Flexbox hadir di tahun 2015, layouting menggunakan CSS semakin dipermudah. Penggunaan...
In Development
Automating OG Images in Next.js
There is a specific kind of dissapointment when you share a link and it shows up as a blank gray box–no image, no context, just a bare URL. I have been there...