Skip to main content

How to Resume a Microsoft Agent Framework Workflow from the Checkpoint Where It Waits for an Answer in C#

· 16 min read
Jagdish Kumawat
Founder @ Dewiride

When a step fails after someone has answered a Microsoft Agent Framework workflow, the answer has to go in again, and the session's latest checkpoint is the wrong place to give it: nothing waits there any more. Keep the checkpoint where the run waits for its answer, and resume from that one as often as you need. This guide shows both in a C# article review whose publish step fails on the first try.

Syntax​

Syntax
// When the run stops at its question: keep the checkpoint where it waits.
CheckpointInfo <waiting> = <run>.LastCheckpoint!;

// Its two IDs, to keep where your app keeps its data.
string <sessionId> = <waiting>.SessionId;
string <checkpointId> = <waiting>.CheckpointId;

// Later, even in a new process: the same CheckpointInfo, from the two IDs.
CheckpointInfo <checkpoint> = new CheckpointInfo(<sessionId>, <checkpointId>);

// Answer from it, as many times as you need.
await using Run <resumed> = await InProcessExecution.ResumeAsync(<workflow>, <checkpoint>, <checkpointManager>);
PartWhat it is
<run>.LastCheckpointGets the most recent checkpoint information. Read it when the run has stopped at its question: the most recent checkpoint is then the one where the run waits.
!LastCheckpoint is declared as CheckpointInfo?, a value that may be null. A run with a checkpoint manager has saved its checkpoints by the time it stops at the question, and ! tells the compiler the value isn't null.
<waiting>.SessionIdGets the unique identifier for the current session.
<waiting>.CheckpointIdThe unique identifier for the checkpoint.
new CheckpointInfo(<sessionId>, <checkpointId>)Initializes a new instance of the CheckpointInfo class with the specified session and checkpoint identifiers. Neither ID may be null or empty.
InProcessExecution.ResumeAsyncLoads the checkpoint into a new run of <workflow>, which has to be built the same way as the workflow that saved it. The question comes back as a RequestInfoEvent, ready for an answer.

Microsoft's human-in-the-loop guide explains why that checkpoint is the one to keep: When a checkpoint is created, pending requests are also saved as part of the checkpoint state. And on the way back: When you restore from a checkpoint, any pending requests will be re-emitted as RequestInfoEvent objects, allowing you to capture and respond to them. A resumed run goes on saving checkpoints in the same session, so once an answer has gone in, the session's newest checkpoints come from after the answer.

The Example​

A console app called ArticleReview asks an editor whether to publish an article. Its workflow has three steps: DescribeArticle turns the title into a question, the request port AskEditor waits for a yes or a no, and PublishArticle publishes the article when the answer is yes.

To show a step that fails after the answer, the publish step depends on a stand-in for a publishing service: a static flag, Publisher.Online. While it's true, PublishArticle returns Published.. While it's false, PublishArticle throws an InvalidOperationException with the message The publishing service is down..

The program starts the run with the title Ten tips for faster builds, keeps the checkpoint where the run waits, and then answers yes three times, each time in a new run resumed from a checkpoint:

  1. From the checkpoint where the run waits, while Publisher.Online is false.
  2. From the session's latest checkpoint.
  3. From the checkpoint where the run waits again, with Publisher.Online back to true.

Everything happens in one run of the program, with the checkpoints in memory, so you see the three answers side by side. There's no AI model in it, so you don't need an Azure account.

Prerequisites​

  • The .NET 10 SDK. dotnet --version prints a version that starts with 10.0; this guide used 10.0.404.

Step 1: Create a Console App​

In a terminal, in the folder where you keep your projects, create a console app, move into it and add Agent Framework's workflows package:

Terminal
dotnet new console -o ArticleReview
cd ArticleReview
dotnet add package Microsoft.Agents.AI.Workflows --version 1.28.0
  • dotnet new console -o ArticleReview creates a console app in a new ArticleReview folder, and cd moves into it.
  • dotnet add package adds Microsoft.Agents.AI.Workflows, which has the workflow types and the checkpoint manager. --version keeps you on the version this guide used.
Output (shortened)
The template "Console App" was created successfully.

Processing post-creation actions...
Restoring /Users/you/source/ArticleReview/ArticleReview.csproj:
Restore succeeded.
…
info : PackageReference for package 'Microsoft.Agents.AI.Workflows' version '1.28.0' added to file '/Users/you/source/ArticleReview/ArticleReview.csproj'.
info : Generating MSBuild file /Users/you/source/ArticleReview/obj/ArticleReview.csproj.nuget.g.targets.
info : Writing assets file to disk. Path: /Users/you/source/ArticleReview/obj/project.assets.json
log : Restored /Users/you/source/ArticleReview/ArticleReview.csproj (in 750 ms).

The project file now has the package:

