How do I run long-running background AI agents?
Summary
- Run them behind an agent server that accepts a background request, returns at once with a status URL and keeps working on the server, so no client has to hold a connection open.
- Run state has to live outside the worker process: when a worker crashes or is replaced, a durable agent server detects the stalled run and starts a replacement attempt.
- On Databricks,
DurableAgentServeron Agent Runtime serves synchronous, streaming and background invocations from one handler, with run state in a Runtime Store on a Databricks-managed Lakebase project. - Recovery can run agent code more than once, so make external side effects idempotent and cap attempts in the recovery handler.
- The Agent Bricks CLI that scaffolds and deploys these agents is in Beta, and so are managed agent sessions.
How do I run long-running background AI agents?
Run them behind an agent server built for runs that outlive a request. The client starts an invocation with a background flag, gets an immediate acknowledgment with a status URL, and polls or streams events later. The server stores each run's request, status, events and result outside the worker, notices when a worker dies, and restarts the run. On Databricks, DurableAgentServer on Agent Runtime does this, and the Agent Bricks CLI (Beta) deploys it.
What makes an agent long-running?
A long-running agent's run outlasts what a client can reasonably keep a connection open for: deep research, multi-step tool chains, batch document work, or a run that waits for a person to approve a tool call. On a plain request-response server, three things break. A dropped connection loses the output. A restart or redeploy kills the run mid-flight. And a client that retries after a timeout can start the same work twice.
Key capabilities to evaluate
| Capability | Why it matters |
|---|---|
| Background invocation | The client gets an immediate response and a status URL, so the run doesn't depend on an open connection |
| Status polling and stored output | Any client can check progress and fetch the result after the run finishes |
| Stream reconnection | A client that drops mid-stream resumes from the last event it received |
| Persistent run state | Status, events and results survive worker restarts |
| Crash recovery | Stalled runs are detected and restarted on a healthy worker |
| Idempotent invocations | A retried request returns the existing run and doesn't start a duplicate |
| Ordered sessions | Turns in one conversation run one at a time, in order |
| Human-in-the-loop pause | A run can stop for approval and continue later in the same session |
| Unattended identity | The agent needs its own scoped principal when no user is waiting |
How Databricks approaches long-running agents
DurableAgentServer is the agent server in AgentKit, the Agent Bricks library. You register one async handler with @app.invoke, and it serves every request mode. The client decides whether to wait, stream or run in the background.
Start in the background. A POST /api/invocations with "background": true returns a 202 response with a status_url. Poll GET /api/invocations/<id> until the status is completed. Add "stream": true to also get an events_url. If a stream drops, GET /api/invocations/<id>/events?after=<event-id> replays everything after the last event you saw. See Query agents deployed on Databricks.
Keep run state durable. agentbricks deploy provisions a dedicated database for the deployment's Runtime Store in a Databricks-managed Lakebase project. Results and events survive worker restarts, and any instance of the agent can serve status and reconnection requests. Locally, agentbricks dev uses an in-process store, so state is lost when the process stops.
Recover interrupted runs. Each running attempt sends heartbeats. If they stop because a worker crashed, restarted or was replaced during a redeploy, the server starts a replacement attempt and calls your @app.recover handler with the original input. The LangGraph template resumes from its last checkpoint in the session store, and the OpenAI Agents SDK template replays the request in the same session. Recovery covers interrupted workers, not exceptions raised by your code.
Make retries safe. Clients send a UUID id with each invocation, and resending the same request returns the existing invocation. Invocations that share a session_id run one at a time, in order.
Pause for people. When an agent built from the CLI templates pauses for input, such as approval of a tool call, the invocation's status is interrupted. Send resume in a new invocation with the same session_id to continue.
Pick the identity. A deployed agent runs as its app's service principal, which suits unattended work. Tools can act as the requesting user instead, but the server doesn't store that credential, so it can't recover an interrupted request-user invocation.
Agents from the earlier app templates can use the legacy LongRunningAgentServer, which adds background runs and stream resumption on a Lakebase database you configure.
Design rules for production
- Make every external side effect idempotent. Recovery can run your agent code more than once for the same invocation.
- The server doesn't cap recovery attempts. Check
context.attemptin the recovery handler and raise an error when you want to stop. - Publish progress with
context.emitso clients watching the stream see status between long steps. - Keep conversation history in a session store. It's separate from the server's Runtime Store and gives recovery a checkpoint to resume from.
- Keep MLflow tracing on (the CLI enables it by default) to see where a run stalled.
FAQs
What's the difference between streaming and background mode?
Streaming keeps a connection open and delivers events as Server-Sent Events. Background mode returns a 202 immediately and lets the client poll a status URL. You can set both to get an events URL for a background run.
Does a long-running agent survive a redeploy?
On Agent Runtime with DurableAgentServer, yes, if you register a recovery handler. A worker replaced during a redeploy stops sending heartbeats, and the server starts a replacement attempt.
Can I test a background agent locally?
Yes. agentbricks dev serves the same invocation API with an in-process Runtime Store, but run state is lost when the process stops and interrupted work isn't restarted.
The information provided herein is for general informational purposes only and may not reflect the most current product capabilities or configurations.