Getting Started With ADK
Copyright 2025 Google LLC.
ADK Simple Demo: Stateful Echo Agent with Gemini
This notebook provides a basic, introductory example of using Gemini in the Google Agent Development Kit (ADK).
Goal: Demonstrate how ADK orchestrates a simple workflow involving state transitions (START -> PROCESSING -> END) around a core interaction with the Gemini API.
Scenario: You will build a "Stateful Echo Agent". This agent's primary task is to echo the user's input. However, it will use ADK components to manage its internal state throughout the process:
- It starts in a
STARTstate. - Upon receiving input, it uses an ADK Tool to transition to
PROCESSING. - It prepares the echo response (implicitly using the Gemini model configured in the Agent).
- It uses the ADK Tool again to transition to the
ENDstate. - It delivers the final echo response.
This example highlights ADK's role in managing structured workflows and state, even for simple tasks.
|
This notebook was contributed by Anand Roy.LinkedIn - See Anand other notebooks here.Have a cool Gemini example? Feel free to share it too! |
Setup
1. Configure Google API Key
To power the Agent with Gemini, access to the Google Generative AI API is required. The next code cell configures your API key.
Important: This example uses Colab Secrets (userdata.get('GOOGLE_API_KEY')). Make sure you have stored your key named GOOGLE_API_KEY in the Colab Secrets manager (View -> Secrets).
False
2. Core ADK Components in this Demo
This example uses the following key ADK components:
Agent: The agent powered by the Gemini model. It understands instructions, decides when to use tools, and generates responses.FunctionTool: A custom capability provided to the agent. In this case, it's a tool to update the workflow status.ToolContext: An object automatically passed to our tool, allowing it to access and modify theSession State.SessionService(InMemorySessionService): Manages the conversation's state (workflow_status).InMemorymeans the state exists only while this script runs.Runner: Orchestrates the entire interaction: passes user input to the agent, handles tool calls, manages the state via theSessionService, and delivers the final response.Session State: A dictionary holding data for the current conversation (session). Here, you use it to store{'workflow_status': '...'}.
3. Define ADK Components (Tool, Agent, Services)
Now, let's define the core ADK components for our Stateful Echo Agent:
- Tool (
set_workflow_state): A Python function wrapped as an ADKFunctionTool. This function will modify theworkflow_statusin the session state when called by the agent. - Agent (
echo_agent): AnLlmAgentconfigured with the Gemini model, specific instructions on when to call thestate_tool, and the tool itself. - Services (
session_service,runner): TheInMemorySessionServiceto hold state and theRunnerto execute the agent. - Session: Used to create a specific session instance with an initial state
{'workflow_status': 'START'}.
4. Run the Interaction
Now send a simple message ("Hello ADK!") to the echo_agent via the Runner. The Runner will manage the execution flow according to the agent's instructions.
Expected Flow:
- Agent receives "Hello ADK!".
- Agent calls
set_workflow_statetool (state ->PROCESSING). - Agent calls
set_workflow_statetool (state ->END). - Agent responds with the text "Hello ADK!".
The next cell initiates the run_async call and processes the stream of events generated during execution, logging the steps.
WARNING:google_genai.types:Warning: there are non-text parts in the response: ['function_call'],returning concatenated text result from text parts,check out the non text parts for full response from model.
[Event 1] Type: Event
Role: model
>>> Function Call <<<
Name: set_workflow_state
Args: {'state_name': 'PROCESSING'}
[Event 2] Type: Event
Role: user
<<< Function Response >>>
Name: set_workflow_state
Data: {'status': 'success', 'message': 'Workflow state set to PROCESSING'}
WARNING:google_genai.types:Warning: there are non-text parts in the response: ['function_call'],returning concatenated text result from text parts,check out the non text parts for full response from model.
[Event 3] Type: Event
Role: model
>>> Function Call <<<
Name: set_workflow_state
Args: {'state_name': 'END'}
[Event 4] Type: Event
Role: user
<<< Function Response >>>
Name: set_workflow_state
Data: {'status': 'success', 'message': 'Workflow state set to END'}
[Event 5] Type: Event
Role: model
Text: 'Hello ADK!
'
5. Analyze the Results
Examine the "Agent Event Log" printed above. You should clearly see the sequence reflecting the agent's instructions:
Function Callevent targetingset_workflow_statewithargs={'state_name': 'PROCESSING'}.Function Responseevent confirming the first tool execution.Function Callevent targetingset_workflow_statewithargs={'state_name': 'END'}.Function Responseevent confirming the second tool execution.- final event containing the agent's text response (the echoed message).
The next cell verifies this outcome by checking the final state stored in the session.
Final workflow status: END Agent response: Hello ADK!
Next Steps & Further Learning
This notebook demonstrated the basic structure of an ADK application, including:
- Defining an
Agentpowered by Gemini. - Creating a simple
FunctionToolto modify state. - Using
SessionServiceandToolContextfor state management. - Orchestrating the flow with the
Runner.
To dive deeper into the capabilities of the Google Agent Development Kit:
- Explore the Official Documentation: For detailed explanations of all components (Agents, Tools, Sessions, Callbacks, Multi-Agent systems, etc.), visit the Google ADK Documentation site.
- Try the Getting Started Notebook: Explore the official ADK tutorial notebook on Colab for a hands-on introduction to building your first agent.
- Discover More Examples: Check out the Google ADK GitHub repository for a wider range of examples, including more complex workflows, integrations, and advanced agent patterns.
Consider exploring concepts like:
- Workflow Agents (
SequentialAgent,ParallelAgent,LoopAgent) for structured process control. - Multi-Agent Systems for building collaborative agent teams.
- Other Tool Types (OpenAPI, Google Cloud Tools, Built-in Tools) for broader integrations.