Concept
An L402 token (macaroon:preimage) is a portable string — once paid, it can be passed to any sub-agent that needs access to the same resource. The orchestrator holds the wallet; sub-agents only receive a token.
Tokens expire after 1 hour. Each preimage can only be used once per server (replay protection). For parallel sub-agents hitting the same endpoint, each needs its own payment.
When to share a token vs. pay independently
There are two delegation patterns, each with different trade-offs: Token sharing works when sub-agents access the same endpoint sequentially. The orchestrator pays once, extracts theAuthorization header value, and passes it as a string. Sub-agents call the endpoint with that header directly — no wallet required. This minimizes cost (one payment) but is not safe for concurrent access — only one agent can hold a given preimage, and servers reject replays.
Independent payment (each sub-agent has its own L402Client sharing the orchestrator’s wallet) works for parallel or concurrent access. Each agent pays its own invoice and caches its own token. This costs more sats but is simpler to reason about and safe at any concurrency level.
Rule of thumb: If your sub-agents fan out in parallel, give each its own L402Client. If they run sequentially and hit the same endpoint, share the token string.
Security considerations
- Never pass wallet credentials to sub-agents. Pass only the token string (
L402 <macaroon>:<preimage>). A token can call one endpoint for up to one hour — a private key can drain a wallet. - Tokens are single-use per server. After the first accepted request, the server records the preimage hash. A second request with the same preimage returns 402. This prevents replay attacks but means you cannot share one token across two concurrent sub-agents hitting the same server.
- Budget control is at the wallet level. Set
budgetSatson the orchestrator’sL402Clientto cap total spend across all sub-agents sharing that wallet. See Budget Control.
TypeScript — orchestrator pays, sub-agent uses token
Python — orchestrator pays, sub-agent uses token
Multi-agent pattern: one payment, parallel reads
If multiple sub-agents need the same resource concurrently, the cleanest pattern is for each sub-agent to pay independently using its ownL402Client with a shared wallet:
Security properties
| Property | How it works today |
|---|---|
| Wallet isolation | Sub-agents receive a token string, never private keys |
| Time-bound | Macaroons expire after 1 hour |
| Replay-safe | Each preimage can only be accepted once by the server |
| Budget control | Set budgetSats on the orchestrator L402Client |