ArticleReview.csproj
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net10.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.Agents.AI.Workflows" Version="1.28.0" />
</ItemGroup>

</Project>

Step 2: Create the Checkpoint Manager​

Replace everything in Program.cs with:

Program.cs
using Microsoft.Agents.AI.Workflows;

const string SessionId = "article-7";
CheckpointManager checkpointManager = CheckpointManager.CreateInMemory();
  • SessionId is the session the run's checkpoints are saved under.
  • CheckpointManager.CreateInMemory() creates a checkpoint manager that keeps the checkpoints in the program's memory. That's enough here, because every answer comes in the same run of the program.

Step 3: Build a Workflow Whose Last Step Can Fail​

Add the method that builds the workflow, and the Publisher class after it, at the end of the file:

Program.cs (at the end of the file)
static Workflow BuildWorkflow()
{
RequestPort askEditor = RequestPort.Create<string, bool>("AskEditor");

Func<string, string> describe = title => $"Publish \"{title}\"?";
Func<bool, string> publish = approved =>
!approved ? "Not published."
: Publisher.Online ? "Published."
: throw new InvalidOperationException("The publishing service is down.");

ExecutorBinding describeArticle = describe.BindAsExecutor("DescribeArticle");
ExecutorBinding publishArticle = publish.BindAsExecutor("PublishArticle");

return new WorkflowBuilder(describeArticle)
.AddEdge(describeArticle, askEditor)
.AddEdge(askEditor, publishArticle)
.WithOutputFrom(publishArticle)
.Build();
}

static class Publisher
{
public static bool Online { get; set; } = true;
}
  • RequestPort.Create<string, bool>("AskEditor") is the request port: the workflow sends it a question as a string and waits for a bool answer. How to Pause a Microsoft Agent Framework Workflow Until a Person Answers in C# explains request ports step by step.
  • describe turns the title into the question. \" puts a double quote inside the string, so the question shows the title in quotes.
  • publish handles the answer with two conditional operators: a ? b : c gives b when a is true, and c otherwise. A no gives Not published.. A yes gives Published. while Publisher.Online is true, and otherwise the throw expression throws the InvalidOperationException.
  • BindAsExecutor turns each function into a step with a fixed ID, DescribeArticle and PublishArticle, and the edges connect the first step to the port and the port to the second step.
  • Publisher is a static class with one property, Online, which starts as true. It stands in for a real publishing service, so the program can turn the service off and back on.

Step 4: Start the Run and Keep the Checkpoint Where It Waits​

Add this below the checkpoint manager:

Program.cs (below the checkpoint manager)
// Start the run. It asks the editor and waits: keep the checkpoint where it waits.
CheckpointInfo waiting;
await using (Run run = await InProcessExecution.RunAsync(BuildWorkflow(), "Ten tips for faster builds", checkpointManager, SessionId))
{
Console.WriteLine($"Started: {await run.GetStatusAsync()}");
waiting = run.LastCheckpoint!;
}
  • CheckpointInfo waiting; declares the variable before the block, so it's still there after the block ends.
  • InProcessExecution.RunAsync runs the workflow with the title as its input, the checkpoint manager and the session ID. It returns at the run's first halt: the question.
  • await using (...) { ... } disposes of the run at the closing brace. Its checkpoints stay in the checkpoint manager.
  • run.LastCheckpoint! is the run's most recent checkpoint. The run has just stopped at the question, so this is the checkpoint where it waits, and waiting keeps it.

Step 5: Answer Three Times​

Add this below the run's block:

Program.cs (below the run)
// The editor approves while the publishing service is down.
Publisher.Online = false;
Console.WriteLine("Answer 1, from the checkpoint where the run waits:");
await AnswerAsync(waiting);

// Continuing from the latest checkpoint instead:
CheckpointInfo latest = (await checkpointManager.GetLatestCheckpointAsync(SessionId))!;
Console.WriteLine("Answer 2, from the latest checkpoint:");
await AnswerAsync(latest);

// The service is back. Continue from the checkpoint where the run waits again.
Publisher.Online = true;
Console.WriteLine("Answer 3, from the checkpoint where the run waits:");
await AnswerAsync(waiting);
  • Answer 1 turns the publishing service off by setting Publisher.Online to false, then answers from waiting.
  • Answer 2 answers from the session's latest checkpoint instead. GetLatestCheckpointAsync returns it, or null when the session has no checkpoints, and ! tells the compiler the session has some.
  • Answer 3 turns the service back on and answers from waiting again.
  • AnswerAsync does the answering. You write it in the next step.

Step 6: Answer from a Checkpoint​

Add this below the three answers:

