Lab 3 Solution: Build and Run the "Echo" Agent
Goal
Overview
Build your first AI agent with the Google Agent Development Kit (ADK). In this lab, you'll create a "Parrot" agent. Its only purpose is to take any text you provide and repeat it back to you exactly, without answering questions or adding any commentary.
Expected Behavior
- User: "What time is it?" -> Agent: "What time is it?"
- User: "Help me with my homework." -> Agent: "Help me with my homework."
Prerequisites
- You have successfully completed the setup in Module 2.
- Your virtual environment is active.
- You have authenticated with the gcloud CLI.
- A Google API key from Google AI Studio or a configured Google Cloud project.
Core Concepts
- Agent: An AI assistant powered by a Large Language Model (LLM). It's a blueprint defining the agent's purpose (instructions), its model (e.g., Gemini), and its capabilities.
adkCLI: A command-line tool for creating and managing ADK projects.- Dev UI: An interactive web interface for testing and debugging your agents.
Step 1: Create the Agent Project
We will use the adk command-line tool to create the file structure for our new agent.
-
Navigate to your training directory:
cd /path/to/your/adk-training -
Create the agent project: Run the following command to create a new agent named
echo_agent. This defaults to the Python-based approach.adk create echo_agentThis command creates a new directory named
echo_agent/with anagent.pyfile and a.envfile inside.
Step 2: Configure the Agent
Now, let's tell the agent how to behave and provide it with the necessary credentials.
-
Set up your API key: Open the
.envfile. This file stores secret information like API keys.- For Google AI Studio API Key:
You can get a free API key from the Google AI Studio.
GOOGLE_API_KEY=<your-google-gemini-api-key> - For Google Cloud Vertex AI:
GOOGLE_GENAI_USE_VERTEXAI=1
GOOGLE_CLOUD_PROJECT=<your_gcp_project>
GOOGLE_CLOUD_LOCATION=us-central1
- For Google AI Studio API Key:
You can get a free API key from the Google AI Studio.
-
Define the agent's behavior (Python method): Open
agent.pyand replace its contents with this:from google.adk.agents import LlmAgent
# IMPORTANT: The ADK requires this main agent variable
# to be named exactly `root_agent`.
root_agent = LlmAgent(
name="echo_agent",
model="gemini-2.5-flash",
description="A parrot agent that only repeats user input.",
instruction="You are a parrot. Your ONLY job is to repeat the user's input back to them exactly as they wrote it. DO NOT answer questions. DO NOT provide help. DO NOT add extra words. If the user asks a question, repeat the question back to them."
)
Alternative: Defining the Agent in YAML
Instead of Python, you can define your agent in a YAML file. This is simpler for basic agents but less flexible.
-
Create the agent with the
--type=configflag:adk create --type=config echo_agentThis creates a
root_agent.yamlfile instead ofagent.py. -
Define the agent's behavior in
root_agent.yaml: Openroot_agent.yamland replace its contents with this:# The first line is an optional schema definition that provides
# auto-completion and validation in compatible code editors.
# yaml-language-server: $schema=https://raw.githubusercontent.com/google/adk-python/refs/heads/main/src/google/adk/agents/config_schemas/AgentConfig.json
name: echo_agent
model: gemini-2.5-flash
description: An agent that repeats the user's input.
instruction: You are an echo agent. Your only job is to repeat the user's input back to them exactly as they wrote it. Do not add any extra words or explanations.Note: If both
agent.pyandroot_agent.yamlexist, the ADK will use theroot_agent.yamlfile.
Step 3: Run the Agent
-
Start the ADK web server: From the parent
adk-trainingdirectory, run:adk webYou should see output indicating a server has started on
http://127.0.0.1:8080. -
Interact with your agent:
- Open the URL in your web browser.
- In the chat interface, type "Hello, world!".
- The agent should respond with the exact same message: "Hello, world!".
Understanding What's Happening
When you send a message:
- ADK packages your message with the agent's instructions.
- It sends the package to the Gemini LLM.
- Gemini generates a response based on the instructions.
- ADK returns the response to you.
Use the Events tab in the Dev UI to see this flow in detail!
Key Takeaways
✅ adk create is the starting point for all agents.
✅ Agent behavior is defined by its instruction.
✅ .env keeps your API keys safe and out of code.
✅ You can define agents in Python (flexible) or YAML (simple).
✅ adk web is your primary tool for testing and debugging.
Common Issues & Solutions
- Problem: "Agent not found" or errors on startup.
- Solution: Make sure you are running
adk webfrom the parent directory ofecho_agent.
- Solution: Make sure you are running
- Problem: "Authentication error".
- Solution: Double-check that your
.envfile is correctly configured with your API key or GCP project.
- Solution: Double-check that your
- Problem: If using
agent.py, "root_agent not found".- Solution: Ensure your agent variable is named exactly
root_agent.
- Solution: Ensure your agent variable is named exactly
Lab Summary
Fantastic! You have successfully built and interacted with your first AI agent. You have learned the core development loop: Create, Configure, and Run.
In the next modules, you will learn how to give your agents more sophisticated instructions and powerful new capabilities.