In this codelab you build an AI course creator: give it a topic such as "the history of coffee" and it returns a finished course module.
Four agents share the work:
pass or fail with feedback.Each agent is its own service. The orchestrator talks to the other three over the Agent2Agent (A2A) protocol, so each one can be deployed, scaled and replaced on its own.
LoopAgent and SequentialAgentRemoteA2aAgentOpen Cloud Shell and point it at your project:
gcloud config set project YOUR_PROJECT_ID
gcloud auth list
gcloud auth list should show your Gmail account as active.
Enable the APIs the lab uses:
gcloud services enable \
run.googleapis.com \
artifactregistry.googleapis.com \
cloudbuild.googleapis.com \
aiplatform.googleapis.com \
compute.googleapis.com \
cloudtrace.googleapis.com \
logging.googleapis.com
The agents send traces to Cloud Trace, so leaving that API off fills the logs with PERMISSION_DENIED errors.
Clone the repository and move into the starter:
git clone https://github.com/xbill9/multi-agent-loop.git
cd multi-agent-loop/codelab-starter
Install the dependencies for all four agents and the web app:
python3 -m pip install -r requirements.txt
Cloud Shell installs the packages into ~/.local, on its Python 3.12. pip then lists conflicts with tools Cloud Shell has preinstalled, such as google-agents-cli and litellm; the lab uses none of them, so you can ignore those lines. Check the result:
adk --version
It should print adk, version 2.15.0 or newer.
Create the environment file and load it:
cp .env.example .env
sed -i "s/^GOOGLE_CLOUD_PROJECT=.*/GOOGLE_CLOUD_PROJECT=\"$(gcloud config get-value project)\"/" .env
set -a; source .env; set +a
The starter has one folder per agent under agents/, plus the web app in app/. Each agent folder has an agent.py with TODO comments that you fill in over the next steps.
The researcher is a single ADK Agent: a model, a description that other agents read, and an instruction.
Open agents/researcher/agent.py and replace the TODO with:
# --- Researcher Agent ---
researcher = Agent(
name="researcher",
model=MODEL,
description="Gathers information on a topic using Google Search.",
instruction="""
You are an expert researcher. Your goal is to find comprehensive and accurate information on the user's topic.
Summarize your findings clearly.
If you receive feedback that your research is insufficient, use the feedback to refine your next search.
DO NOT output any function calls. Provide your research directly as text.
""",
)
root_agent = researcher
root_agent is the name ADK looks for when it loads the folder as an agent.
The judge returns structured feedback instead of free text. A Pydantic model defines the shape, and output_schema makes the agent answer in that shape.
Open agents/judge/agent.py and replace both TODOs with:
# --- Data Models ---
class JudgeFeedback(BaseModel):
"""Structured feedback from the Judge agent."""
status: Literal["pass", "fail"] = Field(
description="Whether the research is sufficient ('pass') or needs more work ('fail')."
)
feedback: str = Field(
description="Detailed feedback on what is missing or needs clarification if status is 'fail'. If 'pass', a brief confirmation."
)
# --- Judge Agent ---
judge = Agent(
name="judge",
model=MODEL,
description="Evaluates research findings for completeness and accuracy.",
instruction="""
You are a strict editor and fact-checker.
Evaluate the 'research_findings' against the user's original request.
Determine if the findings are sufficient to create a high-quality course.
If they are good enough, output status='pass'.
If they are missing key information, are too vague, or likely inaccurate, output status='fail' and provide specific, constructive 'feedback' on what to research next.
""",
output_schema=JudgeFeedback,
# Disallow transfers as it uses output_schema
disallow_transfer_to_parent=True,
disallow_transfer_to_peers=True,
)
root_agent = judge
adk run loads an agent folder and sends it one message, so you can check each agent before wiring them together.
Ask the researcher a question:
adk run agents/researcher "Find the population of Tokyo in 2020"
The researcher answers with figures from the 2020 census.
Give the judge some thin research:
adk run agents/judge "Topic: Tokyo. Findings: Tokyo is a city."
The judge returns JSON with "status": "fail" and a list of what is missing:
{"status": "fail", "feedback": "The findings are extremely insufficient for creating a high-quality course. ..."}
The content builder writes the course from the approved research. The headings matter: the web app turns the ## sections into a table of contents.
Open agents/content_builder/agent.py and replace the TODO with:
# --- Content Builder Agent ---
content_builder = Agent(
name="content_builder",
model=MODEL,
description="Transforms research findings into a structured course.",
instruction="""
You are an expert course creator.
Take the approved 'research_findings' and transform them into a well-structured, engaging course module.
**Formatting Rules:**
1. Start with a main title using a single `#` (H1).
2. Use `##` (H2) for main section headings. These will be used for the Table of Contents.
3. Use bullet points and clear paragraphs.
4. Maintain a professional but engaging tone.
Ensure the content directly addresses the user's original request.
""",
)
root_agent = content_builder
The orchestrator runs in its own process and reaches the other three agents over A2A. RemoteA2aAgent reads an agent's agent card, a JSON file that describes the agent and where to send messages, and then behaves like a local agent.
Each agent serves its card at /a2a/agent/.well-known/agent-card.json. The URLs come from environment variables, so the same code runs locally and on Cloud Run.
Open agents/orchestrator/agent.py and replace the remote-agents TODO with:
# Connect to the Researcher (Localhost port 8001)
researcher_url = os.environ.get(
"RESEARCHER_AGENT_CARD_URL",
"http://localhost:8001/.well-known/agent.json"
)
researcher = RemoteA2aAgent(
name="researcher",
agent_card=researcher_url,
description="Gathers information using Google Search.",
# IMPORTANT: Save the output to state for the Judge to see
after_agent_callback=create_save_output_callback("research_findings"),
# IMPORTANT: httpx client with Id Token Authentication
httpx_client=create_authenticated_client(researcher_url)
)
# Connect to the Judge (Localhost port 8002)
judge_url = os.environ.get(
"JUDGE_AGENT_CARD_URL",
"http://localhost:8002/.well-known/agent.json"
)
judge = RemoteA2aAgent(
name="judge",
agent_card=judge_url,
description="Evaluates research.",
after_agent_callback=create_save_output_callback("judge_feedback"),
# IMPORTANT: httpx client with Id Token Authentication
httpx_client=create_authenticated_client(judge_url)
)
# Content Builder (Localhost port 8003)
content_builder_url = os.environ.get(
"CONTENT_BUILDER_AGENT_CARD_URL",
"http://localhost:8003/.well-known/agent.json"
)
content_builder = RemoteA2aAgent(
name="content_builder",
agent_card=content_builder_url,
description="Builds the course.",
# IMPORTANT: httpx client with Id Token Authentication
httpx_client=create_authenticated_client(content_builder_url)
)
Two details make the agents work together:
after_agent_callback saves each agent's answer into session state, under research_findings and judge_feedback. The judge reads the research from there, and the next step reads the verdict.create_authenticated_client adds a Google ID token to each request, which Cloud Run needs when the agents only accept authenticated calls.A loop needs a way to stop. ADK ends a LoopAgent when one of its agents sends an event with escalate=True. The escalation checker is a small custom agent that reads the judge's verdict from state and escalates on a pass.
Replace the EscalationChecker TODO with:
class EscalationChecker(BaseAgent):
"""Checks the judge's feedback and escalates (breaks the loop) if it passed."""
async def _run_async_impl(
self, ctx: InvocationContext
) -> AsyncGenerator[Event, None]:
feedback = ctx.session.state.get("judge_feedback")
# Debug log to see what we got from the remote agent
print(f"[EscalationChecker] Feedback received: {feedback}")
if feedback and isinstance(feedback, dict) and feedback.get("status") == "pass":
yield Event(author=self.name, actions=EventActions(escalate=True))
elif isinstance(feedback, str) and '"status": "pass"' in feedback:
yield Event(author=self.name, actions=EventActions(escalate=True))
else:
yield Event(author=self.name)
escalation_checker = EscalationChecker(name="escalation_checker")
The str branch covers a verdict that arrives as text instead of parsed JSON.
Two workflow agents put the pieces in order. They call no model themselves; they decide which agent runs next.
Replace the last two TODOs with:
research_loop = LoopAgent(
name="research_loop",
description="Iteratively researches and judges until quality standards are met.",
sub_agents=[researcher, judge, escalation_checker],
max_iterations=3,
)
root_agent = SequentialAgent(
name="course_creation_pipeline",
description="A pipeline that researches a topic and then builds a course from it.",
sub_agents=[research_loop, content_builder],
)
LoopAgent runs researcher, judge and escalation checker in turn, at most three times. On a fail, the researcher sees the judge's feedback and searches again.SequentialAgent runs the loop, then the content builder.run_local.sh starts the three agents on ports 8001 to 8003, the orchestrator on 8004 and the web app on 8000:
./run_local.sh
Check that each agent serves its card. In a second Cloud Shell tab:
for p in 8001 8002 8003; do
curl -s http://localhost:$p/a2a/agent/.well-known/agent-card.json | python3 -c "import json,sys; d=json.load(sys.stdin); print(d['name'], d['supportedInterfaces'][0]['url'])"
done
Each line shows an agent name and its A2A address, such as researcher http://localhost:8001/a2a/agent.
Open the app: in Cloud Shell click Web Preview, then Change port, enter 8000 and click Change and Preview. Enter:
Create a course about the history of coffee.
The page shows the researcher, then the judge, then the content builder at work, and ends with the finished course.
Stop everything with Ctrl+C in the first tab.
deploy.sh deploys five Cloud Run services from source: researcher, judge, content-builder, orchestrator and the course-creator web app. The four agent services only accept authenticated calls; the web app is public.
./deploy.sh
On a new project, the first deploy asks to create an Artifact Registry repository for the container images. Answer Y. The five builds take about 15 minutes in total.
The script reads each agent's URL after it deploys and passes the agent card addresses to the orchestrator:
--set-env-vars RESEARCHER_AGENT_CARD_URL=$RESEARCHER_URL/a2a/agent/.well-known/agent-card.json
When it finishes, open the course-creator URL and try another topic.
Delete the services so they stop using your credits:
for s in course-creator orchestrator researcher judge content-builder; do
gcloud run services delete $s --region us-central1 --quiet
done
Use the region you deployed to if it differs.
codelab-complete/.shared/adk_app.py mounts them at /a2a/agent, so the card addresses stay the same locally and on Cloud Run.supportedInterfaces instead of a single url, and shared/a2a_utils.py rewrites both forms.adk run takes a message as an argument, so you can test an agent in one command.