AI Agent Bitcoin Spending Budgets: Atomic Reservations
AI Agent Bitcoin Spending Budgets: Atomic Reservations
Research question
Concurrent requests must reserve spending capacity before execution, rather than checking a stale balance independently.
Architecture and trust boundary
Separate the model, business ledger, operator authorization and local wallet executor. The model may propose an action but cannot establish financial evidence or grant itself permission. Keep BTC KEY out of prompts, browser contexts and business logs. A restricted tool does not turn the underlying credential into a restricted key.
Source implementation
1import sqlite3
2
3def reserve(db, intent, amount, cap):
4 if type(amount) is not int or type(cap) is not int or not 0 < amount <= cap <= 21_000_000*100_000_000:
5 raise ValueError('invalid amount or cap')
6 db.execute('BEGIN IMMEDIATE')
7 try:
8 total = db.execute('SELECT COALESCE(SUM(amount),0) FROM reservations').fetchone()[0]
9 if total + amount > cap:
10 raise ValueError('budget exceeded')
11 db.execute('INSERT INTO reservations VALUES (?,?)',(intent,amount))
12 db.commit()
13 except Exception:
14 db.rollback()
15 raise
16
17if __name__ == '__main__':
18 db = sqlite3.connect(':memory:')
19 db.execute('CREATE TABLE reservations (intent TEXT PRIMARY KEY, amount INTEGER NOT NULL)')
20 reserve(db,'a',600,1000)
21 try: reserve(db,'b',500,1000)
22 except ValueError: pass
23 else: raise AssertionError('overspend accepted')Integration with WAB ENGINE
The local executor can request Testnet SegWit balance using POST /api/v5/btc/wallet/balance with X-BTC-KEY and JSON body {"net":"test","type":"segwit"}. A balance is not settlement evidence. Any future SEND must bind authorization to the exact intent, use persistent idempotency and preserve the wallet's signed birth fee law. Treat the sender total and recipient net amount as distinct accounting values under the applicable inclusive fee law; never assume they are equal. No SEND is implemented here.
Limitations and failure handling
This is a single-budget reservation demonstration. The cap must be trusted configuration, not a tool argument. Production needs persistent storage, wallet and time-window scope, reservation settlement and recovery after ambiguous sends. Do not release a reservation merely because a request timed out.
The included assertions exercise synthetic inputs only. No live engine, model or blockchain connection was tested. Fail closed on ambiguous financial state; investigate before retrying writes.
References
- https://thamer.org/api
- https://thamer.org/engine
- https://developer.bitcoin.org/devguide/transactions.html
These links are reference pointers, not a claim of current external documentation verification.
