AI agents are moving from simple chat interfaces into applications that perform actual work. An agent may receive a request, call an API, query a database, execute a tool, wait for another system, and then continue the workflow. Once an application works this way, keeping the agent's state only in memory becomes a reliability problem.
A simple ASP.NET Core endpoint can process a request from beginning to end without storing much state. An AI agent workflow is different. The workflow may take several seconds or several minutes, depend on external services, encounter transient failures, or require human approval before continuing.
Durable state gives the application a way to remember where the workflow is, what has already happened, which tools were called, and what information is still required.
This article explains how to design an AI agent workflow in ASP.NET Core using durable state, how to separate workflow state from conversational history, and how to make retries and recovery safer in production.
Why AI Agent Workflows Need Durable State
Consider an AI support agent that receives this request:
Investigate order 48291, check its payment status, contact the shipping service,
and create a support ticket if the shipment is delayed.The agent may perform several operations:
Read the order.
Check payment status.
Call the shipping API.
Determine whether the shipment is delayed.
Create a support ticket.
Return the result to the user.
If everything happens inside one HTTP request, the design appears simple.
HTTP Request
|
v
AI Agent
|
+--> Order API
|
+--> Payment API
|
+--> Shipping API
|
+--> Ticket API
|
v
HTTP ResponseThe problem appears when something fails halfway through the process.
Suppose the shipping API succeeds, but the ticket API times out. The application now needs to know whether the shipping operation already completed. Otherwise, a retry could call the shipping API again and create an unexpected duplicate operation.
This is where durable state becomes important.
Instead of treating the workflow as one long request, store the workflow state as it progresses.
Request
|
v
Create Workflow
|
v
Persist State
|
v
Execute Step
|
v
Persist Result
|
v
Execute Next Step
|
v
Persist ResultIf the process stops, the application can resume from the last safe state instead of starting everything again.
Durable State and Conversation History Are Different
One common design mistake is treating chat history as workflow state.
They are related, but they serve different purposes.
Conversation history answers questions such as:
What did the user ask?
What did the agent say?
What information did the user provide?Workflow state answers questions such as:
Which step has completed?
Which tool was called?
What was the result?
What step should execute next?
Was an external operation already performed?
Does the workflow require approval?A production system may therefore have both.
Conversation
|
+--> User messages
+--> Assistant messages
+--> Tool messages
Workflow
|
+--> Current state
+--> Completed steps
+--> Tool results
+--> Retry information
+--> Approval statusKeeping these concepts separate makes the system easier to reason about.
Define the Workflow State Explicitly
The first step is creating a state model.
For example:
public enum WorkflowStatus
{
Created,
Running,
WaitingForApproval,
Completed,
Failed
}
public sealed class AgentWorkflowState
{
public Guid WorkflowId { get; init; }
public string UserRequest { get; init; } = string.Empty;
public WorkflowStatus Status { get; set; }
public int CurrentStep { get; set; }
public bool OrderChecked { get; set; }
public bool PaymentChecked { get; set; }
public bool ShippingChecked { get; set; }
public bool TicketCreated { get; set; }
public string? OrderStatus { get; set; }
public string? PaymentStatus { get; set; }
public string? ShippingStatus { get; set; }
public string? ErrorMessage { get; set; }
}The exact properties will vary by application. The important part is that the workflow state represents business progress rather than internal implementation details.
For example, storing:
ShippingChecked = trueis more useful than storing:
LastMethodExecuted = "ExecuteToolHandler42"The first represents business state. The second is tightly coupled to the implementation.
Store State in a Durable Repository
ASP.NET Core applications can use a database-backed repository for workflow state.
A simple abstraction keeps the agent logic independent of the storage technology.
public interface IWorkflowStateStore
{
Task<AgentWorkflowState?> GetAsync(
Guid workflowId,
CancellationToken cancellationToken);
Task SaveAsync(
AgentWorkflowState state,
CancellationToken cancellationToken);
}A PostgreSQL, SQL Server, or another transactional database can implement this interface.
The agent does not need to know where the state is stored.
AI Workflow
|
v
IWorkflowStateStore
|
+--> PostgreSQL
|
+--> SQL Server
|
+--> Other durable storeThis separation also makes testing easier.
Create the Workflow From an ASP.NET Core Endpoint
The API endpoint should generally create the workflow rather than execute a long-running agent process synchronously.
For example:
app.MapPost("/api/agent/workflows",
async (
CreateWorkflowRequest request,
IWorkflowStateStore store,
CancellationToken cancellationToken) =>
{
var workflowId = Guid.NewGuid();
var state = new AgentWorkflowState
{
WorkflowId = workflowId,
UserRequest = request.Message,
Status = WorkflowStatus.Created,
CurrentStep = 0
};
await store.SaveAsync(state, cancellationToken);
return Results.Accepted(
$"/api/agent/workflows/{workflowId}",
new
{
workflowId,
status = state.Status
});
});The important behavior here is that the API returns quickly.
The client receives a workflow identifier and can later retrieve the status.
POST /api/agent/workflows
|
v
Workflow ID
|
v
Background processing
|
v
GET /api/agent/workflows/{id}This approach prevents long-running AI operations from being unnecessarily tied to an HTTP request.
Execute Each Agent Step Independently
An agent workflow should be divided into small steps.
For example:
Step 1: Understand request
Step 2: Load order
Step 3: Check payment
Step 4: Check shipping
Step 5: Create support ticket
Step 6: Generate responseEach step should have a clear input and output.
A simple service could look like this:
public interface IAgentStep
{
Task ExecuteAsync(
AgentWorkflowState state,
CancellationToken cancellationToken);
}An order step could then update the durable state after successful completion.
public sealed class OrderCheckStep : IAgentStep
{
private readonly IOrderService _orderService;
public OrderCheckStep(IOrderService orderService)
{
_orderService = orderService;
}
public async Task ExecuteAsync(
AgentWorkflowState state,
CancellationToken cancellationToken)
{
var order = await _orderService.GetAsync(
state.UserRequest,
cancellationToken);
state.OrderChecked = true;
state.OrderStatus = order.Status;
state.CurrentStep++;
}
}The workflow engine can persist the updated state after the step completes.
Execute Step
|
v
Update State
|
v
Save State
|
v
Next StepThis gives the system a recovery point after each important operation.
Do Not Store Only the Final AI Response
Another common mistake is storing only the final response generated by the model.
For example:
WorkflowId
FinalResponse
Status = CompletedThis is not enough for troubleshooting.
A production workflow should normally preserve useful execution information such as:
Workflow ID
Current state
Current step
Completed steps
Tool execution status
External operation identifiers
Retry count
Failure information
Timestamps
Approval stateThe exact amount of information depends on the application's security and compliance requirements.
Sensitive data should not be stored simply because it might be useful for debugging.
Make External Tool Calls Idempotent
Durable state does not automatically prevent duplicate operations.
Consider this sequence:
Create Ticket
|
v
Ticket API succeeds
|
v
Application crashes before saving stateAfter recovery, the application may believe the ticket was never created.
It could call the ticket API again.
This is why external operations should support idempotency whenever possible.
Join the conversation! Your thoughts help the community grow.