| name | cost-optimization |
| description | Optimize Cloudflare Developer Platform costs for Workers CPU vs wall time, D1 operations, KV reads/writes, R2 storage/egress, Durable Object hotspots, Queues, Workflows, Workers AI tokens, and architecture-level trade-offs. Use during design and production reviews.
|
| compatibility | Cloudflare Workers TypeScript projects using Wrangler; verify current Cloudflare APIs, limits, and pricing before production use. |
| metadata | {"source":"Architecting on Cloudflare plus official Cloudflare Developer Platform docs","generated":"2026-04-28"} |
Cost Optimization
Use this skill during architecture review, code review, and production hardening.
Cost model stance
- Optimize architecture first, micro-operations second.
- Workers are usually best when code waits on I/O rather than burning CPU.
- AI inference can dominate costs; manage tokens and model choice early.
- R2 is attractive for blob-heavy and egress-sensitive workloads.
- KV write-heavy designs can become expensive or consistency-fragile.
- Durable Object hot keys can become performance and cost bottlenecks.
Code-level cost rules
- Run independent I/O concurrently with
Promise.all.
- Cache public/deterministic reads where stale data is acceptable.
- Avoid excessive KV writes; batch or write through the source of truth.
- Avoid unbounded D1 queries; select only needed columns and add indexes.
- Stream R2 objects instead of buffering.
- Use Queues/Workflows to defer work and avoid user-visible long requests.
- Set AI
max_tokens; retrieve fewer, better chunks for RAG.
PR review checklist
Add a cost note for changes that introduce:
- New AI model calls.
- New write-heavy KV/D1/Queue paths.
- New Durable Object classes or object-key strategy.
- Large R2 upload/download flows.
- A new Workflow with many steps or retries.
- A new external API call from high-traffic routes.
Cost note template
## Cloudflare cost impact
- Traffic path:
- Expected request volume:
- CPU-heavy work:
- I/O/binding calls per request:
- Storage reads/writes:
- AI model and token cap:
- Cache/TTL strategy:
- Hot-key risk:
- Backpressure/rate limit:
AI cost controls
- Use cheap classifiers/routers before expensive generation.
- Truncate, summarize, or retrieve context rather than passing entire documents.
- Cache safe repeated prompts.
- Use RAG abstention instead of retrying generation blindly.
- Record model, tokens, latency, and tenant/request ID.
Storage cost controls
- D1: index important queries; avoid returning unused columns/rows.
- R2: store blobs once; store metadata in D1; stream responses.
- KV: use for high-read/low-write cache, not write-heavy event streams.
- Queues: batch where safe and make messages small.
- Durable Objects: partition by entity to avoid hot object bottlenecks.
Anti-patterns
- CPU-heavy transforms done synchronously on every request.
- AI prompt includes huge unfiltered context.
- Public static assets served through custom dynamic logic for no reason.
- KV cache invalidated on every request.
- One Durable Object coordinates all tenant traffic.