Beginner-Friendly Topic
Take your time - it's perfectly normal to re-read this topic 2-3 times. Try the interactive code editor below to run code yourself. Use the Q&A section to check your understanding before moving on.You've got this!
Tool Result Handling
After a tool runs, the agent must interpret the result and decide what to do next. Good result handling means the agent responds appropriately to both success and error outcomes.
6 min•By Priygop Team•Updated 2026
Result Handling Logic
Result Handling Logic
# Agent result handling pattern
def handle_tool_result(result: dict, goal: str, state: dict) -> str:
"""
Decide the next action based on a tool result.
Returns: 'continue', 'retry', 'fallback', 'escalate', or 'finish'
"""
status = result.get("status")
if status == "success":
# Add result to state
state["last_result"] = result
# Check if goal is now complete
if is_goal_complete(goal, state):
return "finish"
return "continue"
error_type = result.get("error_type", "unknown")
if error_type == "timeout":
# Transient error - retry
if state.get("retry_count", 0) < 2:
state["retry_count"] = state.get("retry_count", 0) + 1
return "retry"
return "escalate" # Too many retries
elif error_type == "network":
# Try a fallback tool if available
if state.get("fallback_available"):
return "fallback"
return "escalate"
elif error_type == "validation":
# Agent sent bad arguments - cannot retry without fixing them
return "escalate"
elif error_type == "not_found":
# Resource missing - try alternative approach
return "continue"
return "escalate" # Unknown errors always escalate
def is_goal_complete(goal, state):
return state.get("last_result", {}).get("status") == "success"
state = {"retry_count": 0}
result = {"status": "error", "error_type": "timeout"}
action = handle_tool_result(result, "Find data", state)
print(f"Next action: {action}")