Token Quota and Cost Settlement

A governance mechanism managing LLM API token consumption, expenditures, and rate limits through upfront reservations and post-run settlements. It prevents budget overruns and autonomously shifts to fallback runners or rest intervals when quotas exhaust.

General Meaning

Commercial large language model APIs bill on a pay-as-you-go basis calculated across input prompt tokens, cached input tokens, and generated output tokens. Even under fixed enterprise subscriptions, providers enforce stringent rate limits and token quotas per hour or day.

Token quota and cost settlement is a financial safeguards pattern that reserves an estimated budget allotment prior to invoking an external API, followed by a deterministic reconciliation against actual telemetry once the call concludes. It also provides automated circuit breaking, triggering graceful fallback chains or worker cool-downs whenever service quotas deplete.

Why It Matters

  • Guaranteed prevention of runaway billing: Imposing strict upfront spending ceilings halts runaway recursive prompts and runaway context expansions before costs explode.
  • Fair quota allocation across distributed nodes: Prevents concurrent agents and servers from triggering throttling errors while sharing common API tiers.
  • Eliminating silent system failures from depleted quotas: Detects quota exhaustion cleanly, facilitating safe worker handoffs rather than fatal crashes.

How the Site Factory Uses It

The Site Factory maintains Cloudflare D1 as its authoritative ledger for a two-phase reservation and settlement accounting architecture (dispatch/src/openai-usage.js). Before initiating an external API call, a worker registers an upfront reservation (reserved_usd, capped at $1 per call under an overarching $100 cap_usd threshold) in D1.

Upon request completion, the worker settles the record with verified token counts (input_tokens, cached_input_tokens, output_tokens), finalized expenditure (actual_usd), and the API response_id. If actual expenses exceed the reservation (reservation_overrun), a cluster-wide shared_stop flag immediately triggers to freeze all runners. For fixed-tier external providers (Claude, Codex, Cursor, Antigravity), workers detecting exhaustion (exit codes 42 or 75) fall back to alternative runners and enter scheduled rest periods.

Caveats & Operational Rules

  • Post-hoc logging fails to stop budget blowouts: Recording costs only after API calls conclude allows concurrent workers to burst past total spending caps before detection; upfront reservation is mandatory.
  • Quota exhaustion is an expected operational constraint, not a crash: Hitting plan limits is routine; systems should handle it cleanly through fallback chains and timed pauses rather than throwing fatal exceptions.

Related Terms

References

  • Book: Martin Kleppmann, *Designing Data-Intensive Applications*, Chapter 12 (The Future of Data Systems: Reliable Two-Phase Commit and Stream Auditing)

Verified: 2026-11-08