Skip to content

Python: Sequential workflow: Add context_mode option to control what context is passed between agents #3666

@moonbox3

Description

@moonbox3

Summary

Add a configuration option to SequentialBuilder that allows users to control how context is passed between participants in a sequential workflow.

Problem Statement

The current sequential workflow always passes the full conversation history to each subsequent agent. While this is appropriate for many scenarios, some users have deterministic sequential flows where each agent only needs the final output from the previous agent as input—not the entire conversation context.

Passing the full context when it is not needed can:

  • Increase token usage unnecessarily
  • Cause downstream agents to be influenced by earlier conversation that is not relevant to their task
  • Require users to implement custom executors just to filter the context

Current Behavior

From _sequential.py:

"A shared conversation context (list[ChatMessage]) is passed along the chain. Agents append their assistant messages to the context."

The agent response adapter uses full_conversation which preserves the entire history.

Proposed Solution

Add a context_mode parameter to SequentialBuilder with the following options:

Mode Description
"full" (default) Pass the entire conversation history to each agent (current behavior)
"last_output" Pass only the last agent output as input to the next agent

Example API

from agent_framework import SequentialBuilder, ContextMode

# Current behavior (explicit)
workflow = SequentialBuilder().participants([agent1, agent2]).context_mode("full").build()

# New behavior - only pass last output
workflow = SequentialBuilder().participants([agent1, agent2]).context_mode("last_output").build()

Implementation Considerations

  • The _ResponseToConversation adapter (or equivalent logic) would need to respect the context mode when converting AgentExecutorResponse to list[ChatMessage]
  • For "last_output" mode, extract only the last assistant message and wrap it as user input for the next agent
  • Consider whether custom executors should also respect this mode or if it only applies to agent participants
  • Consider if additional modes might be useful (e.g., "last_n", "summary")

Workaround

Users can currently achieve this by inserting a custom executor between agents:

class LastOutputOnly(Executor):
    @handler
    async def extract_last(self, messages: list[ChatMessage], ctx: WorkflowContext[list[ChatMessage]]) -> None:
        last_assistant = next((m for m in reversed(messages) if m.role == "assistant"), None)
        if last_assistant:
            await ctx.send_message([ChatMessage(role="user", text=last_assistant.text)])
        else:
            await ctx.send_message(messages)

Metadata

Metadata

Assignees

Labels

pythonworkflowsRelated to Workflows in agent-framework

Projects

Status

No status

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions