Adding Logs to the Agent › Practical Logging Patterns

Michael BrenndoerferAugust 6, 20252 min read
AI Agent HandbookAll chapters
Part 12 of 15Observability and Debugging - Peering Inside the Agent
Chapter 33Adding Logs to the Agent › Practical Logging Patterns
Reading section 8 of 10 · Practical Logging Patterns · 2 minSection 8 of 10 · 2 min

Book contents

Section 8 of 10 · 2 min read

Adding Logs to the Agent › Practical Logging PatternsOne section · Sections in this chapter8 / 10 ↓

As you add logging to your agent, you'll develop patterns that work well for different situations. Here are a few patterns that prove useful in practice.

Entry and Exit Logging: Log when important functions start and when they complete. This helps you verify the agent is executing the right sequence of operations.

In[15]:
Code
def plan_multi_step_task(goal):
    logger.info(f"Planning started for goal: {goal}")

    # Generate plan
    plan = create_plan(goal)
    logger.info(f"Generated plan with {len(plan)} steps")

    # Execute each step
    for i, step in enumerate(plan):
        logger.info(f"Executing step {i + 1}/{len(plan)}: {step}")
        result = execute_step(step)
        logger.info(f"Step {i + 1} completed with result: {result}")

    logger.info("Planning completed successfully")
    return result

State Change Logging: Log whenever your agent's state changes in an important way. This helps you track how the agent's understanding evolves.

In[16]:
Code
def update_conversation_state(new_info):
    logger.info(f"State before: {current_state}")
    current_state.update(new_info)
    logger.info(f"State after: {current_state}")

Decision Point Logging: When the agent makes a decision based on some logic, log both what it decided and why.

In[17]:
Code
def should_ask_for_clarification(query, confidence):
    if confidence < 0.7:
        logger.info(
            f"Requesting clarification (confidence: {confidence} < 0.7)"
        )
        return True
    else:
        logger.info(
            f"Proceeding without clarification (confidence: {confidence})"
        )
        return False

These patterns give structure to your logging, making it easier to find the information you need when debugging or analyzing your agent's behavior.