Skip to main content

LangChain integration for L402sdk — L402 Lightning payments in AI agents

Project description

l402-langchain

LangChain tools for L402 Lightning payments

PyPI PyPI downloads MIT or Apache-2.0 Licensed

Enable AI agents to autonomously pay for L402-gated APIs using the Lightning Network.

Install

pip install l402-langchain

Quick Start

from l402_langchain import create_l402_client, L402FetchTool, L402BudgetTool

# Create a client backed by LND REST
client = create_l402_client(
    backend="lnd",
    url="https://localhost:8080",
    macaroon="hex-encoded-admin-macaroon",
    budget={"per_request_max": 500, "daily_max": 5000},
)

# Create LangChain tools
fetch_tool = L402FetchTool(client=client)
budget_tool = L402BudgetTool(client=client)

# Use directly
result = fetch_tool.invoke("https://api.example.com/data")
print(result)
# [Paid 100 sats (+0 fee) | status 200]
# {"ok": true}

# Check spending
print(budget_tool.invoke(""))
# Total spent: 100 sats across 1 payment(s).

Tools

L402FetchTool

Makes HTTP requests with automatic L402 payment handling.

Input: URL string (GET) or JSON {"url": "...", "body": "..."} (POST)

# GET
result = fetch_tool.invoke("https://api.example.com/data")

# POST
result = fetch_tool.invoke('{"url": "https://api.example.com/data", "body": "{\"query\": \"BTC\"}"}')

L402BudgetTool

Reports spending: total sats, receipt count, per-endpoint breakdown.

result = budget_tool.invoke("")
# Total spent: 250 sats across 2 payment(s).
#
#   #1: https://api.example.com/weather — 50 sats (+0 fee, status 200, 5ms)
#   #2: https://api.example.com/market — 200 sats (+0 fee, status 200, 3ms)

Callbacks

PaymentCallbackHandler hooks into LangChain's callback system for payment observability:

from l402_langchain import PaymentCallbackHandler

handler = PaymentCallbackHandler(
    on_payment=lambda event: print(f"Paid {event.amount_sats} sats"),
    max_payment_alert=500,
    on_alert=lambda event: print(f"ALERT: {event.amount_sats} sats!"),
)

# Pass to LangChain agent
executor.invoke({"input": "..."}, config={"callbacks": [handler]})

Lightning Backends

from l402_langchain import create_l402_client

# LND REST
client = create_l402_client(backend="lnd", url="https://localhost:8080", macaroon="...")

# Core Lightning (CLN) REST
client = create_l402_client(backend="cln", url="https://localhost:3010", rune="...")

# SwissKnife
client = create_l402_client(backend="swissknife", url="https://app.numeraire.tech", api_key="sk-...")

With LangChain Agents

from langchain_openai import ChatOpenAI
from langchain.agents import create_tool_calling_agent, AgentExecutor
from langchain_core.prompts import ChatPromptTemplate, MessagesPlaceholder
from l402_langchain import create_l402_client, L402FetchTool, L402BudgetTool

client = create_l402_client(
    backend="lnd",
    url="https://localhost:8080",
    macaroon="deadbeef...",
    budget={"per_request_max": 500},
)

tools = [L402FetchTool(client=client), L402BudgetTool(client=client)]
llm = ChatOpenAI(model="gpt-4o")

prompt = ChatPromptTemplate.from_messages([
    ("system", "You can fetch data from L402-gated APIs. "
               "Use l402_fetch for data, l402_check_budget for spending."),
    ("human", "{input}"),
    MessagesPlaceholder("agent_scratchpad"),
])

agent = create_tool_calling_agent(llm, tools, prompt)
executor = AgentExecutor(agent=agent, tools=tools)
response = executor.invoke({"input": "Get weather data, then report costs."})

License

MIT OR Apache-2.0

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

l402sdk_langchain-0.1.0.tar.gz (9.8 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

l402sdk_langchain-0.1.0-py3-none-any.whl (9.2 kB view details)

Uploaded Python 3

File details

Details for the file l402sdk_langchain-0.1.0.tar.gz.

File metadata

  • Download URL: l402sdk_langchain-0.1.0.tar.gz
  • Upload date:
  • Size: 9.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.13

File hashes

Hashes for l402sdk_langchain-0.1.0.tar.gz
Algorithm Hash digest
SHA256 ab919107432a31b670d922e9f3199d725dbb6fa2b94c70901c3007a11e8eac7b
MD5 2267e272f93709eb3184bb0d3be449f7
BLAKE2b-256 eb9c3b032b4fdf7ec2ea2e6f23df4d7f0ac39ab4ee58fb218863b21d3c65afd0

See more details on using hashes here.

File details

Details for the file l402sdk_langchain-0.1.0-py3-none-any.whl.

File metadata

File hashes

Hashes for l402sdk_langchain-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 ff61bccbc0f9c33ddf99caec650c1df32253119d8f43e69c9d5f1cd964dec892
MD5 c6dff128127d8e16e9670f4d0a932b2b
BLAKE2b-256 ad73cb0743132a4d244822837f343338fa14d50a134b87e7aa909283df87bca6

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page