0
0
Agentic_aiml~20 mins

Logging tool calls and results in Agentic Ai - ML Experiment: Train & Evaluate

Choose your learning style8 modes available
Experiment - Logging tool calls and results
Problem:You have an AI agent that calls various tools during its operation. Currently, you do not log these tool calls or their results, making it hard to debug or improve the agent.
Current Metrics:No logs available; unable to track tool usage or results.
Issue:Lack of logging causes difficulty in understanding agent behavior and troubleshooting errors.
Your Task
Implement a logging system that records each tool call with its input parameters and the returned results. Ensure logs are clear and easy to read.
Do not change the core logic of the agent or tools.
Logging should not slow down the agent significantly.
Use simple print statements or a basic logging library.
Hint 1
Hint 2
Hint 3
Solution
Agentic_ai
import time

def log_tool_call(tool_name, input_params, tool_function):
    print(f"[LOG] Calling tool: {tool_name} with input: {input_params}")
    start_time = time.time()
    result = tool_function(input_params)
    end_time = time.time()
    duration = end_time - start_time
    print(f"[LOG] Tool: {tool_name} returned: {result} (took {duration:.3f} seconds)")
    return result

# Example tool function

def example_tool(input_data):
    # Simulate processing
    time.sleep(0.1)
    return f"Processed {input_data}"

# Agent calling the tool with logging

input_for_tool = "sample data"
output = log_tool_call("ExampleTool", input_for_tool, example_tool)
print(f"Agent received output: {output}")
Added a wrapper function 'log_tool_call' to log tool name, input, output, and duration.
Used print statements for simple logging.
Kept original tool function unchanged.
Demonstrated usage with an example tool and agent call.
Results Interpretation

Before: No logs, no visibility into tool calls or results.

After: Logs print tool name, inputs, outputs, and execution time for every call.

Logging tool calls and results helps understand agent behavior and troubleshoot issues without changing core logic.
Bonus Experiment
Extend the logging to write logs to a file with timestamps and log levels (INFO, ERROR).
💡 Hint
Use Python's built-in logging module with a FileHandler and formatters.