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
Adding Logs to the Agent › Practical Logging PatternsOne section · Sections in this chapter8 / 10 ↓
Sections in this chapter10
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]:
Codedef 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 resultState 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]:
Codedef 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]:
Codedef 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 FalseThese patterns give structure to your logging, making it easier to find the information you need when debugging or analyzing your agent's behavior.