⚠️ This blog post was created with the help of AI tools. Yes, I used a bit of magic from language models to organize my thoughts and automate the boring parts, but the geeky fun and the πŸ€– in C# are 100% mine.

Hi!

Picture this: You’re building an AI-powered application. Your agent starts generating a detailed responseβ€”maybe a research summary, a long-form article, or complex code. Then… the network hiccups. Or the user closes their laptop. Or maybe they just need to ask a quick follow-up question before the agent finishes.

In most scenarios, you’re toast. The agent starts over from scratch, wasting tokens, time, andβ€”let’s be honestβ€”patience. Your users get frustrated. Your costs go up. Your demo fails right when you need it most. (We’ve all been there, right?)

But here’s the thing: it doesn’t have to be this way.

The Microsoft Agent Framework introduces a powerful feature called Background Responses that changes the game completely. Think of it as a checkpoint system for your AI agentβ€”you can pause, do something else, and resume exactly where you left off. No data loss. No repetition. Just seamless continuation.

Let me show you how it works.

Spoiler: if you want to avoid read all this just watch this video


What Are Background Responses?

Background responses use a continuation token mechanism to handle long-running AI operations gracefully. Here’s the core concept:

When you enable background responses, every streaming update your agent generates includes a continuation token. This token is like a bookmarkβ€”it captures the exact state of the conversation at that moment. If something interrupts the stream (network issues, user action, or deliberate pause), you can use that token to resume later.

The magic happens in just one line of code:

AgentRunOptions options = new()
{
    AllowBackgroundResponses = true  // That's it!
};

Once enabled, each AgentRunResponseUpdate carries a continuation token. Store it, interrupt the stream, and later pass it back to the agent to continue from that exact point.


When Should You Use Background Responses?

This feature shines in several real-world scenarios:

1. Long-Running Complex Tasks

Research summaries, detailed reports, or comprehensive code generation can take time. Background responses let users start the task, walk away, and check back later for results.

2. Network Resilience

Mobile apps, spotty WiFi, serverless functions with timeoutsβ€”any environment where network interruptions are a reality becomes more reliable with continuation tokens.

3. Interactive Workflows

Users often interrupt long responses to ask clarifying questions or request tools. Background responses let you handle those interruptions gracefully and return to the original task.

4. Cost Optimization

Why regenerate the same content multiple times when something goes wrong? Continuation tokens help you avoid wasting API calls and tokens.


The Core Pattern: Stream, Interrupt, Resume

Let’s walk through the essential pattern using code from our simple sample. Here’s the streaming phase:

// Create an agent with background responses enabled
var agent = responseClient.CreateAIAgent(
    name: "agent",
    instructions: "You are a helpful assistant");

var options = new AgentRunOptions
{
    AllowBackgroundResponses = true
};

var thread = agent.GetNewThread();
var question = "Write a 2 lines story about .NET Conf";

// Start streaming and capture updates
AgentRunResponseUpdate? latestReceivedUpdate = null;

await foreach (var update in agent.RunStreamingAsync(question, thread, options))
{
    Console.Write(update.Text);
    latestReceivedUpdate = update;  // Save the latest update
    
    // Simulate an interruption
    if (!string.IsNullOrWhiteSpace(update.Text))
    {
        break;  // Stop streaming!
    }
}

At this point, you’ve interrupted the stream. The agent has started telling a story, but you’ve stopped listening. The key? You’ve captured latestReceivedUpdate, which contains your continuation token.

Now for the resume phase:

// Resume from where we stopped
options.ContinuationToken = latestReceivedUpdate!.ContinuationToken;

await foreach (var update in agent.RunStreamingAsync(thread, options))
{
    Console.Write(update.Text);  // Agent continues the story!
}

That’s it. The agent picks up exactly where it left off. No repeated content. No starting over. Just seamless continuation.


Taking It Further: Interleaved Queries

Here’s where it gets really interesting. What if you need to ask quick questions while a long response is processing?

Our complex sample demonstrates exactly this scenario:

  1. Start a long-running response (like a detailed story)
  2. Interrupt and capture the continuation token
  3. Ask the agent quick questions (“What’s 2+2?” “What’s the capital of Italy?”)
  4. Get immediate answers to those questions
  5. Resume the original long response using the saved token

All on the same thread. Same agent. Three separate conversations elegantly juggled without losing context.

This pattern is gold for real-world applications where users need to multitask or where your agent needs to call tools or APIs mid-generation.


Works with Tools Too

Background responses also play nicely with function calling and tools. Our tools sample shows how an agent can pause streaming, execute a weather API function, and continue the responseβ€”all while maintaining continuation token support.

The framework handles the complexity. You just enable the feature and let it work.


Provider Agnostic

The best part? This feature works across providers. Whether you’re using:

  • Azure OpenAI (via Azure OpenAI Responses API)
  • GitHub Models (for quick prototyping)
  • Ollama (for local inference)

As long as the provider supports the Agent Framework’s response API, you get background responses out of the box. No provider-specific code. No special configuration. Just standard Agent Framework patterns.


Key Takeaways

Let me wrap this up with the essentials:

βœ… Continuation tokens are bookmarks – Save your place in any AI response and resume later
βœ… One-line enablement – AllowBackgroundResponses = true is all you need
βœ… Resilient by design – Handle interruptions, network issues, and user actions gracefully
βœ… Perfect for real-world UX – Let users multitask, ask quick questions, or walk away mid-process
βœ… Provider agnostic – Works with Azure OpenAI, GitHub Models, Ollama, and more
βœ… Production ready – Used in real applications for complex workflows and long-running tasks


Try It Yourself

Ready to implement background responses in your own AI agents? Here’s where to start:

πŸ“š Official Documentation

Agent Background Responses Guide – Complete reference with detailed explanations

πŸ’» Code Samples

All three samples are ready to run in this repository:

  • Simple Sample – Basic streaming with interruption and continuation
  • Tools Sample – Background responses with function calling
  • Complex Sample – Interleaved queries and advanced workflows

πŸ“– More Context

Background Responses Detailed Guide – Deep dive into the feature with setup instructions

AgentFx Main Lesson – Complete guide to Microsoft Agent Framework in .NET


What Will You Build?

Background responses unlock patterns that were previously complex or impossible. Long-running research agents that survive network blips. Interactive assistants that handle interruptions gracefully. Multi-step workflows that users can pause and resume.

The continuation token pattern is simple, but the possibilities are vast.

Give it a try. Build something interesting. And when you do, tag me on social mediaβ€”I’d love to see what you create!

Happy coding! πŸš€

Related Posts:

Happy coding!

Greetings

El Bruno

More posts in my blog ElBruno.com.

More info in https://beacons.ai/elbruno


2 responses to “🧠 Never Lose Your AI Agent’s Train of Thought: Background Responses in .NET”

  1. […] 🧠 Never Lose Your AI Agent’s Train of Thought: Background Responses in .NET (Bruno Capuano) […]

    Like

  2. […] 🧠 Never Lose Your AI Agent’s Train of Thought: Background Responses in .NET (29 Oct 2025) – Introduces the background response feature. This post explains how to enable and use continuation tokens to pause and resume agent responses, with a sample scenario and code. Link: https://elbruno.com/2025/10/29/%F0%9F%A7%A0-never-lose-your-ai-agents-train-of-thought-background-re… […]

    Like

Leave a reply to Introducing the Microsoft Agent Framework – A Dev-Friendly Recap – El Bruno Cancel reply

Discover more from El Bruno

Subscribe now to keep reading and get access to the full archive.

Continue reading