Lab 39: Fixing a "Hallucinating" Agent with Plugins
Goal
In this lab, you will work with an agent that has been given misleading instructions. It will try to call a tool by the wrong name. Your task is to use the ReflectAndRetryToolPlugin to allow the agent to self-correct and succeed without changing the misleading instruction.
Step 1: Create the Project
-
Create a new agent project:
adk create retry-agentChoose the Programmatic (Python script) option.
-
Navigate to the directory:
cd retry-agent
Step 2: The "Broken" Agent
Exercise: Open agent.py and replace its content with the code below.
This code defines a tool named secret_calculator. However, the agent's system instruction (deliberately) tells it to use a tool named super_calc.
from google.adk.agents import Agent
from google.adk.tools import FunctionTool
from google.adk.runner import Runner
import asyncio
# The actual tool
def secret_calculator(a: int, b: int) -> int:
"""Adds two numbers."""
return a + b
# The agent with MISLEADING instructions
agent = Agent(
name="confused_agent",
model="gemini-2.5-flash",
# We lie to the agent about the tool name!
instruction="You are a helper. To add numbers, you MUST use the tool named 'super_calc'. Do not use any other tool name.",
tools=[FunctionTool(secret_calculator)]
)
async def main():
# TODO: Initialize the plugin here later
runner = Runner(
agent=agent,
# TODO: Add plugins=[...] here later
)
print("User: What is 5 + 5?")
result = await runner.run("What is 5 + 5?")
print(f"Agent: {result.text}")
if __name__ == "__main__":
asyncio.run(main())
Step 3: Observe the Failure
- Run the agent:
python agent.py - Analyze the Output:
You should see an error (likely a
ValueErrororToolNotFoundErrorstack trace, or the agent apologizing that it can't find the tool). This is because the agent tried to callsuper_calc, but onlysecret_calculatoris registered.
Step 4: Add the Safety Net
Now, let's fix this using the Plugin instead of fixing the prompt.
-
Import the Plugin: Add
from google.adk.plugins import ReflectAndRetryToolPluginto your imports. -
Initialize the Plugin: Create an instance of
ReflectAndRetryToolPluginwithmax_retries=3. -
Register the Plugin: Update the
Runnerinitialization to include your plugin instance in thepluginslist.
Step 5: Verify Success
-
Run the agent again:
python agent.py -
Analyze the Output:
- It might take a few seconds longer than usual.
- You should now see the correct answer: "Agent: The result is 10" (or similar).
- What happened?
- Agent called
super_calc. - System threw "Tool not found".
- Plugin caught it and told the Agent: "Error: Tool 'super_calc' not found. Available tools: 'secret_calculator'".
- Agent reasoned: "Oops, I should use 'secret_calculator'."
- Agent called
secret_calculator. - Success!
- Agent called
-
(Optional) Prove the Retry: To see the actual "hidden" retry steps, you can inspect the execution trace. Add this to your
agent.pyafter the result print:# Check the execution trace
if hasattr(result, "trace"):
print("\n--- Execution Trace ---")
for step in result.trace:
print(f"- {step}")You should see an entry for the failed
super_calccall followed by the successfulsecret_calculatorcall.
Self-Reflection Questions
- Why is it better to handle this with a Plugin rather than just fixing the prompt in this specific scenario? (Think about dynamic/unknown errors).
- What would happen if we set
max_retries=0? - How does this plugin help with "transient" errors, like a temporary network glitch in a tool?
🕵️ Hidden Solution 🕵️
Looking for the solution? Here's a hint (Base64 decode me):
L2RvYy1hZGstdHJhaW5pbmcvbW9kdWxlMzktcGx1Z2lucy9sYWItc29sdXRpb24=
The direct link is: Lab Solution