Skip to content

.NET: Persist messages during function call loop#4762

Open
westey-m wants to merge 6 commits intomicrosoft:mainfrom
westey-m:persist-within-functioncallloop
Open

.NET: Persist messages during function call loop#4762
westey-m wants to merge 6 commits intomicrosoft:mainfrom
westey-m:persist-within-functioncallloop

Conversation

@westey-m
Copy link
Contributor

Motivation and Context

Allow persisting messages during the function call loop when using ChatHistoryProvider. This allows us to match the behavior of service stored chat history, e.g. responses with store=true, which persists chat history with each call.

#2889

Description

  • Introduce a ChatClient decorator that allows persisting messages to a ChatHistoryProvider during the function call loop.
  • Introduce a setting on ChatClientAgentOptions that allows switching on this behavior.

Contribution Checklist

  • The code builds clean without any errors or warnings
  • The PR follows the Contribution Guidelines
  • All unit tests pass, and I have added new tests where possible
  • Is this a breaking change? If yes, add "[BREAKING]" prefix to the title of the PR.

Copilot AI review requested due to automatic review settings March 18, 2026 11:38
@markwallace-microsoft markwallace-microsoft added documentation Improvements or additions to documentation .NET labels Mar 18, 2026
@github-actions github-actions bot changed the title Persist messages during function call loop .NET: Persist messages during function call loop Mar 18, 2026
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Adds an opt-in mechanism for persisting chat history (and notifying context providers) after each individual service call within a ChatClientAgent run, including during tool/function-invocation loops—improving recoverability and observability when runs are interrupted.

Changes:

  • Introduces ChatHistoryPersistingChatClient decorator and wires it into the default chat client middleware pipeline when enabled.
  • Adds ChatClientAgentOptions.PersistChatHistoryAfterEachServiceCall and adjusts ChatClientAgent to skip end-of-run persistence when per-call persistence is enabled.
  • Adds unit tests and a new Step 19 sample demonstrating in-function-loop checkpointing.

Reviewed changes

Copilot reviewed 12 out of 12 changed files in this pull request and generated 3 comments.

Show a summary per file
File Description
dotnet/src/Microsoft.Agents.AI/ChatClient/ChatHistoryPersistingChatClient.cs New delegating chat client that notifies providers per service call and deduplicates already-persisted messages.
dotnet/src/Microsoft.Agents.AI/ChatClient/ChatClientExtensions.cs Injects the new decorator into the default middleware pipeline when the option is enabled.
dotnet/src/Microsoft.Agents.AI/ChatClient/ChatClientAgentOptions.cs Adds the new opt-in option (marked experimental) and includes it in Clone().
dotnet/src/Microsoft.Agents.AI/ChatClient/ChatClientAgent.cs Refactors provider notification to support both end-of-run and per-service-call flows.
dotnet/src/Microsoft.Agents.AI/ChatClient/ChatClientAgentSession.cs Adds new using directives (currently introduces build-breaking unused usings).
dotnet/src/Microsoft.Agents.AI/Compaction/SummarizationCompactionStrategy.cs Adds a file-level suppression for CA1873.
dotnet/tests/Microsoft.Agents.AI.UnitTests/ChatClient/ChatHistoryPersistingChatClientTests.cs New unit tests validating per-call persistence, deduping, and decorator injection behavior.
dotnet/samples/02-agents/Agents/README.md Adds sample entry for Step 19.
dotnet/samples/02-agents/Agents/Agent_Step19_InFunctionLoopCheckpointing/* New sample (README + Program + csproj) demonstrating the new option.
dotnet/agent-framework-dotnet.slnx Adds the new sample project to the solution.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

documentation Improvements or additions to documentation .NET

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants