Skip to content

Commit 91bf1e2

Browse files
address PR review comments
1 parent 9d8446f commit 91bf1e2

1 file changed

Lines changed: 17 additions & 2 deletions

File tree

agent-framework/user-guide/agents/agent-background-responses.md

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,9 @@ ms.service: agent-framework
1313

1414
The Microsoft Agent Framework supports background responses for handling long-running operations that may take time to complete. This feature enables agents to start processing a request and return a continuation token that can be used to poll for results or resume interrupted streams.
1515

16+
> [!TIP]
17+
> For a complete working example, see the [Background Responses sample](https://github.com/microsoft/agent-framework/blob/main/dotnet/samples/GettingStarted/Agents/Agent_Step17_BackgroundResponses/Program.cs).
18+
1619
## When to Use Background Responses
1720

1821
Background responses are particularly useful for:
@@ -52,6 +55,12 @@ Some agents may not allow explicit control over background responses. These agen
5255
For non-streaming scenarios, when you initially run an agent, it may or may not return a continuation token. If no continuation token is returned, it means the operation has completed. If a continuation token is returned, it indicates that the agent has initiated a background response that is still processing and will require polling to retrieve the final result:
5356

5457
```csharp
58+
AIAgent agent = new AzureOpenAIClient(
59+
new Uri("https://<myresource>.openai.azure.com"),
60+
new AzureCliCredential())
61+
.GetOpenAIResponseClient("<deployment-name>")
62+
.CreateAIAgent();
63+
5564
AgentRunOptions options = new()
5665
{
5766
AllowBackgroundResponses = true
@@ -60,7 +69,7 @@ AgentRunOptions options = new()
6069
AgentThread thread = agent.GetNewThread();
6170

6271
// Get initial response - may return with or without a continuation token
63-
AgentRunResponse response = await agent.RunAsync("What is the weather like in Amsterdam?", thread, options);
72+
AgentRunResponse response = await agent.RunAsync("Write a very long novel about otters in space.", thread, options);
6473

6574
// Continue to poll until the final response is received
6675
while (response.ContinuationToken is not null)
@@ -88,6 +97,12 @@ Console.WriteLine(response.Text);
8897
In streaming scenarios, background responses work much like regular streaming responses - the agent streams all updates back to consumers in real-time. However, the key difference is that if the original stream gets interrupted, agents support stream resumption through continuation tokens. Each update includes a continuation token that captures the current state, allowing the stream to be resumed from exactly where it left off by passing this token to subsequent streaming API calls:
8998

9099
```csharp
100+
AIAgent agent = new AzureOpenAIClient(
101+
new Uri("https://<myresource>.openai.azure.com"),
102+
new AzureCliCredential())
103+
.GetOpenAIResponseClient("<deployment-name>")
104+
.CreateAIAgent();
105+
91106
AgentRunOptions options = new()
92107
{
93108
AllowBackgroundResponses = true
@@ -97,7 +112,7 @@ AgentThread thread = agent.GetNewThread();
97112

98113
AgentRunResponseUpdate? latestReceivedUpdate = null;
99114

100-
await foreach (var update in agent.RunStreamingAsync("Tell me a joke about a pirate.", thread, options))
115+
await foreach (var update in agent.RunStreamingAsync("Write a very long novel about otters in space.", thread, options))
101116
{
102117
Console.Write(update.Text);
103118

0 commit comments

Comments
 (0)