# Delegate to the Revise agent

> Run multi-turn document tasks through editor.agent or revise_run_agent.

`editor.agent.run()` and the `revise_run_agent` tool delegate an entire multi-turn planning, reading, and editing task to Revise's agent loop.

## Configure a transport

Provide one of `turnStream` ([custom transport](#custom-transport)), `baseUrl` (your own agent backend), or `token` (Revise's hosted agent):

```tsx
export function Workspace({ token, docs }: { token: string; docs: ReviseDocumentInput[] }) {
  return (
    <ReviseEditor
      agent={{
        baseUrl: "https://agent.example.com",
        token,
        model: "claude-opus-5-5",
        provider: "anthropic",
      }}
      initialDocuments={docs}
    />
  );
}
```

With none configured, `agent.run()` rejects with `AgentNotConfiguredError` (`code: "agent_not_configured"`), `revise_run_agent` returns the same code, `editor.getToolDefinitions()` omits `revise_run_agent`, and no requests are sent. Per-document `agent` options override editor-wide defaults.

<Note>
  Delegated runs stream document content to `baseUrl` (default
  `https://agent.revise.io`; email [sdk@revise.io](mailto:sdk@revise.io) for
  hosted access).
</Note>

## Run a task

```ts
const result = await editor.agent.run(
  "Make the liability cap mutual and raise it to two years of fees.",
);

result.actionCount; // edit count
result.messages;    // conversation messages
result.state;       // "complete" | "error"
```

Or invoke via `executeDynamic` from a supervising model:

```ts
await editor.tools.executeDynamic("revise_run_agent", { task });
```

## Stream, steer, and cancel

```ts
useEffect(
  () =>
    editor.agent.subscribe((event) => {
      setStatus(event.status);
      setActiveTool(event.activeTool);
      setMessages(event.messages);
    }),
  [editor],
);

editor.agent.steer("Keep the defined terms as they are."); // injects guidance into active run
editor.agent.cancel();
```

Events are also emitted via the `onAgentEvent(documentId, event)` component prop.

## Custom transport

Supply `turnStream` to route model turns through your own gateway while keeping Revise's tool runtime and review behavior:

```tsx
<ReviseEditor
  agent={{
    turnStream: async function* (request) {
      yield* myGateway.stream(request);
    },
  }}
/>
```

Updating `turnStream` takes effect on the next run without reloading the document.

## Run accounting

On completion, the editor POSTs run metrics to `${baseUrl}/api/v1/chat/metrics/complete`. `turnStream` skips this automatically; pass `disableMetrics: true` to disable it for a custom `baseUrl`. This is separate from [`telemetry`](https://revise.io/developer/docs/editor-sdk/api/revise-editor).

## Visual page inspection

Delegated runs can call an internal `render_document_pages` tool to inspect rendered page contact sheets and layout diagnostics from the mounted canvas. It is not in `editor.tools.getDefinitions()` (which is shared with headless [`revise/server`](https://revise.io/developer/docs/editor-sdk/api/backend)).

## Tracked changes and direct mode

Delegated runs create tracked suggestions by default on non-empty documents (empty documents auto-enable direct mode unless `directMode` is set) and reject in `"viewing"` mode. Pass `directMode` to override for every tool call in the run:

```ts
await editor.agent.run(task, { directMode: true });
```

## Edits staged in a worker

`agent.stagingWorker` validates and plans each agent edit in a Web Worker, and
the page only commits the result, so typing and scrolling stay smooth while a
long run (a whole-document proofread, for example) makes many edits. Pass a
function that starts the worker shipped with the package:

```tsx
<ReviseEditor
  agent={{
    baseUrl,
    stagingWorker: () =>
      new Worker(new URL("revise/staging-worker", import.meta.url), {
        type: "module",
      }),
  }}
/>
```

The worker is an ES module that shares the editor's chunks, so your bundler
must build workers as modules (Vite: `worker: { format: "es" }`). It is read
when a document opens, and each open document gets its own worker. An edit the
worker cannot handle is applied on the main thread, as without the option, so
the option changes speed, never results.
