.NET: Persist messages during function call loop#4762
Open
westey-m wants to merge 6 commits intomicrosoft:mainfrom
Open
.NET: Persist messages during function call loop#4762westey-m wants to merge 6 commits intomicrosoft:mainfrom
westey-m wants to merge 6 commits intomicrosoft:mainfrom
Conversation
Contributor
There was a problem hiding this comment.
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
ChatHistoryPersistingChatClientdecorator and wires it into the default chat client middleware pipeline when enabled. - Adds
ChatClientAgentOptions.PersistChatHistoryAfterEachServiceCalland adjustsChatClientAgentto 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. |
dotnet/src/Microsoft.Agents.AI/ChatClient/ChatClientAgentSession.cs
Outdated
Show resolved
Hide resolved
dotnet/src/Microsoft.Agents.AI/Compaction/SummarizationCompactionStrategy.cs
Outdated
Show resolved
Hide resolved
dotnet/src/Microsoft.Agents.AI/ChatClient/ChatHistoryPersistingChatClient.cs
Show resolved
Hide resolved
SergeyMenshykh
approved these changes
Mar 18, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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
Contribution Checklist