Program.cs (below the three answers)
async Task AnswerAsync(CheckpointInfo checkpoint)
{
await using Run run = await InProcessExecution.ResumeAsync(BuildWorkflow(), checkpoint, checkpointManager);

RequestInfoEvent? question = run.OutgoingEvents.OfType<RequestInfoEvent>().SingleOrDefault();
if (question is null)
{
Console.WriteLine($" Nothing is waiting for an answer. Status: {await run.GetStatusAsync()}");
return;
}

await run.ResumeAsync([question.Request.CreateResponse(true)]);

foreach (WorkflowEvent workflowEvent in run.NewEvents)
{
if (workflowEvent is WorkflowOutputEvent output)
{
Console.WriteLine($" Output: {output.Data}");
}
else if (workflowEvent is ExecutorFailedEvent failed)
{
Console.WriteLine($" {failed.ExecutorId} failed: {failed.Data?.Message}");
}
}
}
  • async Task AnswerAsync(CheckpointInfo checkpoint) is a local function: a method written among the program's top-level statements, which can use checkpointManager from above.
  • InProcessExecution.ResumeAsync(BuildWorkflow(), checkpoint, checkpointManager) loads the checkpoint into a new run of a workflow built the same way as the first, and runs it until its first halt. await using disposes of the run when the method ends.
  • SingleOrDefault() gives the one RequestInfoEvent among the run's outgoing events, or null when there's none. With null, nothing waits in that checkpoint: the method prints the run's status and returns.
  • CreateResponse(true) answers yes, and ResumeAsync sends the answer into the run, which goes on to PublishArticle.
  • The loop goes through NewEvents, which on its first read holds every event the run has emitted, and picks out two kinds. A WorkflowOutputEvent carries the workflow's output. An ExecutorFailedEvent names the failed step in ExecutorId and carries its exception in Data, and ?. guards against that exception being null. How to Handle a Failed Step in a Microsoft Agent Framework Workflow in C# explains these events.

Step 7: Run the App​

Terminal
dotnet run
Output
Started: PendingRequests
Answer 1, from the checkpoint where the run waits:
PublishArticle failed: The publishing service is down.
Answer 2, from the latest checkpoint:
Nothing is waiting for an answer. Status: Ended
Answer 3, from the checkpoint where the run waits:
Output: Published.

macOS-style terminal window titled Terminal — ArticleReview showing dotnet run printing Started: PendingRequests, then answer 1 from the checkpoint where the run waits with PublishArticle failed: The publishing service is down., answer 2 from the latest checkpoint with Nothing is waiting for an answer. Status: Ended, and answer 3 from the checkpoint where the run waits with Output: Published.

Line by line:

  1. Started: PendingRequests — the run stopped at the question, and waiting holds the checkpoint saved there.
  2. Answer 1 — the question came back from waiting, the yes went in, and PublishArticle threw because the service was off. The article isn't published.
  3. Answer 2 — the latest checkpoint has no question in it: Nothing is waiting for an answer., and the resumed run's status is Ended, which Agent Framework's documentation describes as No further events will be emitted, and no messages can be sent to it.
  4. Answer 3 — waiting still had the question. The same yes, with the service back on, gave Output: Published..

Why the Latest Checkpoint Has Nothing Waiting​

Answer 1's run didn't end at the question. It took the answer and ran PublishArticle, and a run saves checkpoints in its session as it goes: Microsoft's checkpoints guide says Checkpoints are created at the end of each superstep, after all executors in that superstep have completed their execution. So the latest checkpoint wasn't waiting any more. It came from answer 1's run, after the answer had gone in, and no question waits in it.

Saving newer checkpoints didn't change the older one. Answer 3 resumed from waiting after answers 1 and 2, and the question came back as it did the first time. That's the checkpoint to answer from, however many times an answer has to go in.

:::warning A finished run is worse After a run that has finished with the status Idle, the latest checkpoint isn't only empty: InProcessExecution.ResumeAsync from it doesn't return. Fixed: InProcessExecution.ResumeAsync Never Returns When a Microsoft Agent Framework Workflow Has Already Finished shows that hang and how keeping the waiting checkpoint avoids it. :::

Keep the Waiting Checkpoint Across a Restart​

Here waiting lives in a variable, because all three answers come in one run of the program. When the answer comes after a restart, the variable is gone. Keep the waiting checkpoint's two IDs, waiting.SessionId and waiting.CheckpointId, where your app keeps its data, and on the next start build the same CheckpointInfo from them with new CheckpointInfo(sessionId, checkpointId), as in the Syntax section. The checkpoints themselves have to outlive the program too: How to Resume a Microsoft Agent Framework Workflow from a Checkpoint After a Restart in C# keeps them in files, and the fixes post above writes the waiting checkpoint's ID to a file, with the session ID fixed in its code.

The Complete File​

Program.cs
using Microsoft.Agents.AI.Workflows;

const string SessionId = "article-7";
CheckpointManager checkpointManager = CheckpointManager.CreateInMemory();

