---
title: "Harness v4 Engine: Loadouts, Code-Mode MCP, and Environments"
description: "Embed @caveman-ai/harness directly: model-native loadouts (app-gpt, app-claude, app-code, app-general), QuickJS with_mcp code mode, byte-stable cache prefixes, and Local/Docker/Sandbox environments."
canonical: https://docs.caveman.so/agent/harness
product: Caveman Agent SDK
group: Agent SDK & Harness v4
---

# Harness v4 Engine: Loadouts, Code-Mode MCP, and Environments

> Embed @caveman-ai/harness directly: model-native loadouts (app-gpt, app-claude, app-code, app-general), QuickJS with_mcp code mode, byte-stable cache prefixes, and Local/Docker/Sandbox environments.

`@caveman-ai/harness` (also re-exported from `@caveman-ai/agent/harness`) is the open-source agent engine underneath `@caveman-ai/agent` and Caveman Code. It matches tool surfaces to each model family, preserves byte-stable prompt prefixes across turns, isolates subprocess credentials (`inheritEnv: false`), and exits with stable codes when parked.

## Install and run a session

```bash
npm install @caveman-ai/agent
```

```ts
import { tool } from "@caveman-ai/agent";
import {
  HarnessSession,
  LocalEnv,
  appLoadoutFor,
  resolveHarnessModel,
  toHarnessTool,
} from "@caveman-ai/agent/harness";

const { model, stream } = resolveHarnessModel("anthropic/claude-sonnet-4-6");

const recordFinding = tool(
  "record_finding",
  "Record a finding from workspace inspection.",
  { type: "object", properties: { note: { type: "string" } }, required: ["note"] },
  async ({ note }: { note: string }) => ({ recorded: true, note }),
);

const session = new HarnessSession({
  model,
  stream,
  loadout: appLoadoutFor(model),
  env: new LocalEnv({ cwd: process.cwd() }),
  tools: [toHarnessTool(recordFinding)],
});

try {
  const outcome = await session.runTurn("Inspect this repo and record one key finding.", {
    onEvent: (event) => {
      if (event.type === "text_delta") process.stdout.write(event.delta);
    },
  });
  console.log(outcome.finalText, outcome.usage);
} finally {
  await session.close();
}
```

## Model-native loadouts

A loadout defines the exact prompt sections, tool schemas, limits, and wire flags sent to a model. Every loadout hashes canonically (`normalizeLoadout()` + `loadoutHash()`), so benchmarked configurations match production byte for byte.

| Loadout | Execution environment | Built-in tools | Best for |
| --- | --- | --- | --- |
| `app-general` | None (`computer: "none"`) | User-declared host tools & MCP only | Support, triage, structured extraction, non-coding workflows |
| `app-code` | In-process QuickJS | `program` over declared tools | Tool-heavy workflows composing calls in code without a Linux shell |
| `app-claude` | `LocalEnv`, `DockerEnv`, or `SandboxEnv` | Compact 5-tool surface (`read`, `bash`, `wait`, fuzzy `edit`, `write`, `compact`) + 270 s cache keepalive | Coding tasks targeting Claude models |
| `app-gpt` | `LocalEnv`, `DockerEnv`, or `SandboxEnv` | Codex `exec` QuickJS surface (`exec_command`, `apply_patch`, `write_stdin`, `wait`, `view_image`, `compact`) | Coding tasks targeting OpenAI Responses API models |

## Cache-stable prefix contract & QuickJS code-mode MCP

- **Deterministic ordering.** Built-in, custom, and MCP tools serialize in a fixed order so earlier prefix bytes never shift between turns.
- **270 s Anthropic cache keepalive.** Keeps warm prompt caches alive across long-running tool pauses.
- **QuickJS code-mode MCP (`with_mcp`).** Attached MCP servers run inside a sandboxed QuickJS WASM cell with compact one-line tool signatures and `ask()` fan-out, loaded only when MCP servers are connected (`connectMcp()` / `mcpDirectTools()`).
- **Explicit compaction.** The `compact` tool writes a clean handoff frame rather than mutating earlier cached history.

### Published Harness v4 benchmark results

Each optimization in Harness v4 was evaluated on held-out paired tasks (`caveman-ai/agent-sdk` README). Local cost figures are `inferred`, not invoice promises:

| Benchmark | Measured result | Scope |
| --- | --- | --- |
| Lean Harness vs Pi 1.3 | 31% lower $/solve (`0.69x`, 95% CI `0.59-0.79`) at equal `0.584` pass@1 | 73 held-out DeepSWE + Terminal-Bench 4 tasks, `k=3` |
| QuickJS code-mode MCP (`with_mcp`) | `0.33x` direct MCP / `0.29x` Claude Code on Opus; `0.67x` direct / `0.59x` Codex on Sol | Connector benchmark suite at equal task passes |
| Anthropic 270 s cache keepalive | `-6.4%` cost/attempt; cache misses dropped `0.50 -> 0` per trial | `o20` Anthropic cache-expiry benchmark |

## Execution environments (`LocalEnv`, `DockerEnv`, `SandboxEnv`)

Every environment implements the same `ExecEnv` interface so the same agent runs locally, in Docker, or on Caveman Cloud without changing agent code:

| Class | Isolation | Credential handling |
| --- | --- | --- |
| `LocalEnv` | Host process groups (Git Bash on Windows) | Explicit allowlist (`inheritEnv: false`): `PATH`, `HOME`, `USER`, `LANG` plus variables you pass; strips ambient provider keys |
| `DockerEnv` | Local container (`docker exec`) | Container boundary with mounted workspace |
| `SandboxEnv` | Caveman Cloud Firecracker microVM | Lazily provisioned on first tool call; API keys stay in the host harness process and never enter the VM |

```ts
import { SandboxEnv } from "@caveman-ai/harness";

const env = new SandboxEnv({
  controlURL: "https://api.caveman.so",
  apiKey: process.env.CAVEMAN_API_KEY!,
  cwd: "/workspace",
});
```

## Headless exit codes

When driven as a headless subprocess (`caveman-harness`), exit codes tell schedulers when to reclaim compute:

| Exit code | Meaning |
| --- | --- |
| `0` | Turn completed normally |
| `1` | Unrecoverable error |
| `2` | Invalid arguments or configuration |
| `3` | Budget or turn limit reached |
| `4` | Cancelled by caller |
| `5` | Parked waiting for approval, external tool result, or timer |
| `6` | Idle between turns; safe to hibernate and resume from event log |
