Create a backend factory that reuses an existing Daytona 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).
createDaytonaSandboxFactoryFromSandbox(sandbox: DaytonaSandbox): BackendFactory| Name | Type | Description |
|---|---|---|
sandbox* | DaytonaSandbox | An existing DaytonaSandbox instance (must be initialized) |
import { createDeepAgent, createFilesystemMiddleware } from "deepagents";
import { DaytonaSandbox, createDaytonaSandboxFactoryFromSandbox } from "@langchain/daytona";
// Create and initialize a sandbox
const sandbox = await DaytonaSandbox.create({ language: "typescript" });
try {
const agent = createDeepAgent({
model: new ChatAnthropic({ model: "claude-sonnet-4-20250514" }),
systemPrompt: "You are a coding assistant.",
middlewares: [
createFilesystemMiddleware({
backend: createDaytonaSandboxFactoryFromSandbox(sandbox),
}),
],
});
await agent.invoke({ messages: [...] });
} finally {
await sandbox.close();
}