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

# run(), resume() and fork()

> Loop turns until onTurn stops, then carry the session on, or branch it.

`run({ ...startOptions, prompt, onTurn })` starts a session, sends `prompt`, then after each turn calls `onTurn(turn, session)`: a message it returns is the next turn, and nothing (or an empty message) stops. Without `onTurn` it sends one turn. It closes the session and resolves with `{ sessionId, cwd, turns, resume, fork }`.

```ts theme={null}
const result = await run({
  agent: claudeCode("claude-opus-5-5"),
  branch: { name: "fix/flaky-test" },
  prompt: "Find why test/cache.test.ts is flaky and fix it.",
  onTurn: (turn) => (turn.commits.length ? undefined : "Commit the fix, then stop."),
});
```

`result.resume(prompt, options?)` continues its session as a new run, and `result.fork(prompt, options?)` branches it into a new session that continues from there, leaving this one's record as it is. Each takes this run's options (its `output` and `onTurn` aside) under any `options` you give. A fork isolates only the conversation: without a `branch` of its own it works where this run did, so give concurrent forks one each. Each needs a message. A run whose session never got an id (no turn reached the agent) has nothing to continue, and both reject with `RunNotResumableError`.

```ts theme={null}
const [a, b] = await Promise.all([
  result.fork("Try a cache instead.", { branch: { name: "try/cache" } }),
  result.fork("Try a retry instead.", { branch: { name: "try/retry" } }),
]);
```

## Errors

| Error | When |
| - | - |
| `RunNotResumableError` | `resume()` or `fork()` on a run whose session never got an id |
| `ResumeNotSupportedError` | the agent's harness keeps no session records ([Agents and providers](https://psychiatrist.sudo.la/sdk/agents.md)) |
| `TypeError` | an empty message, or the old `effort` or `harness` option given to `run()`, `resume()` or `fork()` |


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