◉CLAUDE LABJP
●2.1.301 — Adds the vim % motion, LaTeX math rendering in the VS Code panel, and a retry-watchdog limit for usage-limit waits (10/15)●2.1.299 — Conversations over 200K tokens left idle about 54 minutes get compacted; MCP allow rules now match only the server they name●SONNET 4.5 — Retires on the Claude API on 11/30, 45 days left. Migrate to Sonnet 5.5●WINDOWS — Reports say hooks break silently only on Japanese-locale Windows; check the character encoding first●FAST MODE — Opus 5.5 fast mode is out of research preview and no longer needs the beta header (10/15)●NEW — I Put a Ten-Line Promise File at the To…●2.1.301 — Adds the vim % motion, LaTeX math rendering in the VS Code panel, and a retry-watchdog limit for usage-limit waits (10/15)●2.1.299 — Conversations over 200K tokens left idle about 54 minutes get compacted; MCP allow rules now match only the server they name●SONNET 4.5 — Retires on the Claude API on 11/30, 45 days left. Migrate to Sonnet 5.5●WINDOWS — Reports say hooks break silently only on Japanese-locale Windows; check the character encoding first●FAST MODE — Opus 5.5 fast mode is out of research preview and no longer needs the beta header (10/15)●NEW — I Put a Ten-Line Promise File at the To…
Articles/API & SDK
⬡ API & SDK/2026-04-30Advanced

Building a Production Claude API Pipeline on Cloudflare Queues: Fault Tolerance, Backpressure, and Cost Control

A practical, code-first walkthrough for routing Claude API calls through Cloudflare Queues — covering producer/consumer code, retry-vs-DLQ branching, priority lanes, and token budgeting for production workloads.

claude-api83cloudflare-queuescloudflare-workers5asyncproduction111rate-limit7observability21

✦ Premium Article

"All I want is to summarize 100 uploaded PDFs with Claude — but the Worker dies after 10 seconds." That was a real production incident I worked through, and the cause was painfully simple: a Cloudflare Worker's CPU budget collided with Claude's response latency. Some users sat watching a spinner that would never resolve.

This article is the cleaned-up version of the lesson I took from that incident: never call Claude API directly from a request handler. Cloudflare Queues, used carefully, gives you a place to absorb the impedance mismatch between user-facing latency and model latency. We'll build the pipeline end-to-end with copy-pasteable code, and we'll cover the parts that usually go wrong — retry logic, dead-letter handling, priority lanes, and token budgeting.

Why a request handler is the wrong place to call Claude

Cloudflare Workers cap CPU time at 50ms (free) or up to 30 seconds (paid). That's CPU time, not wall-clock, but for a single Claude API call you're realistically waiting 20–60 seconds of wall time on long outputs. Calling the API synchronously from a handler causes three failures at once:

  • The Worker exits with Exceeded CPU limit: the user gets a 502, no charge to you, but the work is also gone.
  • Client retries cascade: browsers and SDK retries kick in, and one logical job becomes 3–4 billable Claude calls.
  • 429s and 529s pile on: bursty retries hit the rate limit, and now unrelated users in the same org start failing too.

The fix is structurally simple. The handler queues a job and returns immediately. A separate consumer Worker picks the job up and calls Claude. User-perceived latency and model latency are now decoupled. If your workload is purely batch and you don't need user-facing immediacy, the Claude API Messages Batches endpoint is a fine alternative — but for any pipeline where individual user actions need to be tracked, Queues is the better fit.

Pipeline shape

The architecture has three layers:

  • Producer: receives the user request, writes the job to D1, drops a message onto a queue, returns 202 immediately.
  • Consumer: pulls messages off the queue, calls Claude, persists results to R2, updates D1.
  • Observability: failed jobs go to a dead-letter queue (DLQ), and we ship metrics to Logpush so we can alert when failure rates climb.

To keep interactive workloads from being starved by bulk imports, we run two queues: claude-jobs-high (chat-style, foreground work) and claude-jobs-bulk (background batches, uploads). Both feed the same consumer codebase but with different max_concurrency settings.

✦

Thank you for reading this far.

Continue Reading

What follows includes implementation code, benchmarks, and practical content we hope you'll find useful. This site runs without ads — server and development costs are supported entirely by members like you. If it's been helpful, we'd be truly grateful for your support.

WHAT YOU'LL LEARN
✦If you've struggled to fan out hundreds of Claude API calls without melting your Worker, you'll have a working pipeline by the end of the day
✦You'll learn the exact ack vs retry vs DLQ branching for 429s, 529s, and timeouts — copy-pasteable, with the reasoning baked in
✦By combining priority lanes with a Durable Object token budget, you'll be able to absorb traffic spikes and bulk uploads without trampling your interactive users
Secure payment via Stripe · Cancel anytime
✦

Unlock This Article

Get full access to the rest of this article. Buy once, read anytime. This site is ad-free — your support goes directly toward keeping it running.

or
Unlock all articles with Membership →
Share

Thank You for Reading

Claude Lab is ad-free, supported entirely by members like you. We publish practical guides daily with implementation code, benchmarks, and production-ready patterns. If you've found it useful, we'd love to have you on board.

  • ✦Copy-paste ready implementation code
  • ✦New advanced guides published daily
  • ✦$5/mo or $10 for lifetime access
View Membership →

Related Articles

⬡ API & SDK2026-05-02
Building a Budget Circuit Breaker for Claude API in Production — Auto-Halt When Daily Token Spend Exceeds Your Cap
A practical guide to enforcing daily and monthly Claude API budget caps in production. Includes copy-paste Cloudflare Workers + KV / Durable Objects code, three response strategies (halt, degrade, alert), and the operational habits that keep the breaker honest.
⬡ API & SDK2026-05-01
Claude API Telemetry on ClickHouse: A Production Guide to Cost, Latency, and Error Analytics
Stream per-request Claude API telemetry into ClickHouse, build sub-second dashboards with materialized views, and detect cost spikes, retry loops, and silent failures with practical SQL recipes.
⬡ API & SDK2026-04-29
Production Semantic Cache for Claude API — Similarity Thresholds, Pollution Defense, and What to Track
A production playbook for adding a semantic cache in front of Claude API — threshold tuning, multi-tenant isolation, pollution prevention, fallbacks, how it stacks with Anthropic's prompt caching, and the metrics that actually prove it works.