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

# Devin SDK example: harness chat

> A Next.js chat where the AI SDK HarnessAgent runs Devin inside a Vercel Sandbox, streamed to useChat, with the session kept across requests.

> [!NOTE]
>
> Source:
> [`harness-chat`](https://github.com/COG-GTM/devin-sdk-examples/tree/main/harness-chat)

The [AI SDK harness](https://docs.devin.ai/sdk/harness/overview.md) example. Devin edits files, runs commands and calls your
server-side tools in an isolated microVM; the chat shows each step as it streams, asks you to
approve shell commands, and keeps its session so a later message — from any server instance —
picks up where it left off. Side panels show the sandbox status, a file browser, and a preview
of whatever Devin serves from the workspace.

![Harness chat: a chat column with Devin's reasoning, edits and approved bash commands; side panels showing the Vercel Sandbox status, a preview of the built app, and its files](https://mintcdn.com/cognitionai/BR9pVI7VuIdWd-ef/images/sdk/harness-chat.png?fit=max\&auto=format\&n=BR9pVI7VuIdWd-ef\&q=85\&s=1fff6ef0c8d893117b5dfc868a5e18f6)

The console: chat with Devin's reasoning and tool calls, the sandbox, a live preview of the app it built, and its files

## How it fits together

```mermaid theme={null}
flowchart LR
    redis[("Upstash Redis<br/>resume state")]
    subgraph server["Your server (Next.js route)"]
        direction TB
        agent["HarnessAgent<br/>harness: createDevin()"]
        tools["Your tools (AI SDK)"]
    end
    subgraph sandbox["Vercel Sandbox (one per chat)"]
        direction LR
        bridge["bridge"]
        cli["devin acp (CLI)<br/>cwd = /vercel/sandbox/devin-&lt;chat&gt;"]
    end
    redis <-.-> agent
    agent <-- "WebSocket: session events,<br/>tool calls relayed back" --> bridge
    tools -.- agent
    bridge --> cli
    cli --> devin["Devin"]
```

The whole agent is one declaration:

```ts lib/agent.ts (excerpt) theme={null}
import { HarnessAgent } from "@ai-sdk/harness/agent";
import { createVercelSandbox } from "@ai-sdk/sandbox-vercel";
import { createDevin } from "@cognition-ai/harness-devin";

export const devinAgent = new HarnessAgent({
  harness: createDevin({ port: BRIDGE_PORT }),
  sandbox: createVercelSandbox({ runtime: "node24", ports: [BRIDGE_PORT, ...PREVIEW_PORTS] }),
  instructions: "You are Devin working in an empty workspace inside a sandbox. …",
  permissionMode: "allow-edits", // edits go through; shell commands ask, as a card in the chat
});
```

| File | Role |
| - | - |
| `lib/agent.ts` | The agent above. Add AI SDK `tools` here and they run on your server, relayed into the sandbox. |
| `app/api/chat/route.ts` | The `useChat` route. Resumes the chat's session, streams the turn, parks the session (`detach()`) when the stream settles. |
| `lib/session-store.ts` | Resume state in Redis, keyed by chat id, which is also the harness `sessionId` and therefore the sandbox name. |
| `lib/workspace.ts` | Read-only view into a chat's sandbox (status, files, preview URL) for the side panels. |

## Run locally

Local development uses the real Vercel Sandbox; there is no emulation, so what you see locally is
what runs in production.

```sh theme={null}
git clone https://github.com/COG-GTM/devin-sdk-examples
cd devin-sdk-examples/harness-chat
bun install                    # @cognition-ai/sdk + harness-devin from npm (the pinned beta)
vercel link                    # pick or create the project (once)
vercel env pull                # .env.local: VERCEL_OIDC_TOKEN, KV_REST_API_*, REDIS_URL
echo 'DEVIN_API_KEY=cog_…' >> .env.local
bun run dev                    # http://localhost:3210
```

`vercel env pull` needs the Upstash integration on the project (Storage → Upstash in the Vercel
dashboard). The OIDC token expires; rerun `vercel env pull` when sandbox calls start failing with
an auth error.

## Deploy

The [README](https://github.com/COG-GTM/devin-sdk-examples/tree/main/harness-chat) has a
Deploy-with-Vercel button that clones the example, provisions the Upstash store and asks for
`DEVIN_API_KEY`; Vercel Sandbox authenticates automatically on Vercel. `@cognition-ai/harness-devin`
and `@cognition-ai/sdk` are pinned to the same published beta in its `package.json`.

> [!WARNING]
>
> This is a single-user demo: there is no login, and anyone who can reach the deployment can open
> any session in it. Put it behind [Vercel Deployment
> Protection](https://vercel.com/docs/deployment-protection) or add your own auth before sharing a
> URL.

> [card] [AI SDK harness](https://docs.devin.ai/sdk/harness/overview.md)
>
> What `@cognition-ai/harness-devin` provides and how the bridge works.

> [card] [Diagnostics](https://docs.devin.ai/sdk/harness/diagnostics.md)
>
> The `onLog` events this example turns into a status line.


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