Skip to main content

Welcome to a Grasp lesson

Create a free account to keep studying and create your own study path.

Lesson 26

Conditional Logic in Google ADK Workflows

Recommended resources

Hello! In our last lesson, we gave your agent a crucial upgrade: memory. You learned to manage both short-term conversational context with session.state and long-term knowledge with MemoryService. Your agent can now remember details within and across conversations, making it far more capable.

However, we've still been working with a single, monolithic agent. As tasks become more complex, this approach can lead to convoluted prompts and logic that's difficult to debug and maintain. Today, we'll move beyond a single agent and learn how to build sophisticated, multi-step workflows.

This lesson directly addresses the learning outcome: Implement a multi-step workflow with conditional logic in a Google ADK agent. You'll learn how to break down a complex problem into a sequence of tasks, assign each task to a specialized sub-agent, and orchestrate their collaboration using ADK's powerful workflow primitives.

1. From Monolith to Orchestra: The Need for Workflow Agents

Imagine building an agent to create a movie pitch. This involves several distinct steps: researching a historical figure, writing a plot outline based on that research, getting critical feedback on the plot, iteratively improving it, and finally, writing it to a file. Trying to cram all this logic into a single agent's prompt would be a nightmare.

A much cleaner approach, familiar from software engineering principles like microservices, is to create a system of specialized agents: a Researcher, a Screenwriter, a Critic, etc. The challenge then becomes: how do you control the flow of work between them?

This is where Workflow Agents come in. These are special, non-LLM agents in ADK whose sole purpose is to orchestrate the execution of their sub-agents.

Google Agent Development Kit (ADK) Agent Types Overview
This diagram shows the main agent types in ADK. We've worked with `LlmAgent` so far. Today, we focus on the **Workflow Agents** (`SequentialAgent`, `ParallelAgent`, and `LoopAgent`) which act as controllers.

There are three primary types of workflow agents:

  • SequentialAgent: Executes its sub-agents one by one, in a fixed order. Perfect for linear pipelines.
  • LoopAgent: Executes its sub-agents in a sequence and then repeats the cycle. This is essential for iterative tasks and implementing conditional logic to exit the loop.
  • ParallelAgent: Executes all its sub-agents at the same time. Useful for independent tasks that can be run concurrently to save time.

Let's see this in action. The following video demonstrates a common problem: a parent LlmAgent is given three sub-agents but, using its own reasoning, decides to only call one of them. The video then shows how to fix this by replacing the parent LlmAgent with a LoopAgent to enforce a specific workflow.

Getting started with Agent Development Kit

This clip from the 'Getting started with Agent Development Kit' video by Google for Developers clearly illustrates why you need workflow agents for deterministic control over your multi-agent system.

Watch from 00:06:01 to 00:13:22. Focus on these key moments: The Problem (08:20): Notice how the initial agent only runs the script_writer sub-agent, ignoring the visualizer and formatter. The Solution (09:40): The video introduces workflow agents and refactors the parent agent into a LoopAgent. The Result (12:20): Observe the logs in the UI, which now show all three sub-agents (script_writer, visualizer, formatter) being called in an iterative loop.


2. Building Workflows: SequentialAgent and LoopAgent

Now that you understand the "why," let's get hands-on with the "how." The best way to learn this is by building the movie-pitching agent we discussed earlier.

We will follow a Google Codelab that walks through this exact process. It's a fantastic, practical guide that directly implements the concepts for today's lesson.

Build Multi-Agent Systems with ADK

The 'Build Multi-Agent Systems with ADK' Codelab is a step-by-step tutorial for creating a complex workflow. We will focus on the sections covering sequential and loop-based execution.

This is a hands-on lab, but you don't need to run the code unless you want to. The key is to read the code snippets and explanations carefully. Read Section 10: 'Build a multi-agent system with a SequentialAgent': Understand how the film_concept_team agent uses SequentialAgent to run a linear pipeline: researcher → screenwriter → file_writer. Pay attention to how they use session state to pass information (e.g., the researcher's findings) from one step to the next. Read Section 11: 'Add a LoopAgent for iterative work': This is the most important part. It introduces a critic agent and refactors the workflow to use a LoopAgent to create a writers_room. The key insight here is how the critic agent uses the built-in exit_loop tool. This is the conditional logic that stops the iterative refinement process once the plot is good enough.

This Codelab demonstrates the two fundamental patterns for building multi-step workflows:

  • Sequential Pipeline: For tasks that must happen in a strict order.
  • Iterative Refinement: For tasks that benefit from a generate-and-critique cycle until a condition is met.

The critic agent in the Codelab is the heart of the conditional logic. By giving it instructions to evaluate the PLOT_OUTLINE and call exit_loop when it's satisfied, you create an explicit, rule-based condition for ending the workflow.


