Repository navigation
feat(credits): add an opt-in gateway credits ledger - #74
Conversation
CREDITS_ENABLED (off by default) keeps an append-only integer micro-USD ledger per owner: credit, reserve, commit, release, adjust and compensate entries, each idempotent by operation ID and written with a compare-and-swap on the owner's balance revision. Corrections append compensation entries and never rewrite history. Reservations and negative adjustments may not make available credit negative. A commit always records the actual cost and closes its hold, so an overrun leaves available credit negative and new reservations are refused until credits cover it. An unknown cost keeps the hold. GET /v1/credits returns the owner's balance and paged entries, and POST /admin/credits/adjust needs an administrator session and If-Match. The ledger implements the enterprise CreditAuthority; connecting it to request reservations comes with integration (migration 0037).
|
You have reached your Codex usage limits for code reviews. You can see your limits in the Codex usage dashboard. |
Deploying with
|
| Status | Name | Latest Commit | Updated (UTC) |
|---|---|---|---|
| ❌ Deployment failed View logs |
multillm-proxy | 3a2a109 | Oct 09 2026, 08:00 AM |
|
Important Review skippedAuto reviews are disabled on base/target branches other than the default branch. Please check the settings in the CodeRabbit UI or the ⚙️ Run configuration
You can disable this status message by setting the Use the checkbox below for a quick retry:
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
Summary
Adds an opt-in, append-only credits ledger kept separate from usage estimates. With
CREDITS_ENABLEDunset oroff(the default),/v1/creditsand/admin/credits/*return 404 and nothing else changes.CREDITS_ENABLED. Empty means off; a malformed value is off with one warning.CREDITS_CURRENCYdefaults toUSD, the only accepted value.credit,reserve,commit,release,adjustandcompensate, each idempotent by operation ID per owner. The same ID with different content returns 409credits_conflict.credits_insufficient).GET /v1/credits(modelsscope) returns balance, held, available and cursor-paged entries, withCache-Control: no-store.POST /admin/credits/adjustneeds an administrator session, CSRF andIf-Match(428 when missing, 412 when stale). It takes{owner, amount_microusd, operation_id, reason}, writes an audit row and never grants anything automatically.CreditAuthority(reserve/commit/reconcile). Callers pass the owner's current revision; on 412 they re-read it and retry with the same operation ID.Details are in
docs/credits-ledger.md.Wiring
All new files; no existing file changes. Standalone Flask uses SQLite, and D1 deployments use the private credits domain in
worker/credits-d1.mjs. The Wave 7 integration adds:register_credits_routes;Until then, enabling credits only serves the owner read and administrator adjustment routes.
Review fix
The first version applied the no-negative rule to commits too. A request whose actual cost exceeded its hold plus the available balance then failed to commit: the cost was never recorded and the hold never closed. Commits now always record, as described above. Shared Python and Worker vectors cover the overrun and the recovery.
Shared registrations
tests/test_credits_ledger_worker.mjsintest:worker.worker/credits-d1.mjsin the edge module inventory, and the three credit routes in the Flask route inventory.CREDITS_ENABLEDandCREDITS_CURRENCYin the Container env keys.credits_balances,credits_entriesandcredits_auditinREQUIRED_D1_TABLES.Stacked on #73
Migration numbers must be contiguous in each PR, so this branch merges
feat/scim-provisioning(#73, migration 0036), which stacks on #72, #71 and #70. Merge those first. Payment billing stacks on this one.Tests
tests/test_credits_ledger.pyandtests/test_credits_ledger_worker.mjscover:npm run test:worker: 1,840 passed.npm run test:integration: 7 passed.cf:migrations:checkreports 37 contiguous, additive migrations.test:deploy, free routes, client headers and thewrangler deploy --dry-runbundle all pass.tests/test_frontend_build.mjsfails locally because@tailwindcss/cliis not installed. It fails the same way on main.Apply the migration before enabling the flag:
npx wrangler d1 migrations apply multillm-intelligence --remote.CI notes
python/nodejobs: the dependency-audit steps fail on every branch, including main. fix(deps): clear the CI dependency audit advisories #42 fixes that.AdmissionCoordinatorDurable Object migration, whichwrangler versions uploadcannot apply. This is expected untilwrangler deployruns on main.This is part of Wave 7. The README line comes with the Wave 7 integration PR.