THAMER
Sovereign Bitcoin Wallet

WALLET AI BITCOIN

Your Bitcoin Wallet. Your Keys. Your Engine.

Self-hosted Bitcoin software built for developers, applications, and AI agents.

CREATE WALLET

Bitcoin Service Usage: Persistent Idempotent Metering

permalink THAMER
#ai-agents#bitcoin#machine-economy#python

Bitcoin Service Usage: Persistent Idempotent Metering

Deduplicate usage events in a local SQLite ledger.

Design and trust boundary

Use a persistent database in deployment; the in-memory fixture only demonstrates transaction semantics. Authenticate the producer and scope event identifiers to a tenant and service before recording. Repeated identical events are harmless; conflicting replays must be investigated. A usage ledger is not a Bitcoin transaction ledger.

Independent reference implementation

Python · 25 lines
1import sqlite3
2
3def record(db, event_id, units):
4    if not isinstance(event_id, str) or not 0 < len(event_id) <= 128 or type(units) is not int or not 0 < units <= 1000000:
5        raise ValueError("invalid event")
6    with db:
7        db.execute("INSERT INTO usage VALUES (?, ?) ON CONFLICT(id) DO NOTHING", (event_id, units))
8        actual = db.execute("SELECT units FROM usage WHERE id=?", (event_id,)).fetchone()[0]
9        if actual != units:
10            raise ValueError("conflicting replay")
11
12db = sqlite3.connect(":memory:")
13try:
14    db.execute("CREATE TABLE usage(id TEXT PRIMARY KEY, units INTEGER NOT NULL)")
15    record(db, "event-1", 4)
16    record(db, "event-1", 4)
17    assert db.execute("SELECT SUM(units) FROM usage").fetchone()[0] == 4
18    try:
19        record(db, "event-1", 5)
20    except ValueError:
21        pass
22    else:
23        raise AssertionError("conflict accepted")
24finally:
25    db.close()

Operational boundaries

Run with Python 3. The assertions use synthetic data only. No SEND, wallet credentials or live API calls are included. Use a separate authorized execution service for actual settlement. Preserve the WAB Engine API and immutable signed fee law. These are proposed application patterns, not interoperability standards or production financial systems.

Related research

https://thamer.org/learn/bitcoin-for-ai-agents-open-integration-guide

https://thamer.org/api

Questions & Answers

Explore all questions →

Latest from the Lab

THAMER illustrated developer portrait and signature

ABOUT THE DEVELOPER

Independent Software Developer specializing in Bitcoin Script, Golang, and AI Agents.

Software engineering · Independent research · Open-source projects