SQLite WAL BEGIN IMMEDIATE Prepaid API Credit Ledger in Pure Python
Read-modify-write credit counters in memory or naive ORM layers fail under parallel bursts, allowing concurrent agent requests to drive prepaid wallets negative. Every API compiled by Monetize-Anything embeds a pure Python 3 stdlib SQLite WAL ledger using BEGIN IMMEDIATE write transactions, schema-enforced CHECK(balance_micros >= 0), integer micro-USD balances ($1 = 1,000,000 micros), and clean connection teardown with zero ResourceWarnings. Verified in our live adversarial Gauntlet (16/16 real non-mocked heterogeneous cases passing in 13546.33 ms total, median 305.8 ms, plus 34/34 unit tests under python3 -W default with 0 warnings).
HTTP 422, and empty or fake-200 errors bill strictly $0.00.
Deterministic Architecture & Billing Invariants
Debits execute strictly AFTER deterministic output verification (never before), inside one BEGIN IMMEDIATE transaction that checks remaining balance, enforces CHECK(balance_micros >= 0), inserts an immutable ledger entry with receipt_sha256, and closes the SQLite handle cleanly.
1-Source → 3-Channel Publishing: Every compiled target emits (1) a self-hosted Creem prepaid storefront + SQLite WAL gateway (3.9% + $0.40 MoR, $0/mo base), (2) a remote Streamable JSON-RPC 2.0 /mcp endpoint + Causal Bazaar public card (b01_monetize_anything_compiler_v1), and (3) a 4-file treg_hub/ merchant pack at 0% commission (treg call monetize-anything).
Data-Backed Comparison & Live Benchmarks
| Ledger Property | Naive Counter / Heavy DB | Monetize-Anything SQLite WAL Ledger | Adversarial Proof |
|---|---|---|---|
| Concurrent Debit Race Safety (G09) | TOCTOU race produces negative balances | BEGIN IMMEDIATE + CHECK(balance_micros >= 0) | 24-thread race: 5 HTTP 200, 19 HTTP 402, min balance 0 (303.93 ms) |
| Sub-Cent Precision | Float drift ($0.00050000001) | Exact integer micro-USD (500 micros = $0.0005) | Deterministic exact accounting |
| Webhook Replay Idempotency (G10) | Duplicate webhook retries double-credit | UNIQUE event_id table inside WAL txn | 8-thread replay storm: 1 credit / 7 replays (8.23 ms) |
| Operational Cost | Managed Postgres + Redis ($30-$80/mo) | $0/mo embedded stdlib sqlite3 WAL | Zero external DB dependencies; 0 ResourceWarnings |
Copy-Paste Integration Example
curl -fsSL -X POST "https://monetize.fastmesh.dev/v1/keys" \
-H "Content-Type: application/json" \
-d '{"tenant_id":"ci_runner_01","topup_micros":19000000,"cap_micro_usd":50000,"window_cap_micros":5000000}'
Deterministic Output
{
"ok": true,
"tenant_id": "ci_runner_01",
"balance_micros": 19000000,
"ledger_mode": "sqlite_wal_begin_immediate"
}
Implementation Steps
- Represent all balances as integer micro-USD: Store $19.00 as 19,000,000 micros so $0.0004-$0.0025 API prices stay exact.
- Acquire BEGIN IMMEDIATE before debiting: Guarantee serialized atomic balance checks and zero overspend across concurrent threads.
- Verify zero warnings under python3 -W default: Close every SQLite connection explicitly after each transaction.
Frequently Asked Questions
Can SQLite WAL handle multi-threaded HTTP concurrency without overspending?
Yes. WAL mode supports concurrent readers while BEGIN IMMEDIATE serializes balance debits atomically, verified in our multi-threaded Gauntlet race test.
Why use micro-USD integers?
Integer micro-USD supports $0.0004-$0.003 per-call pricing (like our 19 verified targets) with zero floating-point rounding drift.
Are ledger transactions tied to SHA-256 receipts?
Yes. Every billed and $0-waived call emits a canonical SHA-256 receipt hash.
support@fastmesh.dev.