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

# Daytona

> Use Daytona sandbox backends with deepagents for isolated code execution with fast cold starts

[Daytona](https://daytona.io) provides fast-starting sandbox environments with multi-language support. Best for TypeScript and Python development.

## Setup

<CodeGroup>
  ```bash npm theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
  npm install @langchain/daytona
  ```

  ```bash yarn theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
  yarn add @langchain/daytona
  ```

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

### Authentication

Get your API key from [app.daytona.io](https://app.daytona.io).

```bash theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
export DAYTONA_API_KEY=your_api_key
```

Or pass credentials directly:

```typescript theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
const sandbox = await DaytonaSandbox.create({
  auth: { apiKey: "your-api-key-here" },
});
```

## Usage with deepagents

```typescript theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
import { createDeepAgent } from "deepagents";
import { ChatAnthropic } from "@langchain/anthropic";
import { DaytonaSandbox } from "@langchain/daytona";

const sandbox = await DaytonaSandbox.create({
  language: "typescript",
  timeout: 300,
});

try {
  const agent = createDeepAgent({
    model: new ChatAnthropic({ model: "claude-sonnet-4-20250514" }),
    systemPrompt: "You are a coding assistant with sandbox access.",
    backend: sandbox,
  });

  const result = await agent.invoke({
    messages: [{ role: "user", content: "Create a hello world TypeScript app and run it" }],
  });
} finally {
  await sandbox.close();
}
```

## Standalone usage

```typescript theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
import { DaytonaSandbox } from "@langchain/daytona";

const sandbox = await DaytonaSandbox.create({
  language: "typescript",
  timeout: 300,
});

const result = await sandbox.execute("npm --version");
console.log(result.output);

await sandbox.close();
```

## Configuration

| Option             | Type                     | Default        | Description                                                                   |
| ------------------ | ------------------------ | -------------- | ----------------------------------------------------------------------------- |
| `language`         | `string`                 | `"typescript"` | Primary language runtime. Options: `"typescript" \| "python" \| "javascript"` |
| `timeout`          | `number`                 | `300`          | Command timeout in seconds                                                    |
| `target`           | `string`                 | `"us"`         | Region. Options: `"us" \| "eu"`                                               |
| `image`            | `string`                 | -              | Custom Docker image (e.g., `"node:20"`, `"python:3.12"`)                      |
| `snapshot`         | `string`                 | -              | Snapshot name (cannot be used with `image`)                                   |
| `resources`        | `object`                 | -              | CPU, memory, disk allocation (requires `image`)                               |
| `autoStopInterval` | `number`                 | `15`           | Auto-stop after N minutes idle (0 to disable)                                 |
| `labels`           | `Record<string, string>` | -              | Custom labels for organizing sandboxes                                        |
| `initialFiles`     | `Record<string, string>` | -              | Files to create on startup                                                    |

## Custom resources

To customize CPU, memory, or disk, you must specify a Docker image:

```typescript theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
const sandbox = await DaytonaSandbox.create({
  image: "node:20",
  language: "typescript",
  resources: {
    cpu: 4,       // Number of CPUs
    memory: 8,    // GiB
    disk: 50,     // GiB
  },
});
```

## Available regions

| Region | Location      |
| ------ | ------------- |
| `us`   | United States |
| `eu`   | Europe        |

## Accessing the Daytona SDK

For advanced features, access the underlying Daytona SDK:

```typescript theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
const daytonaSandbox = await DaytonaSandbox.create();
const sdk = daytonaSandbox.sandbox;

// Use any Daytona SDK feature directly
const workDir = await sdk.getWorkDir();
const homeDir = await sdk.getUserHomeDir();
await sdk.fs.createFolder("src", "755");
```

## Reconnecting to existing sandboxes

```typescript theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
// Create sandbox with auto-stop interval
const sandbox = await DaytonaSandbox.create({
  language: "typescript",
  autoStopInterval: 60,
});
const sandboxId = sandbox.id;

// Stop the sandbox (keeps it available)
await sandbox.stop();

// Later: reconnect
const reconnected = await DaytonaSandbox.connect(sandboxId);
await reconnected.start();
const result = await reconnected.execute("ls -la");
```

## Sandbox lifecycle

```typescript theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
const sandbox = await DaytonaSandbox.create();

await sandbox.stop();   // Stop (can be restarted)
await sandbox.start();  // Start a stopped sandbox
await sandbox.close();  // Delete permanently
```

## Factory functions

```typescript theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
import { createDaytonaSandboxFactory, createDaytonaSandboxFactoryFromSandbox } from "@langchain/daytona";

// Create new sandbox per invocation
const factory = createDaytonaSandboxFactory({ language: "typescript" });

// Or reuse an existing sandbox across invocations
const sandbox = await DaytonaSandbox.create();
const reuseFactory = createDaytonaSandboxFactoryFromSandbox(sandbox);
```

## Error handling

```typescript theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
import { DaytonaSandboxError } from "@langchain/daytona";

try {
  await sandbox.execute("some command");
} catch (error) {
  if (error instanceof DaytonaSandboxError) {
    switch (error.code) {
      case "NOT_INITIALIZED":
        await sandbox.initialize();
        break;
      case "COMMAND_TIMEOUT":
        console.error("Command took too long");
        break;
      case "AUTHENTICATION_FAILED":
        console.error("Check your Daytona API key");
        break;
    }
  }
}
```

### Error codes

| Code                      | Description                                   |
| ------------------------- | --------------------------------------------- |
| `NOT_INITIALIZED`         | Sandbox not initialized - call `initialize()` |
| `ALREADY_INITIALIZED`     | Cannot initialize twice                       |
| `AUTHENTICATION_FAILED`   | Invalid or missing Daytona API key            |
| `SANDBOX_CREATION_FAILED` | Failed to create sandbox                      |
| `SANDBOX_NOT_FOUND`       | Sandbox ID not found or deleted               |
| `SANDBOX_NOT_STARTED`     | Sandbox is not in started state               |
| `COMMAND_TIMEOUT`         | Command execution timed out                   |
| `COMMAND_FAILED`          | Command execution failed                      |
| `FILE_OPERATION_FAILED`   | File read/write failed                        |
| `RESOURCE_LIMIT_EXCEEDED` | CPU, memory, or storage limits exceeded       |

## Environment variables

| Variable          | Description                   |
| ----------------- | ----------------------------- |
| `DAYTONA_API_KEY` | Daytona API key (required)    |
| `DAYTONA_API_URL` | Custom Daytona API URL        |
| `DAYTONA_TARGET`  | Default target region (us/eu) |

***

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

  <Callout icon="edit">
    [Edit this page on GitHub](https://github.com/langchain-ai/docs/edit/main/src/oss/javascript/integrations/providers/daytona.mdx) or [file an issue](https://github.com/langchain-ai/docs/issues/new/choose).
  </Callout>
</div>
