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

# AI SDK harness

> Run Claude Code, Codex, and other coding agents in Archil sandboxes

The [AI SDK harness](https://ai-sdk.dev/v7/docs/ai-sdk-harnesses/overview) runs coding agents such as Claude Code, Codex, and OpenCode through the AI SDK's `HarnessAgent`. `@archildata/ai-sdk-harness` provides the sandbox they run in: an Archil [sandbox](https://docs.archil.com/compute/sandboxes/introduction.md) with private ports, egress policy, credentials added outside the sandbox, and reusable templates. It works with any harness adapter.

## Setup

```bash theme={null}
npm install ai disk @ai-sdk/harness @archildata/ai-sdk @archildata/ai-sdk-harness
```

Set `ARCHIL_API_KEY` and `ARCHIL_REGION`, or pass an `Archil` client as `client`. Then install the adapter for your agent, such as `@ai-sdk/harness-claude-code`, and set its model credentials, such as `ANTHROPIC_API_KEY` or `AI_GATEWAY_API_KEY`.

## Run Claude Code

```ts theme={null}
import { HarnessAgent } from "@ai-sdk/harness/agent";
import { createClaudeCode } from "@ai-sdk/harness-claude-code";
import { createArchilNetworkSandboxSession } from "@archildata/ai-sdk-harness";
import { tool } from "ai";
import { z } from "zod";

const agent = new HarnessAgent({
  harness: createClaudeCode(),
  id: "archil-demo",
  tools: {
    // Runs in your process; Claude Code calls it from inside the sandbox.
    get_ticket: tool({
      description: "Look up a support ticket by id.",
      inputSchema: z.object({ id: z.string() }),
      execute: async ({ id }) => ({ id, title: "Login page returns 500" }),
    }),
  },
});

const sandboxSession = await createArchilNetworkSandboxSession({
  ports: [4000],
  setupCommands: ["corepack enable pnpm"],
  template: await agent.getSandboxTemplate(),
});
const session = await agent.createSession({ sandboxSession });

try {
  const result = await agent.generate({
    session,
    prompt: "Look up ticket 42, then write a short summary of it to ticket-42.md.",
  });
  console.log(result.text);
} finally {
  await session.destroy();
  await sandboxSession.destroy();
}
```

## Options

`createArchilNetworkSandboxSession` takes any [`sandboxes.create`](https://docs.archil.com/compute/sandboxes/introduction) setting, such as `baseImage`, `vcpuCount`, `memSizeMiB`, `env`, or `mounts`, plus these:

| Option | What it does |
| - | - |
| `ports` | Ports the harness reaches in the sandbox. The harness bridge listens on the first one. |
| `setupCommands` | Shell commands run once on a fresh sandbox before the harness sets it up, such as `corepack enable pnpm` for adapters that install with pnpm. |
| `template` | The adapter's setup, from `agent.getSandboxTemplate()`. See [Templates](#templates). |
| `templateName` | Name for the template sandbox. Defaults to a name derived from the template and the creation settings. |
| `sandboxId` | Name for the sandbox, used to [resume](#resume) it later. |
| `client` | An `Archil` client to use instead of one created from the environment. |

Sandboxes get `IS_SANDBOX=1` in their environment, which lets Claude Code run as the sandbox's root user.

## Ports

Ports stay private. The harness reaches each port through a [port token](https://docs.archil.com/compute/sandboxes/introduction.md) that Archil's proxy checks, so nothing in the sandbox is exposed publicly.

## Network policy and credentials

The harness sets the sandbox's [egress policy](https://docs.archil.com/compute/sandboxes/firewall.md) and adds model credentials to outbound requests through Archil's firewall. The sandbox holds only a placeholder key, and Archil adds the real key to requests to the model provider's host. On the Free plan, the sandbox keeps Archil's managed egress allowlist, and adapters pass credentials into the sandbox instead.

## Templates

With a `template`, the adapter's setup runs once: Archil prepares a template sandbox, stops it, and forks each new session from it. Later sessions with the same settings start from the prepared sandbox, across processes and machines. The template sandbox stays in your account so sessions can fork from it; delete it when you no longer need it.

## Lifecycle

`stop()` pauses the sandbox, keeping its memory and processes. `destroy()` deletes it.

## Resume

To reattach to a sandbox from another process, stop it instead of destroying it, then resume it by its name, the session's `id`:

```ts theme={null}
import { resumeArchilNetworkSandboxSession } from "@archildata/ai-sdk-harness";

const sandboxSession = await resumeArchilNetworkSandboxSession({ sandboxId, ports: [4000] });
```

Pass the same `ports` you created the session with.


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.