Harness streams are compatible with AI SDK UI message streams. You can use
useChat() on the client and stream HarnessAgent output from a server route.
The important difference from model-based chat routes is session management.
A harness owns its conversation state, so the route should resume or create a
HarnessAgentSession for the chat id instead of replaying the whole UI message
history into a model.
import { HarnessAgent } from '@ai-sdk/harness/agent';import { claudeCode } from '@ai-sdk/harness-claude-code';export const agent = new HarnessAgent({ harness: claudeCode, instructions: 'You are a helpful coding assistant.',});
Persist the opaque resume state returned by session.detach(). If the
turn paused for approval or was otherwise interrupted, that resume state carries
the continuation state internally. The chat ID can also be the harness
sessionId. Here we derive a stable sandboxId from that known chat id. If
you do not derive one, persist the returned sandboxSession.id separately.
Use durable storage instead of an in-memory map in production. The creator
always creates a new sandbox and fails on a conflicting sandboxId; only the
resume function reattaches an existing one.
Creating the UI message stream before acquiring the session ensures sandbox,
bootstrap, and harness startup failures are sent as UI error parts instead of
becoming generic HTTP errors. getHarnessErrorMessage preserves reviewed,
client-safe harness messages and masks unknown server errors.
Do not use createAgentUIStreamResponse directly with HarnessAgent unless you
wrap the agent to inject the required session. HarnessAgent.stream() requires
session on every call.
Use session.detach() when you want to park the harness runtime and keep the
sandbox warm for the next request. Bridge-backed adapters can usually reattach
or replay efficiently. If the turn is unfinished, detach() includes the turn
continuation state in the returned resume state.
session.stop() saves resume state and stops the harness runtime, but does not
stop a supplied sandbox. The caller owns that network session and should call
its destroy() method when the chat ends, not while a later request needs to
reattach. Preserve the chosen sandboxId across processes.
Harness output contains the same UI message part shapes used by AI SDK model
streams:
text and reasoning parts for generated content.
typed tool parts such as tool-bash, tool-read, or a host tool like
tool-weather.
dynamic-tool parts for dynamic events such as fileChange and
compaction.
Render typed harness built-ins the same way you render normal AI SDK tool
parts. Check part.state for input-streaming, input-available, and
output-available.