Why Durable Agents?
Aside from the usual challenges of getting your long-running tasks to be production-ready, building mature AI agents typically requires solving several additional challenges:- Statefulness: Persisting chat sessions and turning LLM and tool calls into async jobs with workers and queues.
- Observability: Using services to collect traces and metrics, and managing them separately from your messages and user history.
- Resumability: Resuming streams requires not just storing your messages, but also storing streams, and piping them across services.
- Human-in-the-loop: Your client, API, and async job orchestration need to work together to create, track, route to, and display human approval requests, or similar webhook operations.
Getting Started
To make an Agent durable, we first need an Agent. This guide assumes you have a basic chat application using the AI SDK. If you’re starting from scratch, check out the AI SDK documentation first.Install Dependencies
Add the Workflow DevKit packages to your project:next.config.ts
Create a Workflow Function
Move your agent logic into a workflow function:workflows/chat/workflow.ts
- Add
"use workflow"directive to mark the function as a workflow - Replace
AgentwithDurableAgentfrom@workflow/ai/agent - Use
getWritable()to get a persistent stream for agent output - Mark tool implementations with
"use step"for automatic retries
Update the API Route
Replace the agent call withstart() to run the workflow:app/api/chat/route.ts
- Call
start()to run the workflow function - Return
run.readableas the stream instead of creating a new one
Convert Tools to Steps
Mark all tool implementations with"use step" to make them durable:workflows/chat/steps/tools.ts
"use step":- The tool execution runs in a separate step with full Node.js access
- Failed tool calls are automatically retried (up to 3 times by default)
- Each tool execution appears as a discrete step in observability tools
Observability
In your app directory, open the observability dashboard to see your workflow in action:Next Steps
Now that you have a basic durable agent, explore these additional features:Core Concepts
DurableAgent vs Agent
DurableAgent is a drop-in replacement for AI SDK’s Agent class that makes all LLM calls and tool executions durable:
- Automatic persistence: All messages, tool calls, and results are automatically saved
- Step-based execution: Each LLM call runs as a workflow step with built-in retries
- Stream persistence: Output streams are durable and can be resumed after interruptions
- Observability: Full tracing of all agent actions through the workflow dashboard
Workflows and Steps
- Workflows (
"use workflow") orchestrate the overall agent behavior and maintain state - Steps (
"use step") execute individual operations like API calls with automatic retries
Stream Management
getWritable() provides a persistent stream that:
- Survives function timeouts and crashes
- Can be read by multiple clients simultaneously
- Supports resumption from any point
- Automatically handles backpressure
Related Documentation
- Defining Tools - Patterns for defining tools for your agent
DurableAgentAPI Reference - Full API documentation- Workflows and Steps - Core concepts
- Streaming - In-depth streaming guide
- Errors and Retries - Error handling patterns