Skip to content

perf(cf): cut KV list/write quota burn on health, tenero, and uncached GETs#993

Merged
biwasxyz merged 5 commits into
mainfrom
fix/cf-cost-hygiene
Jun 10, 2026
Merged

perf(cf): cut KV list/write quota burn on health, tenero, and uncached GETs#993
biwasxyz merged 5 commits into
mainfrom
fix/cf-cost-hygiene

Conversation

@biwasxyz

Copy link
Copy Markdown
Contributor

Summary

Three cost-hygiene fixes from a Cloudflare $5/mo Workers plan audit (pricing verified against Cloudflare docs). Goal: keep the bill at $5 by closing the only realistic paths to overage.

1. /api/health — stop full KV list scans per hit

The endpoint ran two full cursor-paginated kv.list() prefix scans (stx: + claim:) on every request, with no-store so nothing was ever cached. List ops are the smallest KV quota on the paid plan (1M/mo, $5/M after), and this is a public endpoint polled by monitors and agents — cost also doubles automatically once the keyspace passes 1,000 keys/prefix.

Now: agent counts are served from caches.default (5-min TTL, via the existing withEdgeCache helper) and KV connectivity is probed with a single keys-read (10M/mo quota). Response shape unchanged, including the agentCount backwards-compat field; counts can be up to 5 min stale.

2. Tenero price refresh — write-on-change

The 5-min refresh wrote every token to KV every tick: ~458k writes/mo at 53 tokens, i.e. ~46% of the 1M/mo write quota from one task. Now a comparison read (reads bill at 1/10th the write rate) gates the put: skip when the price is unchanged and the entry is under 1h old. The hourly forced rewrite keeps fetchedAt honest and renews the 24h TTL safety net, so a stable token can never silently expire. Verified no reader enforces a freshness threshold on fetchedAt before making this change. 3 new tests cover skip / stale-rewrite / changed-price-rewrite.

Trade-off: adds ~458k KV reads/mo against the 10M read quota — net strongly positive.

3. Cache-Control on the two fully uncached GET endpoints

  • /api/get-name: deterministic (same address → same name, no storage reads), now public, max-age=86400, s-maxage=604800, immutable; self-doc response 1h/24h.
  • /api/capabilities: public, max-age=300, s-maxage=3600, stale-while-revalidate=86400 on all three response shapes, mirroring the /api/agents list cadence; errors stay uncached. Collapses the per-request agents:index KV read + per-agent record fan-out.

Testing

  • npm run lint clean (one pre-existing <img> warning on app/page.tsx)
  • npm run test: 96 files / 1515 tests passed (incl. 3 new)
  • npm run build passes

biwasxyz added 5 commits June 10, 2026 13:57
…V list scans

kv.list() ops are the smallest KV quota on the $5/mo plan (1M/mo, $5/M
after) and /api/health ran two full prefix scans per hit with no-store.
Counts now come from caches.default with a 5-min TTL; connectivity is
probed with a single keys-read. Response shape unchanged.
The 5-min refresh wrote every token every tick (~458k KV writes/mo at 53
tokens) against the 1M/mo write quota. Now a comparison read (10x cheaper
than a write) gates the put: skip when priceUsd matches and the entry is
under TENERO_UNCHANGED_REWRITE_MS (1h) old. The hourly forced rewrite
keeps fetchedAt honest for readers and renews the 24h TTL safety net.
Same address always produces the same name, so success responses are
cached long + immutable; the self-doc response gets 1h/24h. Repeat hits
stop invoking the full handler.
Capability declarations change rarely (signed profile updates). An hour
of edge staleness collapses the per-request agents:index KV read and
per-agent record fan-out to once per TTL per colo. Errors stay uncached.
@cloudflare-workers-and-pages

Copy link
Copy Markdown

Deploying with  Cloudflare Workers  Cloudflare Workers

The latest updates on your project. Learn more about integrating Git with Workers.

Status Name Latest Commit Preview URL Updated (UTC)
✅ Deployment successful!
View logs
landing-page 5711a7b Commit Preview URL

Branch Preview URL
Jun 10 2026, 08:15 AM

@biwasxyz biwasxyz merged commit ebffce2 into main Jun 10, 2026
8 checks passed
@biwasxyz biwasxyz deleted the fix/cf-cost-hygiene branch June 10, 2026 08:26
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant