microsoft / microsoft/agent-framework

Python: [Feature]: Add IsolateAgentCompactionStrategy for multi-agent context isolation

Open
#4,959 5 comments 0 reactions 1 assignee View on GitHub

@eavanvalkenburg is already working on this.

Since Apr 20, 2026.

agents compaction harness python
Dominant language
Python
Stars
13.6k
Forks
2.3k
Avg merge
2d 45m
Merged PRs (30d)
358

Description

Description

What problem does it solve?
When building multi-agent orchestrations using HandoffBuilder or GroupChatBuilder with a shared session/thread, the context window grows rapidly with messages from all participating agents. This includes their internal monologues, tool calls, and tool results.

Currently, this shared context often confuses agents. For example, Agent A might try to answer a user based on a tool result that was actually triggered by Agent B, or the model might get distracted by the sheer volume of another agent's specialized workflow.

While SequentialBuilder has the chain_only_agent_responses=True flag, there is no built-in, declarative way to isolate an agent's context within dynamic meshes like HandoffBuilder without writing custom middleware to manipulate context.messages.

What would the expected behavior be?
I propose adding a new built-in compaction strategy: IsolateAgentCompactionStrategy (or ParticipantIsolationStrategy).

This strategy would sit cleanly in the framework's Compaction layer. When applied to an agent, it filters the shared conversation history so the agent only sees:

  1. System messages (instructions and ContextProvider state).
  2. Its own previous interactions (assistant messages, its own tool calls, and its own tool results).
  3. The user prompts directed at it.

Additionally, it should support a keep_last_turns sliding window (leveraging annotate_message_groups) so the agent only retains a specific number of its own historical turns.

Are there any alternatives you've considered?

  • Custom Middleware: Currently, developers must write bespoke ChatMiddleware to intercept and filter context.messages. This is boilerplate-heavy and requires a deep understanding of the internal _compaction metadata.
  • Separated Sessions: Managing separated sessions instead of a single orchestrated workflow breaks the ease of use provided by HandoffBuilder and GroupChatBuilder.
Code Sample
from agent_framework._compaction import IsolateAgentCompactionStrategy

sales_agent = client.as_agent(
    name="SalesAgent",
    instructions="You handle sales...",
    tools=[get_pricing, add_to_cart],
    # Isolates the context, optionally keeping only the last 3 conversational turns
    compaction_strategy=IsolateAgentCompactionStrategy(agent_name="SalesAgent", keep_last_turns=3)
)
Language/SDK

Python

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.