Skip to content

.Net: Add ChatReducerTriggerEvent with AfterToolCallResponseReceived support#13511

Draft
Copilot wants to merge 5 commits intomainfrom
copilot/add-chat-reducer-trigger-event
Draft

.Net: Add ChatReducerTriggerEvent with AfterToolCallResponseReceived support#13511
Copilot wants to merge 5 commits intomainfrom
copilot/add-chat-reducer-trigger-event

Conversation

Copy link
Contributor

Copilot AI commented Feb 5, 2026

Motivation and Context

Long-running agentic workflows can invoke 50+ tool calls in a single turn, exhausting model token limits before completion. Existing chat history reducers only trigger before sending messages to the provider, making them unable to trim history during multi-step tool execution chains.

Description

Implements trigger-based chat history reduction with a new AfterToolCallResponseReceived event that fires after each FunctionResultContent is received, enabling incremental history management during tool-heavy agent workflows.

Core Infrastructure:

  • ChatReducerTriggerEvent enum: AfterMessageAdded, BeforeMessagesRetrieval, AfterToolCallResponseReceived
  • IChatHistoryReducerWithTrigger: Extends IChatHistoryReducer with trigger configuration
  • ChatHistoryReducerBase: Abstract base class with trigger logic and defaults to BeforeMessagesRetrieval

Agent Integration:

  • ChatHistoryAgent.ReduceAsync(ChatHistory, ChatReducerTriggerEvent, CancellationToken): Trigger-aware reduction
  • ChatHistoryChannel: Invokes reducer with AfterToolCallResponseReceived when detecting FunctionResultContent

Usage:

// Reducer that trims history after each tool call response
public class ToolCallReducer : ChatHistoryReducerBase
{
    public ToolCallReducer() 
        : base(ChatReducerTriggerEvent.AfterToolCallResponseReceived) { }
    
    public override Task<IEnumerable<ChatMessageContent>?> ReduceAsync(
        IReadOnlyList<ChatMessageContent> history, 
        CancellationToken ct = default)
    {
        // Keep only last 20 messages
        return Task.FromResult<IEnumerable<ChatMessageContent>?>(
            history.Count > 20 ? history.Skip(history.Count - 20).ToList() : null);
    }
}

// Configure agent with trigger-aware reducer
var agent = new ChatCompletionAgent
{
    HistoryReducer = new ToolCallReducer()
};

Backward compatible: Reducers implementing only IChatHistoryReducer continue to work with existing BeforeMessagesRetrieval behavior.

Contribution Checklist

Original prompt

This section details on the original issue you should resolve

<issue_title>.Net: New Feature: Add New ChatReducerTriggerEvent : AfterToolCallResponseReceived</issue_title>
<issue_description>---
name: Add New ChatReducerTriggerEvent : AfterToolCallResponseReceived
about: Add a new ChatReducerTriggerEvent that will fire on each receipt of AIContent type of FunctionResultContent.


During long running agentic workflows, a single turn may call upwards of 50 tool calls depending on the complexity of the Agentic task. These scenarios can exceed token limits of the model provider. With a managed chat system that is utilizing InMemoryChatHistoryProvider, it would be useful to add a new ChatReducerTriggerEvent so that the chat messages may be trimmed accordingly as needed in these long running agentic use cases.

InMemoryChatHistoryProvider:
Could we add a new trigger for post tool call response received?

Image</issue_description>

<agent_instructions>Investigate this issue and come up with a plan to add this new feature.</agent_instructions>

Comments on the Issue (you are @copilot in this section)


💬 We'd love your input! Share your thoughts on Copilot coding agent in our 2 minute survey.

Copilot AI and others added 4 commits February 5, 2026 09:25
Co-authored-by: markwallace-microsoft <127216156+markwallace-microsoft@users.noreply.github.com>
Co-authored-by: markwallace-microsoft <127216156+markwallace-microsoft@users.noreply.github.com>
…trailing whitespace

Co-authored-by: markwallace-microsoft <127216156+markwallace-microsoft@users.noreply.github.com>
Co-authored-by: markwallace-microsoft <127216156+markwallace-microsoft@users.noreply.github.com>
Copilot AI changed the title [WIP] Add new ChatReducerTriggerEvent for AfterToolCallResponseReceived .Net: Add ChatReducerTriggerEvent with AfterToolCallResponseReceived support Feb 5, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

.Net: New Feature: Add New ChatReducerTriggerEvent : AfterToolCallResponseReceived

2 participants