Skip to main content
Ampersend 使 LangChain agents 能够付费使用远程 AI agent 服务。支付通过 x402 协议透明处理,通信层使用 A2A

概述

集成详情

可序列化JS 支持版本
A2AToolkitlangchain-ampersendPyPI - Version

工具功能

  1. a2a_get_agent_details - 获取远程 agent 的能力信息
  2. a2a_send_message - 向远程 agent 发送消息(自动处理支付)

主要特性

  • 支出控制:可插拔的支付授权机制,支持限额和策略配置
  • 透明支付:x402 协议自动处理支付协商

配置

安装

安装 langchain-ampersend 包:
pip install -U langchain-ampersend

凭证

工具包需要会话密钥和智能账户地址,可从 Ampersend 控制台 获取。
Set up credentials
import os

SESSION_KEY = os.environ.get("AMPERSEND_SESSION_KEY")  # 0x...
SMART_ACCOUNT_ADDRESS = os.environ.get("AMPERSEND_SMART_ACCOUNT_ADDRESS")  # 0x...

实例化

Initialize toolkit
from langchain_ampersend import (
    A2AToolkit,
    AmpersendTreasurer,
    ApiClient,
    ApiClientOptions,
    SmartAccountConfig,
    SmartAccountWallet,
)

# Setup wallet
wallet = SmartAccountWallet(
    config=SmartAccountConfig(
        session_key=SESSION_KEY,
        smart_account_address=SMART_ACCOUNT_ADDRESS,
    )
)

# Setup treasurer
treasurer = AmpersendTreasurer(
    api_client=ApiClient(
        options=ApiClientOptions(
            base_url="https://api.ampersend.ai",
            session_key_private_key=SESSION_KEY,
        )
    ),
    wallet=wallet,
)

# Create toolkit
toolkit = A2AToolkit(
    remote_agent_url="https://agent.example.com",
    treasurer=treasurer,
)

await toolkit.initialize()

调用

向远程 agent 发送消息:
Send message
tools = toolkit.get_tools()
send_tool = tools[1]  # a2a_send_message
response = await send_tool.ainvoke({"message": "Analyze the sales trends in Q4"})
print(response)

在 Agent 中使用

Create agent
from langchain.agents import create_agent
from langchain_anthropic import ChatAnthropic

# Initialize the LLM
llm = ChatAnthropic(model="claude-sonnet-4-20250514")

# Get tools from the toolkit
tools = toolkit.get_tools()

# Create the agent
agent = create_agent(llm, tools)
使用示例:
Run agent
result = await agent.ainvoke({
    "messages": [("user", "What can this agent do, and then ask it to analyze recent trends")]
})

# The agent will call the remote agent and handle payments automatically

支付工作原理

当远程 agent 需要支付(HTTP 402)时,工具包将:
  1. 接收支付要求
  2. 调用财务管理器授权支付
  3. 使用配置的钱包签署支付
  4. 附带支付信息重新发起请求
整个过程对您的 LangChain agent 透明无感知。 AmpersendTreasurer 提供带有支出限额和数据分析的托管支付会话。其他财务管理器实现可在 ampersend_sdk 中找到。

API 参考