# Cost Optimization

> 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.

- Skill: `zllovesuki/cost-optimization` (Agent Skill)
- Install (CLI): `npx skillmds@latest add zllovesuki/cost-optimization`
- Raw SKILL.md: https://api.skillmd.com/api/skills/zllovesuki/cost-optimization/raw
- Safety review: pending
- Works with: Claude Code, Claude.ai, OpenAI Codex
- Category: Coding & Dev Tools
- Author: zllovesuki (https://skillmd.com/u/zllovesuki)
- Updated: 2026-09-22
- Page: https://skillmd.com/skills/zllovesuki/cost-optimization

---

# 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

```markdown
## 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.

