> ## Documentation Index
> Fetch the complete documentation index at: https://langchain-zh.cn/llms.txt
> Use this file to discover all available pages before exploring further.

# Ampersend 集成

> 使 LangChain 代理能够支付并使用远程 AI 代理服务。

[Ampersend](https://ampersend.ai) 使 LangChain 代理能够支付并使用远程 AI 代理服务。支付通过 [x402](https://www.x402.org/) 协议透明处理，[A2A](https://google.github.io/A2A/) 作为通信层。

## 概述

### 集成详情

| 类            | 包                     | 可序列化 | JS 支持 |                                              版本                                              |
| :----------- | :-------------------- | :--: | :---: | :------------------------------------------------------------------------------------------: |
| `A2AToolkit` | `langchain-ampersend` |   ❌  |   ❌   | ![PyPI - 版本](https://img.shields.io/pypi/v/langchain-ampersend?style=flat-square\&label=%20) |

### 工具功能

1. **a2a\_get\_agent\_details** - 获取远程代理的能力
2. **a2a\_send\_message** - 向远程代理发送消息（自动处理支付）

### 关键功能

* **支出控制**：可插拔的支付授权，带有限制和策略
* **透明支付**：x402 协议自动处理支付协商

***

## 设置

### 安装

安装 `langchain-ampersend` 包：

<CodeGroup>
  ```python pip theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
  pip install -U langchain-ampersend
  ```

  ```python uv theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
  uv add langchain-ampersend
  ```
</CodeGroup>

### 凭证

该工具包需要会话密钥和智能账户地址，您可以从 [Ampersend 仪表板](https://app.ampersend.ai) 获取。

```python Set up credentials icon="key" theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
import os

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

***

## 实例化

```python Initialize toolkit icon="robot" theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
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()
```

***

## 调用

向远程代理发送消息：

```python Send message icon="message" theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
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)
```

***

## 在代理中使用

```python Create agent icon="robot" theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
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)
```

示例用法：

```python Run agent icon="rocket" theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
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
```

***

## 支付工作原理

当远程代理需要支付（HTTP 402）时，工具包会：

1. 接收支付要求
2. 调用财务官以授权支付
3. 使用配置的钱包签署支付
4. 重试附带支付的请求

这对您的 LangChain 代理是透明的。

`AmpersendTreasurer` 提供带有支出限制和分析功能的托管支付会话。其他财务官实现可在 `ampersend_sdk` 中找到。

***

## API 参考

* [Ampersend 文档](https://docs.ampersend.ai)
* [x402 协议规范](https://www.x402.org/)

***

<div className="source-links">
  <Callout icon="edit">
    [Edit this page on GitHub](https://github.com/langchain-ai/docs/edit/main/src/i18n\zh-CN\oss\python\integrations\tools\ampersend.mdx) or [file an issue](https://github.com/langchain-ai/docs/issues/new/choose).
  </Callout>

  <Callout icon="terminal-2">
    [Connect these docs](/use-these-docs) to Claude, VSCode, and more via MCP for real-time answers.
  </Callout>
</div>
