Skip to main content

wallet-helper

🇫🇷 · 🇬🇧

CI License: BSD-3-Clause Python Local-first

wallet-helper Logo

Never run the same heavy call twice. wallet-helper is persistent memoization for expensive calls (a paid API request, a slow model, any heavy function): an identical call is served from a local store instead of running again, across process restarts. When two identical calls start at the same time, they collapse into one, so the second waits for the first and reuses its result instead of running in parallel (single-flight).

Drop a ⭐️ on GitHub if my help deserves it.

Documentation

💻 Documentation

🗺️ Landscape

📋 Examples

What it does

A heavy call is a problem you do not want to pay for twice. Two things cause a double run:

  1. You call it again next week. wallet-helper stores each result on disk, content-addressed by a namespace plus the inputs (arguments, a file's content, or bytes), so the repeat is served from the store rather than recomputed.
  2. You call it twice at once. Two threads, or two processes, launch the same slow call before either finishes. wallet-helper lets one of them run it and makes the others wait for that result, so the work happens once.

It is content-addressed, so a renamed input file still hits and two different inputs never collide. The default store is a folder of JSON files, easy to read and to delete. A SQLite backend adds a shared, concurrency-safe store and cross-process single-flight. A small HTTP server centralizes that dedup for many clients.

Status

Battle-tested: 142 tests (doctests included), a green CI gate on every push, and semantic-versioned releases on PyPI (see the badges above). What ships today:

  • library with Wallet and the @memoize decorator (sync and async def), over a Ledger (JSON files), a SqliteLedger (one shared file), or a RemoteLedger (an HTTP server). In-process single-flight is built in.

  • cross-process single-flight through the SQLite backend or the server. Three mechanisms keep one execution honest across machines:

    • a fencing token — a random, unique value handed out with each lease. A new leader taking over a stale lease gets a fresh one, so a late write carrying the old value no longer matches and is rejected, and a crashed or stalled leader cannot disrupt the new leader's lease;
    • a lease timeout, so a dead leader never blocks waiters;
    • a heartbeat, so a long job keeps its lease.

    Duplicates coalesce as long as the leader finishes within its lease or keeps a heartbeat.

  • time-to-live and eviction: per-entry ttl, optional stale-while-revalidate, an evict policy by age or entry count, and an automatic size cap (max_entries).

  • wallet-helper: one command, with stats, path, clear, and evict, to inspect and prune the store.

  • HTTP dedup server (the [api] extra) plus RemoteLedger, so many clients on any host share one dedup point.

  • MCP tools (the [mcp] extra): the same HTTP dedup server exposed as MCP tools for any MCP-aware agent host, served by a second command, wallet-helper-mcp.

How it works

How wallet-helper works: the same expensive call, charged once

Installation

The only requirement is Python 3.10 or later. If you need Python itself:

  • 🍎 macOS (Homebrew): brew install python
  • 🐧 Ubuntu/Debian: sudo apt update && sudo apt install -y python3 python3-pip
  • 🪟 Windows (PowerShell): winget install Python.Python.3.12

Install from PyPI:

pip install wallet-helper

Or from source:

git clone https://github.com/warith-harchaoui/wallet-helper.git
cd wallet-helper
pip install -e .

The wallet-helper command ships with the core install. The HTTP surfaces are opt-in extras:

pip install "wallet-helper[api]"   # HTTP dedup server     -> fastapi, uvicorn
pip install "wallet-helper[mcp]"   # MCP tools + the above -> fastapi-mcp

The [mcp] extra adds a second command, wallet-helper-mcp, serving the HTTP API and an /mcp endpoint from the same app, on 127.0.0.1:8012 unless WALLET_HELPER_HOST / WALLET_HELPER_PORT say otherwise.

Quick start

Memoize any function with one line. The result is stored on disk and reused on the next identical call, this run or next week:

from wallet_helper import memoize


@memoize
def transcribe(path):
    return call_some_paid_api(path)  # slow and billed; runs at most once per file


transcribe("meeting.wav")  # runs, stores the result
transcribe("meeting.wav")  # served from the store, no second call

A file argument is keyed by its content, not its path. So the same file reached under a different name, or a byte-for-byte copy in another folder, still hits, and two different files never collide even if their names look alike:

transcribe("meeting.wav")  # runs
transcribe("archive/meeting.wav")  # a copy with the same bytes, served from the store

Ignore an argument that should not change the result, such as a client handle:

@memoize(ignore=("client",))
def fetch(doc_id, client):
    return client.get(doc_id)

Inspect or drop a function's cache, like functools.lru_cache:

transcribe.cache_info()
# {'entries': 1, 'hits': 1, 'lifetime_hits': 1, 'lifetime_misses': 1}
transcribe.cache_clear()  # forget this function's stored results

Two calls, one of them served from the store: one entry, one hit, and one miss — the first call, which had to run.

entries and hits describe what exists now. The lifetime tallies describe everything the store has ever done.

The difference is not pedantry. hits is stored inside each record, so evicting or expiring an entry destroys the record of every call it ever saved. On a store with five entries and a hundred reuses each, evict(max_entries=2) took the reported total from 500 to 200 — sixty per cent of the saved calls gone, with nothing raised.

Since eviction and TTL are this package's own features, a long-running cache under-reported its own value, and did so more the better it was housekeeping. lifetime_hits survives both.

lifetime_misses is what makes the number mean something. A hit count alone cannot say whether a cache is earning its keep — 500 hits is excellent against 520 calls and dismal against 50 000:

info = transcribe.cache_info()
rate = info["lifetime_hits"] / (info["lifetime_hits"] + info["lifetime_misses"])

Set a freshness window with ttl (seconds), and share one store across a fleet by pointing at a running server:

from wallet_helper import Wallet, RemoteLedger, memoize


@memoize(ttl=3600)  # results expire after an hour
def price(symbol):
    return call_pricing_api(symbol)


wallet = Wallet(RemoteLedger("http://cache.internal:8000"))


@memoize(wallet=wallet)  # every host dedups through one server
def transcribe(path):
    return call_some_paid_api(path)

Block a repeat instead of serving it. A paid call that must not be paid twice within a window, yet is a new and legitimate call once the window has passed (the same prompt to the same model, re-measured a month later), takes on_repeat="refuse": within ttl the repeat raises AlreadyPaid and nothing runs; after ttl it runs again.

from wallet_helper import AlreadyPaid, memoize


@memoize(ttl=15 * 86400, on_repeat="refuse")  # blocked for 15 days, then allowed
def ask(model, prompt, replicate):
    return call_paid_llm(model, prompt)


try:
    ask("some/model", "Is the sky blue?", replicate=1)
except AlreadyPaid as e:
    print(e)  # identical call already paid for (date); allowed again after (date)

Async functions work the same. The result is cached, never the coroutine, and concurrent awaits coalesce:

@memoize
async def fetch(url):
    return await http_get(url)

The wallet-helper command inspects and manages the store (it defaults to $WALLET_HELPER_DIR, then ~/.cache/wallet-helper):

wallet-helper stats   # how many results are cached and how many calls they saved
wallet-helper path    # where the store lives
wallet-helper evict --older-than 86400 --max-entries 1000   # prune expired, old or surplus entries
wallet-helper clear   # empty the store

For a shared store and cross-process single-flight, use the SQLite backend or the HTTP server. See EXAMPLES.md.

Built on os-helper

wallet-helper is part of the AI Helpers suite and builds on os-helper for content-addressed hashing, path helpers, temporary folders, and logging. That is one direct dependency, which pulls a few common transitive libraries (requests, pyyaml, tqdm, and so on). wallet-helper is local-first and needs no separate service, but it is not dependency-free.

Architecture

Piece Role
make_key Content hash of a namespace plus a payload (arguments, file content, or bytes).
Ledger Default store: one JSON file per entry.
SqliteLedger One shared SQLite file, atomic reuse counters, TTL, and the claim/submit/release/extend lease.
RemoteLedger A LedgerLike that talks to the server, so Wallet(RemoteLedger(url)) dedups across a fleet.
Wallet / memoize Front door: lookup, single-flight (in-process or via the lease), then store.
wallet_helper.api HTTP server that centralizes dedup for many clients.

Tests

make install   # editable install with the dev and api extras
make lint      # ruff (PEP 8 and import order)
make test      # pytest and doctests
make           # lint then test

CI is deliberately a light gate: the exhaustive multi-version sweep belongs on a contributor's machine before the push, where .githooks/pre-push (enabled once per clone with git config core.hooksPath .githooks) blocks the push on a failing ruff check, ruff format --check or pytest. Two jobs run on every push and pull request, a third once a week.

  • gate — on one Python (3.12) on Linux, with the [dev,api] extras: ruff check . and ruff format --check . (ruff pinned to a known version so a new default rule cannot turn the gate red by surprise), the whole suite, then the install smoke: wallet-helper answers --version and --help, and scripts/smoke_install.py proves a memoize round-trip runs the work exactly once.
  • smoke-os — the same install smoke on 🍎 macOS and 🪟 Windows (Python 3.13, [api] extra, plus the import and its version), every Monday and on manual dispatch rather than on every push: the cross-process test leans on spawn-based multiprocessing, minutes-slow on a Windows runner, and the smoke alone is what "verified on Windows too" means here.
  • floor — pip install . then import wallet_helper on Python 3.10, so the requires-python = ">=3.10" promise has evidence behind it instead of being a claim nobody ever ran.

The Promise

wallet-helper is part of a local-first, sovereignty-minded suite, and like os-helper it is a small toolbox rather than a service. Rather than market that, here is the honest, case-by-case reality:

  1. Guaranteed local. The default Ledger (a folder of JSON files) and the SqliteLedger (one file) live under $WALLET_HELPER_DIR, or ~/.cache/wallet-helper, on your machine. Nothing is uploaded, there is no telemetry, and there is no account. Your cached results, and the inputs that key them, never leave the disk.

  2. Where local is not possible: the caveat. wallet-helper exists to avoid running your heavy call; it makes no network requests of its own. The one exception is by design: the optional [api] dedup server and RemoteLedger speak HTTP so a fleet can share one dedup point, and they talk only to the endpoint you point them at.

  3. Your decision. wallet-helper stores whatever your function returns; if that function calls a paid cloud API, that is your code's choice, never wallet-helper's. Point RemoteLedger at your own host and the shared store stays sovereign; point it at a third party and that too is your call, never a default.

Use it as an agent skill

skills/wallet-helper/ packages wallet-helper as a Claude Skill and an OpenCode skill, so an agent can reach for it on your behalf. See skills/README.md to install it (symlink into ~/.claude/skills/ and ~/.opencode/skills/), and TRIGGERS.md for the exhaustive trigger catalogue — scripts/check_triggers.py keeps the skill's description in sync with it, and the test suite fails if a capability stops being named.

Author

License

wallet-helper is licensed under BSD-3-Clause. See LICENSE.

Metadata

Release files for wallet-helper 1.5.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for wallet-helper 1.5.0
File Size Uploaded Provenance Actions
wallet_helper-1.5.0.tar.gz 78.2 kB Without provenance Details for wallet_helper-1.5.0.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for wallet-helper 1.5.0
File Interpreter ABI Platform Actions
wallet_helper-1.5.0-py3-none-any.whl Python 3 none any Details for wallet_helper-1.5.0-py3-none-any.whl

Total release size: 124.3 kB

Release files / wallet_helper-1.5.0.tar.gz

Download URL wallet_helper-1.5.0.tar.gz
Size 78.2 kB
Tags Source
SHA-256 checksum
How to use checksums
77267510207893f05cb0c03614a7f5c65a3d01da95c9a0a63105fd43e356316f
BLAKE2b-256 checksum
How to use checksums
4614347726d713bfd409db006e5ffb5b09e70e2243f89aa39a81eaf0e74c475d
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.13.13

Release files / wallet_helper-1.5.0-py3-none-any.whl

Download URL wallet_helper-1.5.0-py3-none-any.whl
Size 46.1 kB
Tags Python 3
SHA-256 checksum
How to use checksums
e2d47e57a658d2b5a13b5a9829fb6e0812dcfe657d84deb09264134bb9503da1
BLAKE2b-256 checksum
How to use checksums
fe3e27a8cdeee260344c5036b2c8ab9be9415d1913c26cd2c615e258673bd05e
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.13.13

Security for wallet-helper 1.5.0

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page