Supervisor and Workers
Last updated
Last updated
The Supervisor Worker pattern is a powerful workflow design where a supervisor agent coordinates multiple specialized worker agents to complete complex tasks. This pattern allows for better task delegation, specialized expertise, and iterative refinement of solutions.
In this tutorial, we'll build a collaborative system with:
Supervisor: An LLM that analyzes tasks and decides which worker should act next
Software Engineer: Specialized in designing and implementing software solutions
Code Reviewer: Focused on reviewing code quality and providing feedback
Final Answer Generator: Compiles the collaborative work into a comprehensive solution
The flow begins with a Start node that captures user input and initializes the workflow state.
Add a Start node to your canvas
Configure the Input Type as "Chat Input"
Set up Flow State with these initial variables:
next
: To keep track of the next agent
instruction
: Instruction for the next agent on what to do
The Supervisor is the orchestrator that decides which worker should handle each part of the task.
Connect a LLM node after the Start node
Label it "Supervisor"
Configure the system message, for example:
Set up JSON Structured Output with these fields:
next
: Enum with values "FINISH, SOFTWARE, REVIEWER"
instructions
: The specific instructions of the sub-task the next worker should accomplish
reasoning
: The reason why next worker is tasked to do the job
Configure Update Flow State to store:
next
: {{ output.next }}
instruction
: {{ output.instructions }}
Set the Input Message to: "Given the conversation above, who should act next? Or should we FINISH? Select one of: SOFTWARE, REVIEWER." The Input Message will be inserted at the end, as if the user is asking the supervisor to assign the next agent.
The Check next worker condition node routes the flow based on the supervisor's decision.
Add a Condition node after the Supervisor
Set up two conditions:
Condition 0: {{ $flow.state.next }}
equals "SOFTWARE"
Condition 1: {{ $flow.state.next }}
equals "REVIEWER"
The "Else" branch (Condition 2) will handle the "FINISH" case
This creates three output paths: one for each worker and one for completion.
The Software Engineer specializes in designing and implementing software solutions.
Connect an Agent node to Condition 0 output
Configure the system message:
Set Input Message to: {{ $flow.state.instruction }}
. The Input Message will be inserted at the end, as if the user is giving an instruction to the Software Engineer Agent.
The Code Reviewer focuses on quality assurance and code review.
Connect an Agent node to Condition 1 output
Configure the system message:
Set Input Message to: {{ $flow.state.instruction }}
. The Input Message will be inserted at the end, as if the user is giving an instruction to the Code Reviewer Agent.
Both worker agents need to loop back to the Supervisor for continued coordination.
Add a Loop node after the Software Engineer
Set Loop Back To as "Supervisor"
Set Max Loop Count to 5
Add another Loop node after the Code Reviewer
Set Loop Back To as "Supervisor"
Set Max Loop Count to 5
These loops enable iterative collaboration between the agents.
The final agent compiles all the collaborative work into a comprehensive solution.
Connect an Agent node to Condition 2 output (the "Else" branch)
It is recommended to use a higher context size LLM like Gemini, due to the back-and-forth nature of the conversation, which consumes a large number of tokens.
Set Input Message. This is important because Input Message will be inserted at the end, as if the user is giving an instruction to the Final Answer Generator to look at all the conversations, and generate a final response.
The Supervisor Worker pattern enables several key benefits:
Intelligent Task Delegation: The supervisor uses context and reasoning to assign the most appropriate worker for each sub-task.
Iterative Refinement: Workers can build upon each other's output, with the software engineer implementing features and the code reviewer providing feedback for improvements.
Stateful Coordination: The flow maintains state across iterations, allowing the supervisor to make informed decisions about what should happen next.
Specialized Expertise: Each agent has a focused role and specialized prompt, leading to higher quality outputs in their domain.
Here's how a typical interaction might flow:
User: "Create a React component for user authentication with form validation"
Supervisor: Decides SOFTWARE should act first to implement the component
Software Engineer: Creates a React authentication component with validation logic
Supervisor: Decides REVIEWER should examine the implementation
Code Reviewer: Reviews the code and suggests improvements for security and UX
Supervisor: Decides SOFTWARE should implement the suggested improvements
Software Engineer: Updates the component based on feedback
Supervisor: Determines the task is complete and routes to FINISH
Final Answer Generator: Compiles the complete solution with implementation and review feedback
This architecture consumes a lot of tokens due to the back and forth communications between agents, hence it is not suitable for every cases. It is particularly effective for:
Software development tasks requiring both implementation and review
Complex problem-solving that benefits from multiple perspectives
Workflows where quality and iteration are important
Tasks that require coordination between different types of expertise
Ensure each agent has a well-defined, specific role. Avoid overlapping responsibilities that could lead to confusion or redundant work.
Establish standard formats for how agents communicate their progress, findings, and recommendations. This helps the supervisor make better routing decisions.
Use memory settings appropriately to maintain conversation context while avoiding token limit issues. Consider using memory optimization settings like "Conversation Summary Buffer" for longer workflows.