Get startedGet started for free

Managing Agent Memory

1. Managing Agent Memory

Welcome back!

2. Why Memory Matters

So far, you've built working agents and even coordinated them through a manager agent. But what makes agents really usable, and not just functional, is their ability to retain context over time and handle follow-up interactions naturally. This is where memory comes in.

3. Stateless by Default

By default, smolagents treat each .run() call as a fresh start. They don't remember anything from a previous run unless explicitly told to retain context. Here's a quick example. The second response fails because the agent doesn't understand the term "those skills" because it has no memory of the first conversation.

4. Retaining Memory Between Interactions

Let's revisit the same example but with memory. To maintain conversation history, pass reset=False to the second .run() call. With memory retained, the agent is now able to understand the follow-up request.

5. Memory Helps You Debug, Too

But memory in smolagents isn't just about user experience; it's also a powerful debugging tool. Let's say your agent gives a strange answer. You suspect it made a calculation error, but you're not sure why. smolagents gives you access to the full memory of your agent. This lets you inspect what happened in an agent run. You can use it to: review the code the agent generated, trace its reasoning, actions, and tool usage, or debug incorrect answers or broken logic. Let's walk through two key memory methods.

6. What Code Did the Agent Run?

The .return_full_code() method lets you see all executed code. Use this when you suspect that the agent made a mistake in logic, calculations, or API calls. This method combines all code actions from the agent's steps into one script. Reviewing it can quickly reveal hardcoded values, misused tools, or flawed logic.

7. What Was the Agent Thinking?

When code logic looks right but the final answer still seems off, check the agent's thinking process with the .get_succinct_steps() method. This method returns the agent's complete thought process as a list of dictionaries. Each one shows the agent's inputs, tool calls, code execution, and observations. This lets you pinpoint what the agent thought, did, and saw at each stage, and which sub-agent handled the step.

8. Save Agent Sessions for Analysis

Some issues only become visible when they repeat across sessions. For instance, your agent may consistently overestimate salaries in certain industries or misinterpret similar types of questions. To track these patterns, save each agent conversation to a file. This function: retrieves the full agent memory via .get_succinct_steps(), and saves the output as readable JSON. These logs can help with post-hoc analysis, regression testing, or improving agent behavior over time.

9. Fixing Agent Failures: What to Adjust

When memory analysis reveals issues, fixes often come down to these following aspects: managing memory correctly, choosing more powerful language models for better reasoning, improving system prompts to guide behavior, writing clearer tool docstrings for improved delegation.

10. Let's practice!

Now you're ready to use these memory techniques in the exercises!

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.