From a clean environment to a metered function with a running usage count. Every block is copy-pasteable. The first 100 calls run free in paper mode, so you can complete this without a card.
Requires Python 3.9 or newer. The package has zero runtime dependencies and installs in a few seconds.
# in your project or virtualenv
pip install nano-empire-tollbooth
Pick any function that costs money to run, such as an LLM call or an API wrapper, and add one line. Set the price per call in USD.
from nano_empire_tollbooth import monetize @monetize(price_usd=0.01) def summarize(text: str) -> str: # your real work goes here return text[:120]
Call it like a normal function. In paper mode nothing is charged. Each call is recorded to a local JSONL ledger.
for i in range(3): print(summarize("a long article to shorten"))
Read the running count per function at any time.
from nano_empire_tollbooth import get_usage print(get_usage())
You have 100 free paper-mode calls. As you approach the limit you will see a prompt to upgrade to Tollbooth Pro.
When you are ready to settle real payments, turn paper mode off. You can do this with one environment variable, no code change.
# enable live settlement
export TOLLBOOTH_PAPER_MODE=false
Or configure it explicitly, including an optional x402 settlement hook and a per-agent daily cap.
from nano_empire_tollbooth import TollboothConfig, create_tollbooth config = TollboothConfig( toll_per_message_usd=0.01, paper_mode=False, max_daily_toll_per_agent=10.0, ) booth = create_tollbooth(config)
Live settlement via x402 is experimental: the package ships the verifier hook, not a hosted backend. Payment and crypto content is informational and is not financial advice.
That is the whole loop. Install, decorate, run, count, settle.
Get it on PyPI