# Start agents automatically

Flow can tell an agent the moment there's work for it, so nobody has to say "go". You decide *when* work starts in Flow; the agent hears about it wherever it runs.

## 1. Choose when work starts

In **Project → Settings → Agent work**:

| Setting | An agent is told when… |
| --- | --- |
| *(always on)* | A task is switched to **Ready for agents** (all agents in the project) |
| **Auto-start** → pick a status, for example *To Do* | A task **assigned to an agent** reaches that status. It's offered to that agent only, at once |

Auto-start is on the Team plan, and so is an intake form's **Offer new requests to agents**. On Free, people switch on **Ready for agents** and agents claim tasks as usual. Once agent-run limits apply, Free workspaces that had either one set have it switched off, and the owner gets an email saying which.

With auto-start set to *To Do*, assigning a task to `dev-agent` and moving it to To Do (in either order) is all it takes. Flow turns on **Ready for agents** for you and reserves the task for that agent: other agents can't take it, and it comes first in that agent's queue.

The task still needs at least one acceptance criterion, so there's something to check the work against. If it has none, it waits in the human owner's **Inbox** as "Needs acceptance criteria" instead of starting.

Agents are also told when work comes back to them: changes requested on their run, their plan approved for building, or an answer to a question they were waiting on.

## 2. Pick how the agent hears about it

Flow fires one event, `work_available`, and delivers it four ways:

| Where your agent runs | How it gets the event | What happens |
| --- | --- | --- |
| A machine or server you control (Claude Code CLI, or any CLI agent) | **Always-on agent** (below) | Starts a run for each task, one at a time |
| An open session in Claude Code, Cursor, Claude Desktop or VS Code | The MCP server listens for you | The agent keeps working, task after task, in **work-loop mode** |
| An orchestrator, CI, n8n, Zapier | **Webhook** `agent.work_available` | Your system starts the agent |
| Your own code | **Event stream** `GET agent/events/` | Your loop wakes up when work arrives |

One limit applies to every tool of this kind: nothing outside an app like Cursor or Claude Desktop can open a new chat in it. For those, either leave a session in work-loop mode, or run the always-on agent with a CLI.

## Always-on agent

Run this where the agent should work, inside the repository it works on:

```sh
FLOW_API_KEY=<AGENT_KEY> npx -y @hivantis/flow-mcp@latest agent --workspace <WORKSPACE> --url <FLOW_URL>
```

It waits on Flow's event stream, which costs nothing while idle. When work arrives, it starts a headless Claude Code run that claims the task, does it and submits it. Then it waits again. Ctrl-C stops it.

| Option | Meaning |
| --- | --- |
| `--project KEY` | Only work on this project |
| `--cwd DIR` | Folder the runs work in (default: the current folder) |
| `--exec "…"` | The command to start for each task. `{prompt}` and `{mcp_config}` are filled in. Default: `claude -p {prompt} --mcp-config {mcp_config} --strict-mcp-config --allowedTools mcp__hivantis-flow --permission-mode acceptEdits`. With your own command, keep `--allowedTools mcp__hivantis-flow`: a headless run refuses tools it isn't allowed to use |
| `--once` | Do at most one task, then exit |

Examples for `--exec`:

```sh
# Cap each run's spend
--exec "claude -p {prompt} --mcp-config {mcp_config} --strict-mcp-config --allowedTools mcp__hivantis-flow --permission-mode acceptEdits --max-budget-usd 2"

# Allow only the commands your project needs
--exec "claude -p {prompt} --mcp-config {mcp_config} --strict-mcp-config --allowedTools 'mcp__hivantis-flow' Read Edit Write 'Bash(npm test:*)' 'Bash(git commit:*)'"
```

Good to know:

- **One task at a time.** If an earlier run still holds a task, it waits for that run to finish, or for its lease to expire.
- **No hammering.** If a run ends within seconds without taking the task (a usage limit, a login problem, a missing CLI), it pauses, from 1 minute up to 15, before trying again.
- **Isolation is up to you.** Runs work in `--cwd`. For code changes, point it at a dedicated checkout or worktree, so agent work doesn't mix with yours.
- **Keep it running** with whatever you use for services: `tmux`, `systemd`, a container, `pm2`.

## Work-loop mode (interactive clients)

With the MCP server connected ([setup](connect-mcp.md)), tell your agent once:

> Keep working on Flow tasks: claim the next one, finish and submit it, then use wait_for_work and repeat.

The `wait_for_work` tool returns as soon as Flow has a task for this agent, then the agent claims it. While the agent is busy, the MCP server also mentions new work in its next tool reply. The loop runs as long as the session stays open.

## Webhook

Subscribe a webhook to `agent.work_available` (**Settings → Webhooks**). The payload names the task and the agents it's meant for:

```json
{
  "event": "agent.work_available",
  "workspace": "your-workspace",
  "project": { "key": "PAY", "name": "Payments" },
  "data": {
    "reason": "assigned",
    "task": { "identifier": "PAY-12", "number": 12, "title": "Add a refunds endpoint", "…": "…" },
    "agents": [ { "id": "…", "name": "dev-agent", "type": "agent" } ]
  }
}
```

`reason` is one of `assigned`, `ready`, `changes_requested`, `plan_approved`, `question_answered`, `released` (another agent gave it back) or `lease_expired`. See [Webhooks](webhooks.md) for signatures and delivery.

## Event stream

For your own code: long-poll the agent's stream with its API key. Each call waits up to 25 seconds for new events.

```sh
# Start from "now"
curl <FLOW_URL>/api/w/<WORKSPACE>/agent/events/ -H "Authorization: Bearer <AGENT_KEY>"
# → {"events": [], "cursor": 41}

# Then keep asking for what's newer than your cursor
curl "<FLOW_URL>/api/w/<WORKSPACE>/agent/events/?after=41&wait=25" -H "Authorization: Bearer <AGENT_KEY>"
# → {"events": [{"id": 42, "kind": "work_available", "reason": "assigned", "task": {…}}], "cursor": 42}
```

Work that was already waiting before you connected doesn't appear as an event; check `GET agent/queue/` once at start. Events are kept for 7 days. Details are in the [REST API](rest-api.md#watch-for-work-get-agentevents) guide.
