Get startedGet started for free

Working With Multi-Step Agents

1. Working With Multi-Step Agents

Welcome back!

2. Challenges of Multi-Step Agents

In the previous lesson, you built an agentic RAG system. That's one example of a multi-step agent. These systems are powerful, but when you deploy them for real users, challenges often come up. Let's look at the first challenge and how smolagents helps solve it.

3. Example: Travel Assistant

Imagine you're building a travel assistant. A user asks: "Plan a 2-week family vacation to Europe with historical sites for adults and fun activities for kids, staying under $5000." Without guidance, the agent might immediately start searching for expensive Paris hotels, use up the entire budget, and never get to kid-friendly activities or other destinations.

4. Planning Intervals: Helping Agents Rethink

smolagents introduces planning intervals to solve this. Planning intervals force agents to pause and rethink their approach. Instead of committing to a wrong path, they stop every few steps to evaluate what they've learned. With planning_interval equals 3, the agent pauses every 3 steps to rethink its plans.

5. Agent Run with Planning Intervals

For our travel example, after initial searches, it might reconsider: "I found expensive hotels in Paris, but that blows the budget. I should look for affordable options so we can visit multiple cities. I also need to include kid-friendly activities." This check-in prevents poor planning and wasted effort.

6. Callbacks: Hooks Into the Agent's Process

But sometimes, the challenge isn't about the agent's reasoning. It's about observing and controlling the agent's workflow. That's where callbacks come in. Callbacks are hooks that let you run custom code at key moments in the agent's execution, like after each step. This is useful for debugging, monitoring patterns, and even collecting data on how your agent performs over time.

7. Basic Callback Function

Every callback function has the same basic structure. The function gets two inputs: agent_step contains details about the current step (such as the plan or step number), and agent gives you the full agent object, so you can access its state and methods. Inside this function, you write whatever logic your application needs.

8. Planning Step Callbacks

There are two types of steps where callbacks can run. Let's start with planning steps. Planning steps happen when the agent thinks through its strategy. For example, here is a planning_callback() function. This function grabs the planning text from agent_step.plan, cuts it to 300 characters so it's readable, and formats it nicely so you get a clean view of what the agent is thinking. This callback can be a more user-friendly version than the agent's terminal logs.

9. Action Step Callbacks

Action steps happen when the agent actually does something, like searching documents, calling tools, or giving final answers by writing code. Here we get the step number and check if this is the final answer. When it's the last step, we can see how many tokens the whole conversation used. This callback is designed for user-friendly updates of the agent's progress.

10. Adding Callbacks to Agents

To use these callbacks, you register them when creating your agent. First, you import ActionStep and PlanningStep classes. Then, the step_callbacks parameter takes a dictionary mapping step types to your functions. The agent automatically calls the right function based on what type of step just finished.

11. What Can You Do with Callbacks?

The callbacks system opens up many possibilities for applications. For example: logging searches to understand what users look for most, adding human-approval checkpoints, sending progress updates to dashboards or apps, adjusting agent behavior mid-run based on performance, and more...

12. Let's practice!

In the exercises, you will practice adding callbacks to your agents!

Create Your Free Account

or

By continuing, you accept our Terms of Use, our Privacy Policy and that your data is stored in the USA.