-
Notifications
You must be signed in to change notification settings - Fork 1.4k
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:
- System messages (instructions and
ContextProviderstate). - Its own previous interactions (assistant messages, its own tool calls, and its own tool results).
- 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
ChatMiddlewareto intercept and filtercontext.messages. This is boilerplate-heavy and requires a deep understanding of the internal_compactionmetadata. - Separated Sessions: Managing separated sessions instead of a single orchestrated workflow breaks the ease of use provided by
HandoffBuilderandGroupChatBuilder.
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