3. Types of Conditional Logic: Implicit vs. Explicit

The LoopAgent with an exit_loop tool is a form of explicit conditional logic. You, the developer, define a clear, deterministic rule for the workflow's branching or termination.

However, there is another powerful form of conditional logic in multi-agent systems: implicit conditional logic, which is driven by an LLM's reasoning. In this pattern, a parent LlmAgent acts as a "coordinator" or "dispatcher," deciding which specialized sub-agent to call based on the user's request and the descriptions of its sub-agents.

This next video demonstrates this "coordinator" pattern beautifully. An "inspiration agent" has two sub-agents: a "news agent" and a "places agent." Watch how it intelligently delegates tasks to the correct specialist based on the user's query.

How to Build Multi AI Agents with Google Agent Development Kit (ADK) For Beginners

In this video from 'AI with Vlad', you'll see a great example of an LLM-based coordinator agent that implements conditional logic by choosing the right sub-agent for the job.

Watch from 00:10:01 to 00:13:48. Observe the interaction: The user asks, "I want to go to Rome next week based on the current events." The root agent transfers control to the inspiration_agent, which calls both the news_agent and places_agent to gather initial context. The user then asks, "How about the events?" The inspiration_agent correctly routes this task only to the news_agent. Next, the user asks, "I want to find a restaurant nearby..." The agent correctly routes this task only to the places_agent. This is conditional logic in action, powered by the LLM's understanding of the query and the sub-agents' capabilities.

Choosing between these two approaches involves a classic engineering trade-off:

  • Implicit (LLM-based Delegation): More flexible and "intelligent." It can handle a wider range of user inputs without you pre-defining every possible path. The downside is that it's less predictable and can sometimes make mistakes.
  • Explicit (Workflow Agents): More robust, deterministic, and reliable. You have precise control over the execution flow. The downside is that it's more rigid and requires you to define the workflow paths in advance.

In practice, the most powerful agent systems often combine both. You might use a top-level coordinator LlmAgent to handle initial user interaction and delegate to a SequentialAgent or LoopAgent to execute a well-defined, multi-step sub-task.

Test your understanding!

You are tasked with building a customer support agent that handles returns. The process is:

  1. Ask the user for their order number.
  2. Validate the order number using an internal API tool.
  3. If valid, check if the order is within the 30-day return window.
  4. If it is, process the return; if not, inform the user.

How would you structure this using workflow agents and conditional logic? Describe which agents you would use and where the conditional logic lies.

Show answer

A good approach would be to combine SequentialAgent and LlmAgent for conditional logic.

  1. Root Agent (LlmAgent): This agent interacts with the user to get the order number.
  2. Validation Agent (LlmAgent): This agent takes the order number, calls the validation API tool, and based on its instructions, outputs a simple status like "VALID" or "INVALID". It saves this status to the session state using output_key="validation_status".
  3. Return Logic Agent (LlmAgent): This is where the core conditional logic happens. Its instructions would be something like: "If {validation_status} is VALID, call the check_return_window tool. If the tool returns true, process the return. If it returns false, inform the user their order is outside the return window. If {validation_status} is INVALID, tell the user the order number is not found."

This entire process could be wrapped in a SequentialAgent: RootAgent -> ValidationAgent -> ReturnLogicAgent. The branching logic is handled by the ReturnLogicAgent's LLM reasoning, guided by the state set by the previous agent in the sequence. You could also implement the logic inside a Custom Agent for more programmatic control if needed.


Conclusion

Today you've made a significant leap from building single agents to orchestrating complex, multi-agent systems. You now have the tools to design and implement sophisticated workflows that can solve problems far beyond the reach of a single agent.

Key Takeaways:

  • Workflow Agents are Orchestrators: Use SequentialAgent, LoopAgent, and ParallelAgent to control the flow of work between specialized sub-agents.
  • Sequential for Pipelines: SequentialAgent is ideal for linear processes where the output of one step is the input to the next.
  • Loop for Iteration: LoopAgent enables iterative refinement cycles. You can implement explicit conditional logic by having a sub-agent (like a "critic") call the exit_loop tool when a condition is met.
  • LLMs for Delegation: You can implement implicit conditional logic by using a parent LlmAgent as a coordinator that intelligently delegates tasks to the appropriate sub-agent based on the user's query.
  • Combine Patterns for Power: The most robust systems often combine these patterns, using LLM-based coordinators to route tasks to deterministic workflow agents.

In our next lesson, we will delve into the final module, "Advanced Architectures: Multi-Agent Systems." We'll build upon today's concepts to explore more complex collaboration patterns and discuss the strategic decisions behind choosing a multi-agent architecture over a single-agent one.