Adding Logs to the Agent › Adding Logs to Our Assistant

Michael BrenndoerferAugust 6, 20251 min read
AI Agent HandbookAll chapters
Part 12 of 15Observability and Debugging - Peering Inside the Agent
Chapter 33Adding Logs to the Agent › Adding Logs to Our Assistant
Reading section 4 of 10 · Adding Logs to Our Assistant · 1 minSection 4 of 10 · 1 min

Book contents

Section 4 of 10 · 1 min read

Adding Logs to the Agent › Adding Logs to Our AssistantOne section · Sections in this chapter4 / 10 ↓

Let's take a simplified version of our personal assistant and add logging at the key points. We'll start by setting up the logging configuration.

In[9]:
Code
## Example (Claude Sonnet 4.5)
import logging
import os

from anthropic import Anthropic

## Configure logging with a clear format
logging.basicConfig(
    level=logging.INFO,
    format="%(asctime)s - %(name)s - %(levelname)s - %(message)s",
)
logger = logging.getLogger(__name__)

Now let's look at the main query processing function with logging at each decision point:

In[10]:
Code
def process_query(user_input):
    # Log the incoming query
    logger.info(f"User query: {user_input}")

    # Check if we need tools
    needs_tool = check_tool_requirement(user_input)
    logger.info(f"Tool required: {needs_tool}")

    if needs_tool:
        tool_name = select_tool(user_input)
        logger.info(f"Selected tool: {tool_name}")

        tool_result = use_tool(tool_name, user_input)
        logger.info(f"Tool result: {tool_result}")

        response = generate_response_with_tool(user_input, tool_result)
    else:
        logger.info("Generating direct response (no tool)")
        response = generate_direct_response(user_input)

    # Log final response
    logger.info(f"Assistant response: {response}")
    return response

Notice how each major decision point has a log statement. When you run this agent and something goes wrong, you can trace through the logs to see exactly what path the agent took.