This page demonstrates advanced usage of the smolagents library, with a special focus on Human-in-the-Loop (HITL) approaches for interactive plan creation, user-driven plan modification, and memory preservation in agentic workflows.
The example is based on the code in examples/plan_customization/plan_customization.py.
This example teaches you how to implement Human-in-the-Loop strategies to:
The agent is configured to pause after creating a plan. This is achieved by registering a step callback for the PlanningStep:
agent = CodeAgent(
model=InferenceClientModel(),
tools=[DuckDuckGoSearchTool()],
planning_interval=5, # Plan every 5 steps
step_callbacks={PlanningStep: interrupt_after_plan},
max_steps=10,
verbosity_level=1
)When the agent creates a plan, the callback displays it and prompts the human user to:
Example interaction:
============================================================
🤖 AGENT PLAN CREATED
============================================================
1. Search for recent AI developments
2. Analyze the top results
3. Summarize the 3 most significant breakthroughs
4. Include sources for each breakthrough
============================================================
Choose an option:
1. Approve plan
2. Modify plan
3. Cancel
Your choice (1-3):This Human-in-the-Loop step enables a human to intervene and review or modify the plan before execution continues, and ensures that the agent’s actions align with human intent.
If the user chooses to modify, they can edit the plan directly. The updated plan is then used for subsequent execution steps.
By running the agent with reset=False, all previous steps and memory are preserved. This allows you to resume execution after an interruption or plan modification:
# First run (may be interrupted)
agent.run(task, reset=True)
# Resume with preserved memory
agent.run(task, reset=False)You can inspect the agent’s memory to see all steps taken so far:
print(f"Current memory contains {len(agent.memory.steps)} steps:")
for i, step in enumerate(agent.memory.steps):
step_type = type(step).__name__
print(f" {i+1}. {step_type}")The example includes error handling for:
This example demonstrates:
For the full code, see examples/plan_customization.