---
title: "Durable Agents"
description: "Serve agents written with the AI SDK, Mastra, LangGraph, the OpenAI Agents SDK, or plain TypeScript as durable runs that pause for approval and survive restarts and redeploys."
canonical_url: "https://mcp.farmjs.dev/docs/0.1/integrations/agent"
markdown_url: "https://mcp.farmjs.dev/docs/0.1/integrations/agent.md"
last_updated: "2026-11-28"
x_farming_labs_generated_preamble: true
---

# Durable Agents
URL: /docs/0.1/integrations/agent
LLM index: /llms.txt
Description: Serve agents written with the AI SDK, Mastra, LangGraph, the OpenAI Agents SDK, or plain TypeScript as durable runs that pause for approval and survive restarts and redeploys.

# Durable Agents

`@farm.js/agent` serves the agents in `src/agents` as durable runs, built and run by [agent-unit](https://github.com/farming-labs/agent-unit). A run can pause for a person's approval, survive a restart or a redeploy, and resume without repeating a model call or a side effect. Clients reconnect to a run's event stream from the last event they saw. The same agents are also an MCP server and an A2A agent.

Use it when a run must outlast its request. For a chat that streams one answer per request, a plain API route is enough; see [agent frameworks](/docs/0.1/integrations/agent-frameworks).

## Install

```bash
pnpm add @farm.js/agent agent-unit
```

## Register the integration

**farm.config.ts**

```ts
import { agent } from "@farm.js/agent";
import { defineConfig } from "@farm.js/core";

export default defineConfig({
  integrations: {
    agent: agent({
      storage: { driver: "redis", url: process.env.REDIS_URL },
    }),
  },
});
```

`farm dev` serves the agents from source and reloads them when they change. `farm build` builds them into the server for the app's deploy target: agent-unit adds the agents' framework adapters, run storage, and a scheduled sweep that wakes sleeping runs and recovers stalled ones. The settings evaluate where the server runs, so a storage URL or a token read from `process.env` is never written into the build.

## Write an agent

Each file in `src/agents` is an agent named after the file. Write it with the framework you already use and export it as the default. Plain TypeScript agents use `defineAgent`:

**src/agents/refund.ts**

```ts
import { defineAgent } from "agent-unit";

export default defineAgent(async (input: { orderId: string }, run) => {
  const order = await run.step("look up order", () => findOrder(input.orderId));
  const { approved } = await run.interrupt<{ approved: boolean }>("approve-refund", order);
  return approved ? await refund(order) : "kept";
});
```

`run.step` runs once and is replayed from the run's journal afterwards. `run.interrupt` pauses the run until an answer arrives, across restarts and deploys. Give it a `timeout`, such as `{ timeout: "24h" }`, and it throws `InterruptTimeoutError` once that passes without an answer; catch it to decide what no answer means. See agent-unit's [interrupt timeouts](https://github.com/farming-labs/agent-unit#stop-waiting-after-a-while).

Agents from the [AI SDK](https://ai-sdk.dev) (`new ToolLoopAgent(…)`), [Mastra](https://mastra.ai) (`new Agent(…)`), [LangGraph](https://langchain-ai.github.io/langgraphjs/) (`graph.compile()`), and the [OpenAI Agents SDK](https://openai.github.io/openai-agents-js/) (`new Agent(…)`) work unchanged. Their own pauses become interrupts: tool approvals, Mastra's `suspend()`, and LangGraph's `interrupt()`. The framework is detected from `package.json`; name it to serve only that framework:

```ts
agent({ framework: "mastra" });
```

With `framework` set, the build fails with the install command when that framework is missing.

## Wait for an external event

With agent-unit 0.6.0, a run can wait for a webhook or another system:

```ts
// src/agents/checkout.ts
import { defineAgent } from "agent-unit";

export default defineAgent(async (input: { orderId: string }, run) => {
  const payment = await run.waitFor<{ orderId: string; amount: number }>("payment.succeeded", {
    match: { orderId: input.orderId },
    timeout: "1h",
  });
  return { orderId: input.orderId, paid: payment.amount };
});
```

After verifying the webhook provider's signature, send its event to the app's guarded
`POST /api/agent/events` endpoint:

```json
{
  "name": "payment.succeeded",
  "data": { "orderId": "o_1", "amount": 42 },
  "id": "provider-event-1"
}
```

Matching runs resume with the event data. Reuse the provider event ID on retries to avoid
redelivery. Events are retained for 24 hours by default, so one sent after the run starts but
before it waits can still be consumed. A parked run has status `waiting` and emits `RUN_WAITING`.
A timeout throws `EventTimeoutError` from `agent-unit/runtime`.

Protect event sends with the same middleware and guards as the rest of the agent API. The
`authorize` callback receives `action: "send"` and the event name. Farm.js Infra's existing store
and clock support these waits; event sends go to the app, and event waits do not create human
approval requests in Infra.

## Call the agents

The API lives under `basePath`, `/api/agent` by default:

| Request                                  | Does                                                                   |
| ---------------------------------------- | ---------------------------------------------------------------------- |
| `POST /api/agent/agents/:name/runs`      | Starts a run with `{ input }`. `Accept: text/event-stream` streams it. |
| `GET /api/agent/runs/:id`                | Reads a run: its status, the interrupt it waits on, and its output.    |
| `GET /api/agent/runs/:id/events`         | Streams its events, from `?after=` or `Last-Event-ID`.                 |
| `POST /api/agent/runs/:id/resume`        | Answers the interrupt with `{ answer }`.                               |
| `GET /api/agent/runs?status=interrupted` | Lists runs, for an approvals inbox.                                    |
| `GET /api/agent/manifest.json`           | Lists the agents.                                                      |
| `POST /api/agent/events`                 | Sends an event to matching waiting runs.                               |
| `GET /api/agent/runs?status=waiting`     | Lists runs waiting for an event.                                       |
| `POST /api/agent/mcp`                    | The agents as MCP tools.                                               |

Events follow [AG-UI](https://docs.ag-ui.com). The typed client reconnects dropped streams:

```ts
import { createAgentClient } from "agent-unit/client";

const agents = createAgentClient({ baseUrl: "/api/agent" });

for await (const event of agents.run("refund", { orderId: "o_1" })) {
  if (event.type === "RUN_INTERRUPTED") console.log("needs approval:", event.interrupt.payload);
}
for await (const event of agents.resume(runId, { approved: true })) console.log(event.type);
```

The agent API is made of ordinary API routes, so `basePath` sits under `/api`. Requests pass the app's [middleware](/docs/0.1/middleware), plugin `runtime.before` hooks, and integration guards such as `protectedRoutes` before they reach the agents, in `farm dev` and in every deploy target. The integration owns every path under `basePath`. `farm dev` refuses to start when an API route in `src/app` sits under it; move the route or set another `basePath`.

## Authorize requests

Every request reaches `authorize` before the agents. Return `false` to answer `401`, or decide per action, agent, and run:

```ts
agent({
  authorize: (request) =>
    request.headers.get("authorization") === `Bearer ${process.env.AGENT_TOKEN}`,
});
```

Set `authorize` before anyone else can reach the app; a middleware or plugin guard on `basePath` works too. Without either, anyone who can reach the server can list every run with its input and output, read a run and its events, answer its interrupt, cancel or delete it, and start new runs.

A guard on `basePath` also sees agent-unit's internal `__agent-unit/continue` and `__agent-unit/sweep` requests, which carry `AGENT_UNIT_SECRET` as a bearer token, and [Farm.js Infra's](#approve-runs-in-farmjs-infra) `__agent-unit/wake`, `answer`, and `cancel` calls, which carry a `farmjs-signature` header. Let them through, rather than redirecting them to a sign-in page, so a yielded run continues in a fresh invocation and Netlify's scheduled sweep reaches the server; agent-unit checks the secret and the signature itself. A redirect that ends on a page answering 200 looks like a delivered hand-off, so the run waits for the next sweep instead of continuing.

## Run an agent on a schedule

A task with `agent` starts a run of that agent each time its schedule comes round, on the deploy target's scheduler:

```ts
agent({
  tasks: {
    "morning-digest": { schedule: "0 8 * * *", agent: "digest", input: { team: "support" } },
  },
});
```

Each scheduled time starts one run. Its id comes from the task's name and that time, so a retried cron call, or a cluster where every process runs the schedule, gets the run the first call started. Times are read in the server's time zone: UTC on Vercel and Workers. The runs are ordinary runs: `GET /api/agent/runs?agent=digest` lists them, and they can sleep or pause for approval like any other. `input` evaluates where the server runs, like the other settings.

A task with `handler` instead calls the default export of that module. See agent-unit's [scheduled tasks](https://github.com/farming-labs/agent-unit#background-work-and-scheduled-tasks) for the hosts that run a schedule.

## Approve runs in Farm.js Infra

With `FARMJS_API_KEY` set where the server runs, every interrupt a run waits on shows up under Agents > Approvals in the [Farm.js Infra](https://app.farmjs.dev) dashboard. An owner or admin of the project's organization approves or rejects it, with an optional note, or cancels the run. Infra calls `POST /api/agent/__agent-unit/answer/:id` or `POST /api/agent/__agent-unit/cancel/:id` on the app, signed with the key, and a cancel ends the run only while it still waits on that pause. The same key lets Infra wake sleeping runs. See agent-unit's [Farm.js Infra section](https://github.com/farming-labs/agent-unit#approve-interrupts-in-farmjs-infra) for the variables it reads.

## Storage and deploy targets

Runs live in [Farm.js storage](https://github.com/farming-labs/agent-unit#farmjs-storage) or any [unstorage](https://unstorage.unjs.io) driver. Without `storage`, the build decides:

- With `FARMJS_API_KEY` in the build's environment, runs are kept in Farm.js storage on every target, with no database to set up. Set the key where the server runs too. It needs the `agents:runtime` permission.
- Without the key, a Node server keeps them on disk in `.data/agent-unit` and the build warns, since a container without a volume loses them on every redeploy.
- Without the key, a build for Vercel, Netlify, AWS Lambda, or Cloudflare Workers stops and lists what to set: the key, a `storage` driver, or `storage: "memory"` for runs that may be lost between requests.

`farm dev` keeps runs on disk and makes no calls to Farm.js. `storage: "farmjs"` chooses Farm.js storage whatever the build's environment has, and the key is then read only where the server runs. Use `presets` to choose storage per target:

```ts
agent({
  storage: { driver: "fs-lite", base: ".data/agents" },
  presets: {
    vercel: { storage: { driver: "upstash" } },
    "cloudflare-module": { storage: { driver: "cloudflare-kv-binding", binding: "AGENTS" } },
  },
});
```

On Redis, runs and leases change atomically, so two instances never take the same approval twice. Each target's scheduler runs the sweep; Netlify, AWS Lambda, and Vercel's Hobby plan need a step of their own, listed in agent-unit's [host table](https://github.com/farming-labs/agent-unit#deploy-anywhere).

On Vercel, set `CRON_SECRET` in the project's environment variables. Vercel Cron runs the sweep and `tasks` through `/_vercel/cron` and sends the secret as a bearer token; the route answers `401` to any call without it, including every call while `CRON_SECRET` is unset, and the build warns when it does not see the variable.

## Options

| Option      | Default          | Description                                                                          |
| ----------- | ---------------- | ------------------------------------------------------------------------------------ |
| `basePath`  | `"/api/agent"`   | Where the API is mounted, under `/api`.                                              |
| `agentsDir` | `"src/agents"`   | The folder of agent files, relative to the app root (`agents` in `srcDir`).          |
| `framework` | detected         | `"ai-sdk"`, `"mastra"`, `"langgraph"`, or `"openai-agents"`.                         |
| `storage`   | per target       | Run storage: `"farmjs"`, `"memory"`, or an unstorage driver config.                  |
| `presets`   | none             | Settings per deploy target, applied over the top-level ones.                         |
| `authorize` | none             | Decides every request.                                                               |
| `name`      | the package name | The name in the manifest, MCP, and A2A.                                              |
| `tasks`     | none             | Scheduled tasks: an `agent` to run or a `handler` module, on the target's scheduler. |
| `budget`    | per target       | How long a run works before it yields and continues: 240s on Vercel, 25s on Workers. |
| `retention` | kept             | How long finished runs are kept.                                                     |
| `sweep`     | every minute     | The sweep's cron schedule, or `false`.                                               |
| `agents`    | `src/agents`     | Agents as an object, instead of the folder.                                          |
| `adapters`  | none             | Adapters for other frameworks.                                                       |

The Nitro and Workers settings belong in `farm.config.ts`. A framework's own server, such as Mastra's, and agent-unit's Durable Objects runtime are built by `agent-unit build` instead of this integration.

The [`examples/agent`](https://github.com/farming-labs/farm.js/tree/main/examples/agent) app runs an approval agent end to end.

## Sitemap

See the full [sitemap](/sitemap.md) for all pages.
Docs-scoped sitemap: [/docs/sitemap.md](/docs/sitemap.md).
Well-known sitemap: [/.well-known/sitemap.md](/.well-known/sitemap.md).
