Skip to main content

openlibx402-langchain

LangChain integration for the X402 payment protocol - enables AI agents to autonomously pay for API access.

Overview

The openlibx402-langchain package provides LangChain tools and utilities that enable AI agents to autonomously make payments to access paid APIs using the X402 protocol. Agents can detect payment requirements, process payments, and retry requests automatically.

Features

  • Autonomous payment capability for LangChain agents
  • X402 payment tool for LangChain agent workflows
  • Helper functions for quick agent creation
  • Configurable payment limits for safety
  • Seamless integration with existing LangChain applications

Installation

pip install openlibx402-langchain

Quick Start

Simple Agent with Autonomous Payment

The easiest way to create an X402-enabled agent:

from openlibx402_langchain import create_x402_agent
from langchain_openai import ChatOpenAI
from solders.keypair import Keypair
import json

# Load wallet
with open("wallet.json") as f:
    wallet_data = json.load(f)
    keypair = Keypair.from_bytes(bytes(wallet_data))

# Create agent with X402 support in one function call
agent = create_x402_agent(
    wallet_keypair=keypair,
    llm=ChatOpenAI(temperature=0),
    max_payment="5.0",  # Safety limit
    debug=True,
)

# Agent can now autonomously pay for API access
inputs = {
    "messages": [
        {
            "role": "user",
            "content": "Get the premium data from http://localhost:8000/premium-data"
        }
    ]
}

for chunk in agent.stream(inputs, stream_mode="updates"):
    if "agent" in chunk:
        result = chunk["agent"]

if result and "messages" in result:
    final_message = result["messages"][-1]
    print(f"Agent response: {final_message.content}")

Usage Patterns

Pattern 1: Quick Agent Creation (Recommended)

Best for most use cases - creates a fully configured agent:

from openlibx402_langchain import create_x402_agent
from langchain_openai import ChatOpenAI
from solders.keypair import Keypair

keypair = Keypair()  # Your wallet

agent = create_x402_agent(
    wallet_keypair=keypair,
    llm=ChatOpenAI(temperature=0),
    max_payment="10.0",
    debug=True,
)

# Use the agent
result = agent.invoke({
    "messages": [
        {"role": "user", "content": "Access paid APIs"}
    ]
})

Pattern 2: Custom Agent with Payment Tool

For custom agent configurations:

from openlibx402_langchain import X402PaymentTool
from langchain.agents import create_agent
from langchain_openai import ChatOpenAI
from solders.keypair import Keypair

keypair = Keypair()

# Create X402 payment tool
payment_tool = X402PaymentTool(
    wallet_keypair=keypair,
    max_payment="5.0",
    name="pay_for_api",
    description="Make payment to access premium API data"
)

# Create custom agent with your tools
agent = create_agent(
    model=ChatOpenAI(temperature=0),
    tools=[payment_tool, *other_tools],
    system_prompt="Your custom system prompt",
)

Pattern 3: Multi-API Access

Agent accessing multiple paid APIs:

from openlibx402_langchain import create_x402_agent
from langchain_openai import ChatOpenAI
from solders.keypair import Keypair

keypair = Keypair()

agent = create_x402_agent(
    wallet_keypair=keypair,
    llm=ChatOpenAI(temperature=0),
    max_payment="10.0",  # Higher limit for multiple payments
    debug=True,
)

inputs = {
    "messages": [
        {
            "role": "user",
            "content": "Get data from both /premium-data and /expensive-data APIs, then compare them"
        }
    ]
}

result = agent.invoke(inputs)

Complete Example

from openlibx402_langchain import (
    create_x402_agent,
    X402PaymentTool,
)
from langchain_openai import ChatOpenAI
from solders.keypair import Keypair
import json
import os

# Load wallet keypair
with open("wallet.json") as f:
    wallet_data = json.load(f)
    keypair = Keypair.from_bytes(bytes(wallet_data))

# Create X402-enabled agent
agent = create_x402_agent(
    wallet_keypair=keypair,
    llm=ChatOpenAI(
        temperature=0,
        api_key=os.getenv("OPENAI_API_KEY")
    ),
    max_payment="5.0",
    debug=True,
)

# Run agent
inputs = {
    "messages": [
        {
            "role": "user",
            "content": "Get premium market data from http://localhost:8000/premium-data and summarize it"
        }
    ]
}

for chunk in agent.stream(inputs, stream_mode="updates"):
    if "agent" in chunk:
        result = chunk["agent"]
        if "messages" in result:
            print(result["messages"][-1].content)

Configuration

create_x402_agent Parameters

  • wallet_keypair: Your Solana wallet keypair for payments (required)
  • llm: LangChain LLM instance (required)
  • max_payment: Maximum payment amount - safety limit (required)
  • debug: Enable debug logging (optional)

X402PaymentTool Parameters

  • wallet_keypair: Your Solana wallet keypair
  • max_payment: Maximum payment limit
  • name: Tool name for agent (default: "x402_payment")
  • description: Tool description for agent

Wallet Setup

import json
from solders.keypair import Keypair

# Create new wallet
keypair = Keypair()
wallet_data = list(bytes(keypair))
with open("wallet.json", "w") as f:
    json.dump(wallet_data, f)

print(f"Wallet address: {keypair.pubkey()}")
print("Fund this wallet with SOL and USDC on devnet!")

Documentation

For complete API reference and guides, see:

Testing

pytest tests/

License

MIT License - See LICENSE file for details.

Metadata

Release files for openlibx402-langchain 0.1.1

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for openlibx402-langchain 0.1.1
File Size Uploaded
openlibx402_langchain-0.1.1.tar.gz 6.2 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for openlibx402-langchain 0.1.1
File Interpreter ABI Platform
openlibx402_langchain-0.1.1-py3-none-any.whl Python 3 none any Details

Total release size: 14.7 kB

Release files / openlibx402_langchain-0.1.1.tar.gz

Download URL openlibx402_langchain-0.1.1.tar.gz
Size 6.2 kB
Tags Source
SHA-256 checksum
How to use checksums
8eaaf4567764d23700162b5736560827212788b4d01454404fa6260ec1364cb6
BLAKE2b-256 checksum
How to use checksums
b2e94244edaf36bc76bf110399d1b7f386b228f2e89aa03c9616586342b899cf
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.9.23

Release files / openlibx402_langchain-0.1.1-py3-none-any.whl

Download URL openlibx402_langchain-0.1.1-py3-none-any.whl
Size 8.5 kB
Tags Python 3
SHA-256 checksum
How to use checksums
3a40c2d0dde35b5668b224ad68f38645546ce1b1c66ba40da8d0725873050a0b
BLAKE2b-256 checksum
How to use checksums
29676be87d2712cf590d68d0903aac00b829b69417428aa216b40e3f0c6bccd8
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.9.23

Release history Release notifications | RSS feed

This release

0.1.1 This release

2 release files

0.1.0

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page