Skip to main content

LangChain integration for LightningProx - Pay-per-use AI via Lightning Network micropayments

Project description

langchain-lightningprox

LangChain integration for LightningProx — pay-per-use AI access via Bitcoin Lightning Network micropayments.

No API keys. No accounts. No subscriptions. Just Lightning payments.

Installation

pip install langchain-lightningprox

Quick Start

from langchain_lightningprox import LightningProxLLM

# Initialize with your LNBits wallet
llm = LightningProxLLM(
    lnbits_url="https://demo.lnbits.com",
    lnbits_admin_key="your_admin_key_here"
)

# Use it like any LangChain LLM
response = llm.invoke("Explain quantum computing in one sentence.")
print(response)

How It Works

┌──────────────┐      ┌───────────────┐      ┌─────────────┐
│  Your Code   │ ──▶  │ LightningProx │ ──▶  │  Claude/GPT │
│              │      │               │      │             │
│  (LNBits)    │ ─$─▶ │   (verify)    │      │  (respond)  │
│              │ ◀──  │               │ ◀──  │             │
└──────────────┘      └───────────────┘      └─────────────┘
  1. Your code sends a prompt via the LangChain interface
  2. LightningProx returns a Lightning invoice (~5-50 sats)
  3. The library automatically pays via your LNBits wallet
  4. LightningProx verifies payment and forwards to Claude/GPT
  5. Response returned to your code

All automatic. No manual intervention.

Setup

1. Get an LNBits Wallet

The easiest way to start is with demo.lnbits.com:

  1. Go to https://demo.lnbits.com
  2. Create a new wallet
  3. Click your wallet → API info
  4. Copy the Admin key

2. Fund Your Wallet

Send a small amount of sats (~500) to your wallet's Lightning address. You can use any Lightning wallet (Phoenix, Muun, Cash App, etc.) to send.

3. Use It

from langchain_lightningprox import LightningProxLLM

llm = LightningProxLLM(
    lnbits_url="https://demo.lnbits.com",
    lnbits_admin_key="your_admin_key_here"
)

# Single query
response = llm.invoke("What is Bitcoin?")
print(response)

Configuration

llm = LightningProxLLM(
    # Required
    lnbits_admin_key="your_admin_key",
    
    # Optional (defaults shown)
    lnbits_url="https://demo.lnbits.com",
    model="claude-sonnet-4-20250514",  # or "gpt-4-turbo"
    max_tokens=256,
    api_url="https://lightningprox.com/v1/messages",
)

Available Models

Model Provider Best For
claude-sonnet-4-20250514 Anthropic General use (default)
claude-3-5-sonnet-20241022 Anthropic General use
gpt-4-turbo OpenAI General use

Examples

Basic Usage

from langchain_lightningprox import LightningProxLLM

llm = LightningProxLLM(
    lnbits_url="https://demo.lnbits.com",
    lnbits_admin_key="your_key"
)

# Simple question
answer = llm.invoke("What causes rainbows?")
print(answer)

With Environment Variables

import os
from langchain_lightningprox import LightningProxLLM

llm = LightningProxLLM(
    lnbits_url=os.getenv("LNBITS_URL", "https://demo.lnbits.com"),
    lnbits_admin_key=os.getenv("LNBITS_ADMIN_KEY")
)

Multiple Queries

questions = [
    "What is photosynthesis?",
    "How do airplanes fly?",
    "Why is the sky blue?"
]

for q in questions:
    print(f"Q: {q}")
    print(f"A: {llm.invoke(q)}\n")

Using Different Models

# Use GPT-4 Turbo instead
llm = LightningProxLLM(
    lnbits_admin_key="your_key",
    model="gpt-4-turbo"
)

Pricing

  • ~5-50 sats per request (depending on response length)
  • 50% discount on cached/repeated queries
  • No minimums, no subscriptions

At current rates, 1000 sats ≈ $1 USD gets you roughly 20-200 queries.

Use Cases

Autonomous Agents

Perfect for AI agents that need to pay for their own intelligence:

class ResearchAgent:
    def __init__(self):
        self.llm = LightningProxLLM(
            lnbits_admin_key=os.getenv("AGENT_WALLET_KEY")
        )
    
    def research(self, topic):
        return self.llm.invoke(f"Summarize recent developments in {topic}")

Pay-Per-Use Applications

Build apps where users pay per query without managing API keys:

def answer_question(user_question, user_wallet_key):
    llm = LightningProxLLM(lnbits_admin_key=user_wallet_key)
    return llm.invoke(user_question)

Cost-Controlled Experiments

Test prompts without committing to monthly subscriptions:

# Each query costs ~5-50 sats
# Perfect for experimentation
for prompt_variation in prompt_variations:
    result = llm.invoke(prompt_variation)
    evaluate(result)

Alternative Wallet Providers

While this library uses LNBits by default, you can integrate other Lightning wallets by subclassing:

class StrikeLightningProx(LightningProxLLM):
    def _pay_invoice(self, payment_request: str) -> bool:
        # Implement Strike API payment
        pass

Supported wallet APIs:

  • LNBits (built-in)
  • Strike (subclass)
  • Voltage (subclass)
  • LND (subclass)
  • Any wallet with a payment API

Error Handling

from langchain_lightningprox import LightningProxLLM

llm = LightningProxLLM(lnbits_admin_key="your_key")

try:
    response = llm.invoke("Hello!")
except RuntimeError as e:
    if "Payment failed" in str(e):
        print("Insufficient balance in wallet")
    else:
        print(f"Error: {e}")

Why Lightning Payments?

  • Micropayments — Pay fractions of a cent per request
  • No accounts — Payment IS authentication
  • Instant — Settlements in milliseconds
  • Global — Works anywhere, no banking required
  • Autonomous — Agents can pay without human intervention

Links

License

MIT

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

langchain_lightningprox-0.1.0.tar.gz (6.4 kB view details)

Uploaded Source

Built Distribution

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

langchain_lightningprox-0.1.0-py3-none-any.whl (6.8 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: langchain_lightningprox-0.1.0.tar.gz
  • Upload date:
  • Size: 6.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.3

File hashes

Hashes for langchain_lightningprox-0.1.0.tar.gz
Algorithm Hash digest
SHA256 3e14a63d9ebcfb926a624522a1836046b8a22ad34a157576a0813ec74e856ddd
MD5 06ecbb6d97aa0907f9d02dca83fa3b38
BLAKE2b-256 66147f0b539b0078b0c31c385a39b0f091f5dd1f6e691ec81b16085ab56f8251

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for langchain_lightningprox-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 3f5d2194f10969e7f9d0ddf5e8ca062561ad052dc7df22e66b00a219c2615452
MD5 a98b194bad3d5fa6b8fb8439db2e0ae8
BLAKE2b-256 c5abeb1f3943f66c3aebd5e002914b4e30e528d887d2d28a45a2226812dd3a51

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