> ## 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.

# ModalSandbox 集成

> 使用 LangChain Python 与 ModalSandbox 沙箱后端进行集成。

[Modal](https://modal.com) 提供带有 GPU 支持的无服务器容器基础设施。有关注册、身份验证和平台详细信息，请参阅 [Modal 文档](https://modal.com/docs)。

## 安装

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

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

## 创建沙箱后端

在 Python 中，您使用提供商 SDK 创建沙箱，然后将其封装到 [deepagents 后端](/oss/python/deepagents/backends) 中。

```python theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
import modal

from langchain_modal import ModalSandbox

app = modal.App.lookup("your-app")
modal_sandbox = modal.Sandbox.create(app=app)
backend = ModalSandbox(sandbox=modal_sandbox)

result = backend.execute("echo hello")
print(result.output)
```

## 与 Deep Agents 配合使用

```python theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
import modal
from langchain_anthropic import ChatAnthropic

from deepagents import create_deep_agent
from langchain_modal import ModalSandbox

app = modal.App.lookup("your-app")
modal_sandbox = modal.Sandbox.create(app=app)
backend = ModalSandbox(sandbox=modal_sandbox)

agent = create_deep_agent(
    model=ChatAnthropic(model="claude-sonnet-4-20250514"),
    system_prompt="You are a coding assistant with sandbox access.",
    backend=backend,
)

result = agent.invoke(
    {"messages": [{"role": "user", "content": "Install numpy and calculate pi"}]}
)
```

## 清理

您负责通过 Modal 管理沙箱的生命周期。完成后，请终止沙箱。

另见：[沙箱](/oss/python/deepagents/sandboxes)。

***

<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\sandboxes\modal.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>
