Sequential agents¶
The SequentialAgent
¶
The SequentialAgent
is a workflow agent that executes its sub-agents in the order they are specified in the list.
Use the SequentialAgent
when you want the execution to occur in a fixed, strict order.
Example¶
- You want to build an agent that can summarize any webpage, using two tools:
get_page_contents
andsummarize_page
. Because the agent must always callget_page_contents
before callingsummarize_page
(you can't summarize from nothing!), you should build your agent using aSequentialAgent
.
As with other workflow agents, the SequentialAgent
is not powered by an LLM, and is thus deterministic in how it executes. That being said, workflow agents are only concerned only with their execution (i.e. in sequence), and not their internal logic; the tools or sub-agents of a workflow agent may or may not utilize LLMs.
How it works¶
When the SequentialAgent
's run_async()
method is called, it performs the following actions:
- Iteration: It iterates through the
sub_agents
list in the order they were provided. - Sub-Agent Execution: For each sub-agent in the list, it calls the sub-agent's
run_async()
method.
Full Example: Code Development Pipeline¶
Consider a simplified code development pipeline:
- Code Writer Agent: An
LlmAgent
that generates initial code based on a specification. - Code Reviewer Agent: An
LlmAgent
that reviews the generated code for errors, style issues, and adherence to best practices. It receives the output of the Code Writer Agent. - Code Refactorer Agent: An
LlmAgent
that takes the reviewed code (and the reviewer's comments) and refactors it to improve quality and address issues.
A SequentialAgent
is perfect for this:
This ensures the code is written, then reviewed, and finally refactored, in a strict, dependable order. The output from each sub-agent is passed to the next by storing them in state via output_key
.
Code
from google.adk.agents.sequential_agent import SequentialAgent
from google.adk.agents.llm_agent import LlmAgent
from google.genai import types
from google.adk.sessions import InMemorySessionService
from google.adk.runners import Runner
# --- Constants ---
APP_NAME = "code_pipeline_app"
USER_ID = "dev_user_01"
SESSION_ID = "pipeline_session_01"
GEMINI_MODEL = "gemini-2.0-flash"
# --- 1. Define Sub-Agents for Each Pipeline Stage ---
# Code Writer Agent
# Takes the initial specification (from user query) and writes code.
code_writer_agent = LlmAgent(
name="CodeWriterAgent",
model=GEMINI_MODEL,
instruction="""You are a Code Writer AI.
Based on the user's request, write the initial Python code.
Output *only* the raw code block.
""",
description="Writes initial code based on a specification.",
# Stores its output (the generated code) into the session state
# under the key 'generated_code'.
output_key="generated_code"
)
# Code Reviewer Agent
# Takes the code generated by the previous agent (read from state) and provides feedback.
code_reviewer_agent = LlmAgent(
name="CodeReviewerAgent",
model=GEMINI_MODEL,
instruction="""You are a Code Reviewer AI.
Review the Python code provided in the session state under the key 'generated_code'.
Provide constructive feedback on potential errors, style issues, or improvements.
Focus on clarity and correctness.
Output only the review comments.
""",
description="Reviews code and provides feedback.",
# Stores its output (the review comments) into the session state
# under the key 'review_comments'.
output_key="review_comments"
)
# Code Refactorer Agent
# Takes the original code and the review comments (read from state) and refactors the code.
code_refactorer_agent = LlmAgent(
name="CodeRefactorerAgent",
model=GEMINI_MODEL,
instruction="""You are a Code Refactorer AI.
Take the original Python code provided in the session state key 'generated_code'
and the review comments found in the session state key 'review_comments'.
Refactor the original code to address the feedback and improve its quality.
Output *only* the final, refactored code block.
""",
description="Refactors code based on review comments.",
# Stores its output (the refactored code) into the session state
# under the key 'refactored_code'.
output_key="refactored_code"
)
# --- 2. Create the SequentialAgent ---
# This agent orchestrates the pipeline by running the sub_agents in order.
code_pipeline_agent = SequentialAgent(
name="CodePipelineAgent",
sub_agents=[code_writer_agent, code_reviewer_agent, code_refactorer_agent]
# The agents will run in the order provided: Writer -> Reviewer -> Refactorer
)
# Session and Runner
session_service = InMemorySessionService()
session = session_service.create_session(app_name=APP_NAME, user_id=USER_ID, session_id=SESSION_ID)
runner = Runner(agent=code_pipeline_agent, app_name=APP_NAME, session_service=session_service)
# Agent Interaction
def call_agent(query):
content = types.Content(role='user', parts=[types.Part(text=query)])
events = runner.run(user_id=USER_ID, session_id=SESSION_ID, new_message=content)
for event in events:
if event.is_final_response():
final_response = event.content.parts[0].text
print("Agent Response: ", final_response)
call_agent("perform math addition")