Building an AI agent is relatively easy when every step succeeds. The real challenge starts when an agent performs a long-running task and something fails in the middle.
An agent might call an API, query a database, execute a tool, wait for an external system, and then continue with another step. If the application crashes after one of those operations, simply starting the agent again can create duplicate work or lose its progress.
This is where durable execution and workflow state become important.
The Microsoft Agent Framework provides concepts for building agent-based workflows that can preserve progress, recover from failures, and continue long-running operations instead of treating every execution as a completely new request.
This article explains the problem, how failure recovery works conceptually, and how you can design a C# agent workflow with reliability in mind.
Why Long-Running Agents Fail
A simple chatbot usually follows this pattern:
User
|
v
AI Model
|
v
ResponseA long-running agent can be much more complicated:
User
|
v
Agent
|
+--> Search information
|
+--> Call external API
|
+--> Analyze result
|
+--> Update database
|
+--> Ask another agent
|
+--> Generate final responseEvery external operation introduces another possible failure.
For example:
An API can return a timeout.
A database connection can fail.
A service can temporarily become unavailable.
The application process can restart.
A tool can return an unexpected response.
The machine running the workflow can become unavailable.
The biggest problem is not necessarily the failure itself. It is knowing where the workflow stopped.
Suppose an agent has completed three steps:
Step 1 - Read customer information Completed
Step 2 - Validate account Completed
Step 3 - Create recommendation Failed
Step 4 - Send notification Not startedIf the application restarts, the system should ideally continue from the appropriate point rather than executing Steps 1 and 2 again.
What Durable Execution Means
Durable execution means that the system can persist enough workflow state to resume execution after interruptions.
Conceptually, the workflow becomes:
Start
|
v
Step 1
|
v
Save progress
|
v
Step 2
|
v
Save progress
|
v
Step 3
|
X Failure
|
v
Resume
|
v
Step 3
|
v
Step 4The important distinction is between application state and workflow progress.
Application state might contain customer records or configuration.
Workflow state answers questions such as:
Which step has completed?
Which operation is currently running?
What information must be carried into the next step?
What should happen after a failure?
Can a completed operation safely be skipped during recovery?
For long-running agents, these questions are essential.
Designing an Agent Workflow in C#
A useful approach is to separate the agent's workflow from the individual operations it performs.
For example:
public sealed class CustomerWorkflow
{
public async Task RunAsync(
string customerId,
CancellationToken cancellationToken)
{
var customer = await LoadCustomerAsync(
customerId,
cancellationToken);
var analysis = await AnalyzeCustomerAsync(
customer,
cancellationToken);
await SaveRecommendationAsync(
customerId,
analysis,
cancellationToken);
await SendNotificationAsync(
customerId,
analysis,
cancellationToken);
}
private Task<Customer> LoadCustomerAsync(
string customerId,
CancellationToken cancellationToken)
{
// Load customer information.
throw new NotImplementedException();
}
private Task<Recommendation> AnalyzeCustomerAsync(
Customer customer,
CancellationToken cancellationToken)
{
// Call an agent or model.
throw new NotImplementedException();
}
private Task SaveRecommendationAsync(
string customerId,
Recommendation recommendation,
CancellationToken cancellationToken)
{
// Persist the recommendation.
throw new NotImplementedException();
}
private Task SendNotificationAsync(
string customerId,
Recommendation recommendation,
CancellationToken cancellationToken)
{
// Notify the user.
throw new NotImplementedException();
}
}The example itself does not provide durability. It demonstrates an important architectural principle: make workflow steps explicit and independently recoverable.
A durable workflow system can then associate persisted state with these execution steps.
Why Checkpointing Matters
A checkpoint represents a known point of progress.
Consider an agent that processes a document:
1. Download document
2. Extract text
3. Summarize sections
4. Validate summary
5. Store resultIf the process fails during step 4, restarting from step 1 may waste resources and repeat external operations.
A better workflow can preserve progress:
Download Completed
Extract Completed
Summarize Completed
Validate Failed
Store PendingAfter recovery:
Validate
|
v
StoreThis becomes especially valuable when an agent performs expensive model calls or external operations.
Handling Transient Failures
Not every failure should stop an agent permanently.
Transient failures include situations such as:
temporary network problems
service throttling
short-lived database connectivity issues
temporary service unavailability
A retry policy can help.
A simple C# example looks like this:
public async Task<T> ExecuteWithRetryAsync<T>(
Func<Task<T>> operation,
int maxAttempts = 3)
{
Exception? lastException = null;
for (int attempt = 1; attempt <= maxAttempts; attempt++)
{
try
{
return await operation();
}
catch (Exception ex) when (attempt < maxAttempts)
{
lastException = ex;
var delay = TimeSpan.FromSeconds(
Math.Pow(2, attempt));
await Task.Delay(delay);
}
}
throw lastException!;
}This uses exponential backoff:
Join the conversation! Your thoughts help the community grow.