Durable Execution for Long-Horizon AI Agents
Ensuring reliable automation for complex, multi-day tasks through persistent state management and recovery.
1The Challenge of Duration
Long-horizon agents automate complex tasks spanning days or weeks, involving multiple human and system interactions. These processes are prone to crashes, network failures, and unpredictable real-world behaviors. Traditional software struggles with these non-linear, extended workflows, making durable execution a critical pattern for ensuring agents successfully reach their defined goals.
2Redefining Economic Viability
For decades, business operations were constrained by the scarcity of human attention, limiting complex tasks to high-value scenarios. Long-horizon agents change this by making previously uneconomical work viable. By automating administrative dependencies like onboarding or procurement, enterprises can now provide personalized assistance at scale, effectively removing bottlenecks that previously hindered operational efficiency.
3The Agentic Loop Mechanism
Most agents operate on a four-part loop: observing input, deciding an action, executing it, and responding. In long-horizon tasks, this loop repeats over extended periods. The 'harness' manages tool execution and state updates, while the model decides the next step. However, this loop is vulnerable if the underlying process crashes.
4Durable Execution Defined
Durable execution is a programming model where the runtime records the results of every step interacting with the outside world. If a failure occurs, the program restarts, skips completed steps, and rebuilds its state from the persistent log. This ensures the agent resumes exactly where it left off without duplicating actions.
5Core Components of Durability
The model relies on three key concepts: steps, a persistent log, and replay. A step represents any interaction with external systems, such as LLM calls or API requests. Before proceeding, the runtime logs the input and output. During replay, the system uses these logs to avoid re-executing previously completed external actions.
6Implementing with Temporal
Using Temporal, developers annotate the agentic loop with @workflow.run to enable persistence. External interactions are wrapped in workflow.execute_activity, which logs inputs and outputs. This allows the workflow to survive process crashes, as the runtime automatically manages the state recovery process, letting developers focus on domain-specific logic rather than infrastructure.
7Handling Asynchronous Waits
Long-horizon agents often wait for human approval or supplier responses. Durable runtimes use primitives like workflow.wait_condition to suspend execution without keeping the process active. This wait is recorded, ensuring that even if the system goes offline, the agent remains aware of its pending state and resumes upon receiving the required signal.
8Stock Replenishment Example
The Stock Replenisher agent demonstrates these principles by managing parts from shortage to delivery. It handles quotes, manager approvals, and order placement. If the process crashes while waiting for a manager, the agent recovers upon restart, skipping the already-placed requests and continuing the workflow once the approval event is finally received.
9Distributed System Parallels
Long-horizon agents function like distributed systems, facing similar failure modes such as network timeouts and process crashes. However, they are unique because the AI model at the core can decide how to recover from these failures. The harness provides the model with the right context to determine the next logical step.
10Process vs Entity Workflows
Effective design distinguishes between process and entity workflows. A process workflow carries out a single, finite piece of work, such as one replenishment order. An entity workflow represents a long-term business object, like a SKU, maintaining state across multiple replenishment cycles. This separation ensures modularity and clarity in complex agentic systems.
11Business Rules in Code
In this architecture, business rules reside in code rather than prompts. Tools define an 'allowed' method to check the current state before execution. For example, the 'place_order' tool is only available after price approval. This structure ensures that the agent adheres to strict operational constraints throughout its long-running lifecycle.
12Future of Reliable Automation
Durable execution transforms AI from simple chatbots into robust operational agents. By handling the complexities of state recovery and long-duration waits, this pattern enables reliable automation of unpredictable business processes. Enterprises adopting this approach can significantly reduce manual oversight, allowing agents to shepherd complex deals and administrative tasks to completion.
Original article: Martin Fowler — Durable Execution for Long-Horizon AI Agents