以下示例展示了使用 Daytona 的获取或创建模式。对于其他提供商,请查阅沙盒提供商 API 以获取等效的标签、元数据和 TTL 选项。
from langchain_core.utils.uuid import uuid7from daytona import CreateSandboxFromSnapshotParams, Daytonafrom deepagents import create_deep_agentfrom langchain_daytona import DaytonaSandboxclient = Daytona()thread_id = str(uuid7())# Get or create sandbox by thread_idtry: sandbox = client.find_one(labels={"thread_id": thread_id})except Exception: params = CreateSandboxFromSnapshotParams( labels={"thread_id": thread_id}, # Add TTL so the sandbox is cleaned up when idle auto_delete_interval=3600, ) sandbox = client.create(params)backend = DaytonaSandbox(sandbox=sandbox)agent = create_deep_agent( model="google_genai:gemini-3.1-pro-preview", backend=backend, system_prompt="You are a coding assistant with sandbox access. You can create and run code in the sandbox.",)try: result = agent.invoke( { "messages": [ { "role": "user", "content": "Create a hello world Python script and run it", } ] }, config={ "configurable": { "thread_id": thread_id, } }, ) print(result["messages"][-1].content)except Exception: # Optional: delete the sandbox proactively on an exception client.delete(sandbox) raise
智能体在您的机器或服务器上运行。当它需要执行代码时,它会调用沙盒工具(例如 execute、read_file 或 write_file),这些工具会调用提供商的 API 以在远程沙盒中运行操作。优点:
✅ 无需重建镜像即可即时更新智能体代码。
✅ 智能体状态和执行之间更清晰的分离。
API 密钥保留在沙盒外部。
沙盒故障不会丢失智能体状态。
可以选择在多个沙盒中并行运行任务。
✅ 只按执行时间付费。
权衡
🔴 每次执行调用都有网络延迟。
示例
from daytona import Daytonafrom deepagents import create_deep_agentfrom dotenv import load_dotenvfrom langchain_daytona import DaytonaSandboxload_dotenv()# Can also do this with AgentCore, E2B, Runloop, Modalsandbox = Daytona().create()backend = DaytonaSandbox(sandbox=sandbox)agent = create_deep_agent( model="google_genai:gemini-3.1-pro-preview", backend=backend, system_prompt="You are a coding assistant with sandbox access. You can create and run code in the sandbox.",)try: result = agent.invoke( { "messages": [ { "role": "user", "content": "Create a hello world Python script and run it", } ] } ) print(result["messages"][-1].content)except Exception: # Optional: delete the sandbox proactively on an exception sandbox.stop() raise
本文档中的示例使用沙盒作为工具模式。当您的提供商 SDK 处理通信层并且您希望生产环境与本地开发保持一致时,请选择沙盒内智能体模式。当您需要快速迭代智能体逻辑、将 API 密钥保留在沙盒外部或更喜欢更清晰的职责分离时,请选择沙盒作为工具模式。
from daytona import Daytonafrom langchain_daytona import DaytonaSandboxsandbox = Daytona().create()backend = DaytonaSandbox(sandbox=sandbox)results = backend.download_files(["/src/index.py", "/output.txt"])for result in results: if result.content is not None: print(f"{result.path}: {result.content.decode()}") else: print(f"Failed to download {result.path}: {result.error}")
import modalfrom langchain_modal import ModalSandboxapp = modal.App.lookup("your-app")modal_sandbox = modal.Sandbox.create(app=app)backend = ModalSandbox(sandbox=modal_sandbox)results = backend.download_files(["/src/index.py", "/output.txt"])for result in results: if result.content is not None: print(f"{result.path}: {result.content.decode()}") else: print(f"Failed to download {result.path}: {result.error}")
pip install langchain-runloop
from runloop_api_client import RunloopSDKfrom langchain_runloop import RunloopSandboxapi_key = "..."client = RunloopSDK(bearer_token=api_key)devbox = client.devbox.create()backend = RunloopSandbox(devbox=devbox)results = backend.download_files(["/src/index.py", "/output.txt"])for result in results: if result.content is not None: print(f"{result.path}: {result.content.decode()}") else: print(f"Failed to download {result.path}: {result.error}")
pip install langchain-agentcore-codeinterpreter
from bedrock_agentcore.tools.code_interpreter_client import CodeInterpreterfrom langchain_agentcore_codeinterpreter import AgentCoreSandboxinterpreter = CodeInterpreter(region="us-west-2")interpreter.start()backend = AgentCoreSandbox(interpreter=interpreter)results = backend.download_files(["hello.py"])for result in results: if result.content is not None: print(f"{result.path}: {result.content.decode()}") else: print(f"Failed to download {result.path}: {result.error}")interpreter.stop()
from langsmith.sandbox import SandboxClientfrom deepagents.backends.langsmith import LangSmithSandboxclient = SandboxClient()ls_sandbox = client.create_sandbox(template_name="deepagents-deploy")backend = LangSmithSandbox(sandbox=ls_sandbox)results = backend.download_files(["/src/index.py", "/output.txt"])for result in results: if result.content is not None: print(f"{result.path}: {result.content.decode()}") else: print(f"Failed to download {result.path}: {result.error}")