// Start the run. It asks the editor and waits: keep the checkpoint where it waits.
CheckpointInfo waiting;
await using (Run run = await InProcessExecution.RunAsync(BuildWorkflow(), "Ten tips for faster builds", checkpointManager, SessionId))
{
Console.WriteLine($"Started: {await run.GetStatusAsync()}");
waiting = run.LastCheckpoint!;
}

// The editor approves while the publishing service is down.
Publisher.Online = false;
Console.WriteLine("Answer 1, from the checkpoint where the run waits:");
await AnswerAsync(waiting);

// Continuing from the latest checkpoint instead:
CheckpointInfo latest = (await checkpointManager.GetLatestCheckpointAsync(SessionId))!;
Console.WriteLine("Answer 2, from the latest checkpoint:");
await AnswerAsync(latest);

// The service is back. Continue from the checkpoint where the run waits again.
Publisher.Online = true;
Console.WriteLine("Answer 3, from the checkpoint where the run waits:");
await AnswerAsync(waiting);

async Task AnswerAsync(CheckpointInfo checkpoint)
{
await using Run run = await InProcessExecution.ResumeAsync(BuildWorkflow(), checkpoint, checkpointManager);

RequestInfoEvent? question = run.OutgoingEvents.OfType<RequestInfoEvent>().SingleOrDefault();
if (question is null)
{
Console.WriteLine($" Nothing is waiting for an answer. Status: {await run.GetStatusAsync()}");
return;
}

await run.ResumeAsync([question.Request.CreateResponse(true)]);

foreach (WorkflowEvent workflowEvent in run.NewEvents)
{
if (workflowEvent is WorkflowOutputEvent output)
{
Console.WriteLine($" Output: {output.Data}");
}
else if (workflowEvent is ExecutorFailedEvent failed)
{
Console.WriteLine($" {failed.ExecutorId} failed: {failed.Data?.Message}");
}
}
}

static Workflow BuildWorkflow()
{
RequestPort askEditor = RequestPort.Create<string, bool>("AskEditor");

Func<string, string> describe = title => $"Publish \"{title}\"?";
Func<bool, string> publish = approved =>
!approved ? "Not published."
: Publisher.Online ? "Published."
: throw new InvalidOperationException("The publishing service is down.");

ExecutorBinding describeArticle = describe.BindAsExecutor("DescribeArticle");
ExecutorBinding publishArticle = publish.BindAsExecutor("PublishArticle");

return new WorkflowBuilder(describeArticle)
.AddEdge(describeArticle, askEditor)
.AddEdge(askEditor, publishArticle)
.WithOutputFrom(publishArticle)
.Build();
}

static class Publisher
{
public static bool Online { get; set; } = true;
}

macOS-style code window titled Program.cs — ArticleReview showing an in-memory checkpoint manager, a run that keeps run.LastCheckpoint as the checkpoint where it waits, three answers that resume from the waiting checkpoint, from the latest checkpoint and from the waiting checkpoint again, an AnswerAsync method that answers the re-emitted RequestInfoEvent and prints the output or the ExecutorFailedEvent, and a workflow whose PublishArticle step throws while Publisher.Online is false

Common Mistakes​

MistakeWhat happensDo this instead
Answering again from the latest checkpoint after a step failedNothing waits there: the example printed Nothing is waiting for an answer. Status: EndedAnswer from the checkpoint where the run waits
Reading LastCheckpoint after the answer has gone inIt gives a newer checkpoint, saved after the answerRead it when the run has stopped at the question
Resuming from the latest checkpoint of a run that has finishedResumeAsync doesn't returnResume only from the checkpoint where the run waits, while it waits
Using CheckpointManager.CreateInMemory() when the answer comes after a restartThe checkpoints live in the program's memory and end with itKeep them in a store that lasts, such as files

FAQ​

Can I resume the same checkpoint more than once?​

Yes. The example resumed waiting twice, for answers 1 and 3, and the question came back both times. Each resume starts a new run from the same saved state.

Can I go back to the waiting checkpoint on the run I already have?​

Microsoft's checkpoints guide shows that too: You can resume a workflow from a specific checkpoint directly on the same run. Its example calls RestoreCheckpointAsync on the run. This guide starts a new run for each answer with InProcessExecution.ResumeAsync instead.

How do I see which step failed?​

From the ExecutorFailedEvent: its ExecutorId names the step, and its Data holds the exception. In the example, that's PublishArticle failed: The publishing service is down..

Conclusion​

Read run.LastCheckpoint when a run stops at its question, and keep it: it's the checkpoint where the run waits, and every resume from it brings the question back. Don't rely on the session's latest checkpoint to answer again, because once an answer has gone in, newer checkpoints have nothing waiting. In the example, the first yes failed with The publishing service is down., the latest checkpoint had nothing waiting, and the same yes from the waiting checkpoint gave Published..

Additional Resources​