I Built an Agent That Trades on Bitcoin Lightning. It Remembered Nothing. So I Built a Brain.

By Jason Sosa | 2026-03-13 | Bitcoin, AI Agents, Lightning, Open Source

L402 lets AI agents pay for APIs with Lightning. But they forget everything between sessions. I built Lightning Memory to fix that. Interactive demo included.

The Problem

L402 enables AI agents to pay for API calls using Lightning Network without credit cards, OAuth, or subscriptions. However, every agent using L402 faces a critical limitation: memory loss between sessions.

An agent discovering a transcription API costing 2,000 sats (~$1.42) per request might use it repeatedly within one session. But when the session ends, all records disappear. The next session requires starting from scratch, searching for APIs again, likely finding worse options.

An agent managing a real budget, spending millions of sats a week on compute, data, and API access, is flying blind between sessions. No vendor history. No spending patterns.

This gap led to the creation of Lightning Memory.

What Lightning Memory Does

Lightning Memory functions as an MCP (Model Context Protocol) server with nine tools. Installation is simple:

pip install lightning-memory

The system operates across three layers:

Memory Layer

Agents store transaction data, vendor notes, errors, and preferences in local SQLite with FTS5 full-text search. Data remains on your machine, nothing is uploaded to the cloud.

memory_store(
  content="transcribe-api.com, 2000 sats for 10min audio. Response under 3 seconds",
  type="transaction",
  metadata='{"vendor": "transcribe-api.com", "amount_sats": 2000}'
)

Intelligence Layer

Four tools convert raw memories into actionable insights. This includes vendor reputation scoring, spending analysis, and anomaly detection that flags unusual pricing patterns.

ln_anomaly_check(vendor="transcribe-api.com", amount_sats=50000)
# {verdict: "high", context: "50,000 sats is 25x the historical average"}

Identity Layer

Each agent receives a Nostr keypair using BIP-340 Schnorr signatures, the same cryptography underlying the Nostr protocol. Memories sync as NIP-78 events to relays, making agent identity and data portable across platforms.

The L402 Gateway

A hosted HTTP gateway allows agents to pay sats querying other agents' memories using the L402 protocol:

GET /ln/vendor/transcribe-api.com
→ 402 Payment Required (invoice for 3 sats)
(agent pays invoice)
→ 200 OK
→ {total_txns: 47, success_rate: 0.94, avg_sats: 2100}

An agent that has spent weeks interacting with L402 APIs has built up real intelligence about which vendors are reliable and what fair prices look like. That intelligence is valuable. The gateway enables agents to monetize their knowledge through peer-to-peer sats transactions without intermediaries.

Why Nostr for Identity

I deliberately selected Nostr keypairs over traditional authentication (API keys, JWTs) because agent identity control shapes the emerging economy. If major platforms control agent identity, they control that economy.

Nostr keypairs offer sovereignty, a 32-byte private key stored locally, immune to platform revocation. The existing Nostr relay infrastructure already supports identity portability and syncing.

Implementation Details

The project includes:

  • Nine tools with 156 tests
  • MIT license
  • MCP registry listing
  • Compatibility with Claude and any MCP-compatible agent

Gateway requirements: Phoenixd (ACINQ's zero-config Lightning node) generates invoices. Core memory and intelligence tools function independently.

Getting Started

Installation from PyPI:

pip install lightning-memory

Or from source:

git clone https://github.com/singularityjason/lightning-memory
cd lightning-memory
pip install -e .

Run as MCP server:

lightning-memory

Configure in Claude Code by adding to ~/.claude.json under your project key.

I'd love to hear feedback from Lightning builders, hit me up via GitHub issues or on X (@jasonsosa).

Related: Your Claude AI Agent Has Amnesia. OMEGA Remembers.