createModalSandboxFactoryFromSandbox(sandbox: ModalSandbox): BackendFactory| Name | Type | Description |
|---|---|---|
sandbox* | ModalSandbox | An existing ModalSandbox instance (must be initialized) |
import { createDeepAgent, createFilesystemMiddleware } from "deepagents";
import { ModalSandbox, createModalSandboxFactoryFromSandbox } from "@langchain/modal";
// Create and initialize a sandbox
const sandbox = await ModalSandbox.create({ imageName: "python:3.12-slim" });
try {
const agent = createDeepAgent({
model: new ChatAnthropic({ model: "claude-sonnet-4-20250514" }),
systemPrompt: "You are a coding assistant.",
middlewares: [
createFilesystemMiddleware({
backend: createModalSandboxFactoryFromSandbox(sandbox),
}),
],
});
await agent.invoke({ messages: [...] });
} finally {
await sandbox.close();
}Create a backend factory that reuses an existing Modal Sandbox.
This allows multiple agent invocations to share the same sandbox, avoiding the startup overhead of creating new sandboxes.
Important: You are responsible for managing the sandbox lifecycle
(calling close() when done).