Skip to content

Add autonomous L402 agent for machine-to-machine payments#214

Open
hasky00 wants to merge 1 commit into
lightninglabs:masterfrom
hasky00:claude/explain-codebase-01nBx
Open

Add autonomous L402 agent for machine-to-machine payments#214
hasky00 wants to merge 1 commit into
lightninglabs:masterfrom
hasky00:claude/explain-codebase-01nBx

Conversation

@hasky00
Copy link
Copy Markdown

@hasky00 hasky00 commented Mar 8, 2026

Summary

This PR introduces a new agent package that enables AI agents to autonomously access L402-protected APIs through automatic Lightning payments. Agents can now make authenticated requests to any L402-protected service without human intervention, API keys, or OAuth flows — only a Lightning wallet connection is required.

Key Changes

  • New agent.Agent type: Main entry point for AI agents. Manages LND connection, token caching, and HTTP client lifecycle. Agents are initialized with minimal configuration (LND host, TLS cert, macaroon path) and can immediately start making authenticated requests.

  • L402-aware HTTP client (agent.Client): Automatically handles 402 Payment Required responses by:

    • Parsing L402 challenges from WWW-Authenticate headers
    • Extracting and validating Lightning invoices
    • Paying invoices via LND to obtain preimages
    • Retrying requests with L402 credentials (macaroon + preimage)
    • Caching tokens per service to avoid re-payment
  • Token store (agent.TokenStore): Thread-safe in-memory credential cache keyed by service URL. Allows agents to reuse paid L402 tokens across multiple requests without re-paying.

  • Credential management (agent.Credential): Encapsulates the minimal L402 authentication data (macaroon + preimage) along with payment metadata (amount, fee, hash, timestamp).

  • Configuration (agent.Config): Defines agent parameters including spending limits (max cost per token, max routing fee), timeouts, network selection, and LND connection details. Includes sensible defaults and validation.

  • Logging integration: Added agent/log.go with btclog integration and updated root log.go to initialize agent logging.

Notable Implementation Details

  • Minimal authentication model: L402 tokens require only a macaroon (from server) and preimage (proof of payment) — no usernames, passwords, or shared secrets beyond the Lightning payment itself.

  • Cost controls: Agents enforce configurable spending limits (MaxCost, MaxRoutingFee) to prevent runaway payments.

  • Service-scoped tokens: Credentials are cached per service (scheme + host) to support agents accessing multiple L402-protected APIs independently.

  • Automatic retry logic: Failed requests with expired/invalid cached tokens trigger re-acquisition of new credentials transparently.

  • Convenience methods: High-level Agent.Call() method for simple GET requests and Client.Get()/Client.Post() for more control.

  • Example usage: Includes comprehensive examples demonstrating agent-to-agent communication and multi-service access patterns.

https://claude.ai/code/session_01VfRqwYxEdvTFAu6Kkc89c2

…yments

Add a new `agent` package that enables AI agents to autonomously access
L402-protected APIs via Lightning payments with minimal authentication
information (just macaroon + preimage, no API keys or OAuth).

Key components:
- agent.go: Agent lifecycle management with LND connection
- client.go: L402-aware HTTP client that auto-handles 402 challenges,
  pays invoices, and retries with tokens
- token_store.go: Thread-safe credential cache with Credential type
- config.go: Minimal config (LND connection + spending limits)
- log.go: Logging subsystem registered with aperture logger
- example_test.go: Usage examples for agent-to-agent payments

https://claude.ai/code/session_01VfRqwYxEdvTFAu6Kkc89c2
@hasky00
Copy link
Copy Markdown
Author

hasky00 commented Mar 8, 2026

all good , pre-elimenary test before deploy .the minium information required , the more delicious is the macaroon .

Copy link
Copy Markdown
Author

@hasky00 hasky00 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

great

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants