Skip to content

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

@srv-smn

Description

@srv-smn

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

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions