Refining the Agent Using Observability › Turning Observations into Improvements
AI Agent HandbookAll chapters Refining the Agent Using Observability › Turning Observations into ImprovementsOne section · Sections in this chapter3 / 11 ↓
Once you've identified patterns, you can make targeted improvements. Let's walk through some examples.
Example 1: Addressing Knowledge Gaps
Suppose your logs show the agent frequently says "I don't know" when asked about recent events. The pattern is clear: it lacks access to current information.
The fix: Add a news search tool or update its knowledge base more frequently.
## Using Claude Sonnet 4.5 for its superior tool-use capabilities
from anthropic import Anthropic
client = Anthropic(api_key=os.getenv("ANTHROPIC_API_KEY"))
## Define a news search tool
tools = [
{
"name": "search_news",
"description": "Search for recent news articles about a topic. Use this when the user asks about current events or recent developments.",
"input_schema": {
"type": "object",
"properties": {
"query": {
"type": "string",
"description": "The search query for news articles",
}
},
"required": ["query"],
},
}
]
def search_news(query):
"""Simulate a news search (in practice, call a real news API)."""
# This would call a real news API
return (
f"Recent articles about {query}: [Article 1], [Article 2], [Article 3]"
)
## Now when the agent encounters a question about recent events,
## it has a tool to help instead of saying "I don't know"
response = client.messages.create(
model="claude-sonnet-4-5",
max_tokens=1024,
tools=tools,
messages=[
{
"role": "user",
"content": "What happened in the tech industry this week?",
}
],
)
print(response.content)[31m---------------------------------------------------------------------------[39m
[31mNameError[39m Traceback (most recent call last)
[36mCell[39m[36m [39m[32mIn[3][39m[32m, line 4[39m
[32m 1[39m [38;5;66;03m## Using Claude Sonnet 4.5 for its superior tool-use capabilities[39;00m
[32m 2[39m [38;5;28;01mfrom[39;00m[38;5;250m [39m[34;01manthropic[39;00m[38;5;250m [39m[38;5;28;01mimport[39;00m Anthropic
[32m----> [39m[32m4[39m client = Anthropic(api_key=[43mos[49m.getenv([33m"[39m[33mANTHROPIC_API_KEY[39m[33m"[39m))
[32m 6[39m [38;5;66;03m## Define a news search tool[39;00m
[32m 7[39m tools = [
[32m 8[39m {
[32m 9[39m [33m"[39m[33mname[39m[33m"[39m: [33m"[39m[33msearch_news[39m[33m"[39m,
[32m (...)[39m[32m 21[39m }
[32m 22[39m ]
[31mNameError[39m: name 'os' is not definedAfter adding this tool, you'd monitor the logs again. You should see fewer "I don't know" responses to news-related questions and more successful tool calls to search_news.
Example 2: Optimizing Tool Usage
Your logs might show the agent calling the web search tool for information it already has in its conversation history. This wastes time and costs money.
The fix: Adjust the prompt to encourage the agent to check its memory first.
## Improved system prompt that emphasizes checking memory first
system_prompt = """You are a helpful personal assistant.
Before using any tools, always check if you already have the information you need:
1. Review the conversation history
2. Check if the user has already provided this information
3. Only use tools if you genuinely need new information
This saves time and provides faster responses."""
## The agent will now be more thoughtful about when to use toolsExample 3: Streamlining Reasoning
If your logs show the agent's reasoning chains are consistently long and winding, you can guide it toward more efficient thinking.
The fix: Add examples of concise reasoning to the prompt.
system_prompt = """You are a helpful personal assistant.
When reasoning through problems, be concise and direct:
Good example:
User asks: "What's 15% of 80?"
Reasoning: Need to calculate 15% of 80. That's $0.15 \times 80 = 12$.
Answer: 12
Avoid overthinking simple problems. Break down complex problems, but keep each step clear and necessary."""