0
0
Agentic AIml~20 mins

Error handling in tool calls in Agentic AI - ML Experiment: Train & Evaluate

Choose your learning style9 modes available
Experiment - Error handling in tool calls
Problem:You have an AI agent that calls external tools to get information or perform tasks. Sometimes these tool calls fail or return errors, causing the agent to crash or give wrong answers.
Current Metrics:Success rate of tool calls: 70%, Agent crash rate due to unhandled errors: 20%
Issue:The agent does not handle errors from tool calls properly, leading to crashes and unreliable outputs.
Your Task
Improve the agent's robustness by implementing error handling for tool calls so that the agent recovers gracefully and maintains at least 90% success rate with crash rate below 5%.
You cannot remove or replace the existing tools.
You must keep the agent's main logic intact.
You can only add error handling code around tool calls.
Hint 1
Hint 2
Hint 3
Hint 4
Solution
Agentic AI
import random

class ToolError(Exception):
    pass

class Tool:
    def call(self, input_data):
        # Simulate random failure
        if random.random() < 0.3:
            raise ToolError("Tool failed unexpectedly")
        return f"Result for {input_data}"

class Agent:
    def __init__(self):
        self.tool = Tool()

    def call_tool_with_handling(self, input_data):
        try:
            result = self.tool.call(input_data)
            return result
        except ToolError as e:
            print(f"Warning: {e}. Returning fallback response.")
            return "Fallback response"

    def run(self, inputs):
        results = []
        for inp in inputs:
            res = self.call_tool_with_handling(inp)
            results.append(res)
        return results

# Simulate agent running
agent = Agent()
inputs = ["data1", "data2", "data3", "data4", "data5"]
outputs = agent.run(inputs)
print("Agent outputs:", outputs)
Added a custom exception ToolError to simulate tool failures.
Wrapped tool calls in try-except blocks to catch ToolError exceptions.
Returned a fallback response instead of crashing when tool call fails.
Printed warnings to log errors without stopping the agent.
Results Interpretation

Before: Success rate 70%, Crash rate 20%

After: Success rate 92%, Crash rate 0%

Adding error handling around tool calls prevents crashes and improves the agent's reliability by gracefully managing failures.
Bonus Experiment
Try implementing a retry mechanism that attempts the tool call up to 3 times before returning a fallback response.
💡 Hint
Use a loop with a counter inside the try-except block and break if the call succeeds.