Agent Development Kit (ADK) Crash Course Summary
Key Concepts:
- Agent Development Kit (ADK): Google's framework for building AI agents.
- Agents: Independent entities with specific instructions, models, and tools.
- Tools: Functions or external resources that agents can use to perform tasks.
- Models: Large Language Models (LLMs) like Gemini, OpenAI, or Claude.
- Sessions: Stateful message histories between users and agents, including state and events.
- State: A dictionary-like storage for information accessible to agents within a session.
- Runners: Components that manage agents and sessions to generate responses.
- Workflows: Structured patterns for agent interactions (sequential, parallel, loop).
- Callbacks: Functions triggered at specific points in the agent lifecycle (before/after agent, model, tool).
- Light LLM: A library that handles the complexities of working with different models.
- Open Router: A tool that allows users to purchase tokens that can be used for any model.
1. Building Your First Agent
- Core Attributes:
- Name: Must match the agent's folder name.
- Model: Specifies the LLM (e.g., Gemini 2.0 No Flash).
- Description: A high-level job overview for multi-agent delegation.
- Instructions: Directives for the agent's behavior.
- Folder Structure:
init.py: Tells Python to import the agent..env: Stores environment variables (API keys). Only needed in the root agent.agent.py: Contains the agent's definition.
- Dependencies:
- Use
requirements.txtto install necessary packages (e.g.,google-generative-ai-python). - Create a virtual environment using
python -m venv .venv. - Activate the environment (e.g.,
source .venv/bin/activateon macOS/Linux). - Install dependencies using
pip install -r requirements.txt.
- Use
- API Key:
- Create a Google Cloud account and project.
- Enable the Gemini API.
- Generate an API key and store it in the
.envfile.
- Running the Agent:
- Navigate to the agent's folder in the terminal.
- Use
adk webto start a web server for interacting with the agent.
2. Adding Tools to Agents
- Types of Tools:
- Function Calling Tools: Custom Python functions.
- Built-in Tools: Google-provided tools (e.g., Google Search, Code Execution, Vertex AI Search). Only work with Gemini models.
- Third-Party Tools: Tools from libraries like Langchain or CrewAI.
- Adding Tools:
- Create a Python function with a docstring describing its purpose.
- Specify the return type.
- Add the function to the
toolslist in the agent definition.
- Best Practices:
- Make tool return statements as specific and instructional as possible.
- Return results as dictionaries with descriptive keys.
- Avoid default values in function parameters.
- Limitations:
- Cannot combine built-in tools with custom tools in the same agent.
- Example: Using Google Search to fetch news about Tesla.
3. Connecting to Other Models (OpenAI, Claude)
- Technologies:
- Light LLM: A library that handles the complexities of working with different models.
- Open Router: A tool that allows users to purchase tokens that can be used for any model.
- Configuration:
- Import Light LLM from
google_agent_development_kit. - Define the model using
light_llm.Model(provider="openrouter", model_family="openai", model_name="gpt-4-0125-preview"). - Set the
OPENROUTER_API_KEYenvironment variable.
- Import Light LLM from
- Example: Creating a dad joke agent using OpenAI's GPT-4 via Open Router.
4. Structured Outputs
- Options:
- Input Schema: Defines the expected input format (not recommended).
- Output Schema: Defines the desired output format as a Pydantic class.
- Output Key: Specifies where to store the output in state.
- Output Schema:
- Define a Pydantic base model with the desired fields and types.
- Include a description for each field.
- Specify the output schema in the agent definition.
- Cannot use output schema when using tools or transferring information to other agents.
- Instructions:
- Clearly define the desired JSON structure in the agent's instructions.
- Example: Creating an email generation agent with a subject and body.
5. Sessions, State, and Runners
- Sessions:
- State: A dictionary for storing information.
- Events: A message history between the user and the agent.
- IDs, app names, user IDs, and last update times.
- Session Types:
- In-memory: Data is lost when the application closes.
- Database: Data is stored in a local database.
- Vertex AI: Data is stored in Google Cloud.
- Runners:
- Combine agents and sessions to generate responses.
- Handle the lifecycle of requests, including tool calls and LLM interactions.
- Example: Creating a frequently asked question agent that accesses user preferences from state.
6. Saving Sessions to a Database
- Database Session Service:
- Use
google_agent_development_kit.providers.memory.DatabaseSessionServiceto store sessions in a SQLite database. - Specify the database file path.
- Use
- Session Management:
- Use
list_sessionsto retrieve existing sessions for a user. - Create a new session if one doesn't exist.
- Use
- Interactive Conversation Loop:
- Capture user input and send it to the runner.
- Process the agent's response and update the session.
- Example: Creating a reminder agent that saves reminders to a database.
7. Multi-Agent Systems
- Delegation:
- The root agent delegates tasks to sub-agents based on their descriptions.
- The sub-agent handles the task and generates the response.
- Limitations:
- Cannot use built-in tools in sub-agents directly.
- Workaround: Wrap the sub-agent as a tool using
agent_as_tool.
- Agent as Tool:
- The root agent calls the sub-agent as a tool.
- The sub-agent performs the task and returns the result to the root agent.
- The root agent uses the result to generate the final response.
- Example: Creating a manager agent that delegates tasks to a stock analysis agent, a funny nerd agent, and a news analyst agent.
8. Multi-Agent Systems with Shared State
- State Sharing:
- Agents can access and modify state to share information.
- The behavior of agents can change based on the state.
- Example: Creating a customer service agent with sub-agents for policy, sales, course support, and order management.
- The sales agent updates state when a user purchases a course.
- The course support agent answers questions based on the user's purchased courses.
- The order agent processes refunds and updates state accordingly.
9. Callbacks
- Types of Callbacks:
- Before Agent: Triggered before the agent starts processing.
- After Agent: Triggered after the agent finishes processing.
- Before Model: Triggered before sending a request to the LLM.
- After Model: Triggered after receiving a response from the LLM.
- Before Tool: Triggered before calling a tool.
- After Tool: Triggered after a tool returns a result.
- Callback Context:
- Provides access to state and other necessary information.
- Before Agent:
- Use for setting up resources and hydrating state.
- After Agent:
- Use for post-execution validation and logging.
- Before Model:
- Use for adding dynamic instructions or guardrails.
- After Model:
- Use for reformatting the response or censoring information.
- Before Tool:
- Use for inspecting and modifying tool arguments or performing authorization.
- After Tool:
- Use for inspecting, modifying, and logging tool results.
- Example:
- Filtering inappropriate content using the before model callback.
- Modifying tool responses using the after tool callback.
10. Sequential Workflows
- Sequential Agents:
- Agents are triggered one after another in a specified order.
- Execution occurs from first to last.
- Use Cases:
- When tasks must be performed in a specific sequence.
- Example: Creating a lead qualification pipeline with a validator agent, a score agent, and a recommendation agent.
11. Parallel Workflows
- Parallel Agents:
- Agents are triggered in parallel to speed up processing.
- All agents generate and do work all in parallel.
- Use Cases:
- When speed is a priority and tasks can be performed independently.
- Example: Creating a system information gatherer with agents for CPU, memory, and disk information.
12. Loop Agents
- Loop Agents:
- Agents iterate on a problem until a condition is met or a maximum number of iterations is reached.
- Exit Conditions:
- Max iterations.
- A specific condition is met.
- Use Cases:
- When agents need to refine a solution over multiple iterations.
- Example: Creating a LinkedIn post generator with agents for reviewing and refining the post.
Synthesis/Conclusion
The ADK crash course provides a comprehensive guide to building AI agents using Google's Agent Development Kit. It covers fundamental concepts like agent creation, tool integration, model selection, and state management. It also explores advanced topics like multi-agent systems, workflows, and callbacks, enabling developers to create sophisticated and adaptable AI solutions. The course emphasizes practical examples and best practices, empowering developers to build AI agents that can automate tasks, solve complex problems, and interact intelligently with users.
AI summaries can miss context or contain errors. Check important details against the original video.





