Callbacks are functions that get executed at specific points during agent execution, allowing you to monitor, log, and control the agent's behavior without modifying the core logic.
Sign in to save downloads to your library and vote.
Preview
π Tutorial 6: Callbacks
π― What You'll Learn
- Agent Lifecycle Callbacks: Monitor agent creation, initialization, and cleanup
- LLM Interaction Callbacks: Track model requests, responses, and token usage
- Tool Execution Callbacks: Monitor tool calls, parameters, and results
π‘ Core Concept: Callbacks
Callbacks are functions that get executed at specific points during agent execution, allowing you to monitor, log, and control the agent's behavior without modifying the core logic.
Callback Flow Diagram
βββββββββββββββββββ βββββββββββββββββββ βββββββββββββββββββ
β Agent Start βββββΆβ LLM Request βββββΆβ Tool Execution β
β Callback β β Callback β β Callback β
βββββββββββββββββββ βββββββββββββββββββ βββββββββββββββββββ
β β β
βΌ βΌ βΌ
βββββββββββββββββββ βββββββββββββββββββ βββββββββββββββββββ
β Agent End β β LLM Response β β Tool Result β
β Callback β β Callback β β Callback β
βββββββββββββββββββ βββββββββββββββββββ βββββββββββββββββββ
Why Use Callbacks?
- Monitoring: Track agent performance and behavior
- Logging: Record interactions for debugging and analysis
- Control: Modify behavior based on specific events
- Integration: Connect agents to external systems
- Debugging: Understand what's happening inside the agent
π Tutorial Overview
This tutorial covers three essential callback patterns in Google ADK:
- Agent Lifecycle Callbacks: Monitor agent creation, initialization, and cleanup events
- LLM Interaction Callbacks: Track model requests, responses, and token usage
- Tool Execution Callbacks: Monitor tool calls, parameters, and execution results
Each sub-tutorial provides simple, focused examples that demonstrate specific callback patterns.
π Project Structure
6_callbacks/
βββ README.md # This file - concept explanation
βββ 6_1_agent_lifecycle_callbacks/ # Agent lifecycle monitoring
β βββ README.md # Lifecycle callback patterns
β βββ agent.py # Agent with lifecycle callbacks
β βββ app.py # Streamlit interface
β βββ requirements.txt # Dependencies
βββ 6_2_llm_interaction_callbacks/ # LLM request/response tracking
β βββ README.md # LLM callback patterns
β βββ agent.py # Agent with LLM callbacks
β βββ app.py # Streamlit interface
β βββ requirements.txt # Dependencies
βββ 6_3_tool_execution_callbacks/ # Tool execution monitoring
βββ README.md # Tool callback patterns
βββ agent.py # Agent with tool callbacks
βββ app.py # Streamlit interface
βββ requirements.txt # Dependencies
π― Learning Objectives
By the end of this tutorial, you'll understand:
- β Callback Fundamentals: How callbacks work in Google ADK
- β Lifecycle Monitoring: Track agent creation, initialization, and cleanup
- β LLM Tracking: Monitor model requests, responses, and performance
- β Tool Monitoring: Track tool execution and results
- β Practical Applications: Real-world use cases for callbacks
- β Debugging Techniques: Use callbacks for troubleshooting
π Getting Started
Prerequisites
- Python 3.11+
- Google AI Studio API key
- Basic understanding of Google ADK (Tutorials 1-5)
Setup
- Get API Key: Visit Google AI Studio
- Create .env file: Add
GOOGLE_API_KEY=your_key_here - Install dependencies:
pip install -r requirements.txt
Run Tutorials
# Agent Lifecycle Callbacks
cd 6_1_agent_lifecycle_callbacks
streamlit run app.py
# LLM Interaction Callbacks
cd ../6_2_llm_interaction_callbacks
streamlit run app.py
# Tool Execution Callbacks
cd ../6_3_tool_execution_callbacks
streamlit run app.py
βοΈ Callback Patterns
1. Agent Lifecycle Callbacks
def on_agent_start(agent_name: str):
print(f"βΆοΈ Agent {agent_name} started")
def on_agent_end(agent_name: str, result: str):
print(f"β
Agent {agent_name} completed: {result}")
# Register callbacks
agent = LlmAgent(
name="my_agent",
model="gemini-3-flash-preview",
on_start=on_agent_start,
on_end=on_agent_end
)
2. LLM Interaction Callbacks
def on_llm_request(model: str, prompt: str):
print(f"π€ LLM Request to {model}: {prompt[:50]}...")
def on_llm_response(model: str, response: str, tokens: int):
print(f"π₯ LLM Response from {model}: {tokens} tokens")
# Register callbacks
agent = LlmAgent(
name="my_agent",
model="gemini-3-flash-preview",
on_llm_request=on_llm_request,
on_llm_response=on_llm_response
)
3. Tool Execution Callbacks
def on_tool_start(tool_name: str, params: dict):
print(f"π§ Tool {tool_name} started with params: {params}")
def on_tool_end(tool_name: str, result: str):
print(f"β
Tool {tool_name} completed: {result}")
# Register callbacks
agent = LlmAgent(
name="my_agent",
model="gemini-3-flash-preview",
tools=[my_tool],
on_tool_start=on_tool_start,
on_tool_end=on_tool_end
)
π Use Cases
Monitoring & Analytics
- Track agent performance metrics
- Monitor token usage and costs
- Analyze tool usage patterns
- Debug agent behavior
Logging & Debugging
- Log all agent interactions
- Debug tool execution issues
- Monitor LLM response quality
- Track error patterns
Integration & Control
- Connect to external monitoring systems
- Implement custom error handling
- Add authentication and validation
- Control agent behavior dynamically
π Next Steps
After completing this tutorial, you'll be ready for:
- Advanced Agent Patterns - Complex agent architectures
- Production Deployment - Deploying agents to production
- Custom Tools - Building custom tools and integrations
π Additional Resources
Ingestion metadata
- Source catalog
- awesome-llm-apps
- Repository
- Shubhamsaboo/awesome-llm-apps Β· main
- File path
- ai_agent_framework_crash_course/google_adk_crash_course/6_callbacks/README.md
- Last refreshed
- 7/24/2026, 3:00:13 AM (53m ago)
- Refresh schedule
- Daily Β· 03:00 UTC
- Dedupe status
- Unique Β· deduped by (source, url)