Skip to content

Python: Add context_mode to control what gets sent to agents in workflows #3688

Description

@moonbox3

Summary

Investigate adding a context_mode option to control what context gets passed between agents in a workflow.

Background

From a user question:

My scenario involves a deterministic sequential flow with multiple agents, so I'm using the Agent framework's sequential workflow. In my case, I don't need to pass the whole context to each agent in the workflow; I only want each agent to receive the final output from the previous one as input. From what I understand, the default behavior is to pass the entire context. How can I adjust this?

Current Workaround

Today, users need to create a custom executor that transforms the conversation between agents:

class LastOutputOnlyExecutor(Executor):
    @handler
    async def transform(
        self,
        messages: list[ChatMessage],
        ctx: WorkflowContext[list[ChatMessage]]
    ) -> None:
        # Extract only the last assistant message
        last_assistant = [m for m in messages if m.role == "assistant"][-1:]
        await ctx.send_message(last_assistant)

Then build the flow like:

workflow = SequentialBuilder().participants([
    agent1,
    LastOutputOnlyExecutor(id="filter-1"),
    agent2,
    LastOutputOnlyExecutor(id="filter-2"),
    agent3,
]).build()

Proposed Solution

Investigate adding a context_mode parameter to reduce verbosity. Possible modes:

  • full (default) - pass entire conversation context
  • last_output - pass only the last assistant message
  • last_n - pass last N messages
  • Custom filter function

Scope

This feature should be evaluated for all orchestration patterns, not just sequential:

  • Sequential workflows
  • Concurrent workflows
  • Group chat
  • Other orchestration patterns

Activity

  1. changed the title [-]Add context_mode to control what gets sent to agents in workflows[/-] [+]Python: Add context_mode to control what gets sent to agents in workflows[/+] on Feb 5, 2026
  2. moonbox3 commented on Mar 5, 2026

    @moonbox3
    ContributorAuthor

    See #4038 for further ideas, if not represented here.

  3. droideronline commented on Mar 6, 2026

    @droideronline
    Contributor

    Thanks for covering these ideas - really appreciate the thorough exploration of context_mode options. One thing I'd like to highlight again: once this feature is implemented, it will also need to be supported in declarative workflows (e.g., YAML-based definitions). The goal is to allow users to configure context passing behavior — such as full, last_output, or last_n — directly in the workflow definition, without requiring any code changes. This is especially important for scenarios where non-developer users or ops teams need to adjust workflow behavior through configuration alone.

  4. TaoChenOSU commented on Mar 6, 2026

    @TaoChenOSU
    Contributor

    When Agent A invokes Agent B, we also need to consider the messages that are already in Agent B's cache. Currently, we are replacing the cache of Agent B with Agent A's context, either the full conversation or the response: https://github.com/microsoft/agent-framework/blob/main/python/packages/core/agent_framework/_workflows/_agent_executor.py#L150

    I think we should also have a mode to control if an agent should retain its own context when receiving a request from another agent.

  5. TaoChenOSU commented on Mar 6, 2026

    @TaoChenOSU
    Contributor

    Perhaps a mode on the receiving agent executor that says:

    @dataclass
    class ContextMode:
        #  Default to the full context
        incoming_messages_filter: Callable[[list[Mesage]], list[Message]] = lambda messages: messages
        # Default to retaining cache
        retain_cache: bool = True

    Note that if not handled carefully, retaining cache and supplying full context from the previous agent may cause unexpected behavior.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    pythonUsage: [Issues, PRs], Target: PythonworkflowsUsage: [Issues, PRs], Target: Workflows

    Projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions