Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place. Commercial Alternative to JupyterHub.
Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place. Commercial Alternative to JupyterHub.
Path: blob/main/smolagents_doc/en/building_good_agents.ipynb
Views: 2934
Building good agents
There's a world of difference between building an agent that works and one that doesn't. How can we build agents that fall into the former category? In this guide, we're going to talk about best practices for building agents.
[!TIP] If you're new to building agents, make sure to first read the intro to agents and the guided tour of smolagents.
The best agentic systems are the simplest: simplify the workflow as much as you can
Giving an LLM some agency in your workflow introduces some risk of errors.
Well-programmed agentic systems have good error logging and retry mechanisms anyway, so the LLM engine has a chance to self-correct their mistake. But to reduce the risk of LLM error to the maximum, you should simplify your workflow!
Let's revisit the example from the intro to agents: a bot that answers user queries for a surf trip company. Instead of letting the agent do 2 different calls for "travel distance API" and "weather API" each time they are asked about a new surf spot, you could just make one unified tool "return_spot_information", a function that calls both APIs at once and returns their concatenated outputs to the user.
This will reduce costs, latency, and error risk!
The main guideline is: Reduce the number of LLM calls as much as you can.
This leads to a few takeaways:
Whenever possible, group 2 tools in one, like in our example of the two APIs.
Whenever possible, logic should be based on deterministic functions rather than agentic decisions.
Improve the information flow to the LLM engine
Remember that your LLM engine is like an intelligent robot, tapped into a room with the only communication with the outside world being notes passed under a door.
It won't know of anything that happened if you don't explicitly put that into its prompt.
So first start with making your task very clear! Since an agent is powered by an LLM, minor variations in your task formulation might yield completely different results.
Then, improve the information flow towards your agent in tool use.
Particular guidelines to follow:
Each tool should log (by simply using
print
statements inside the tool'sforward
method) everything that could be useful for the LLM engine.In particular, logging detail on tool execution errors would help a lot!
For instance, here's a tool that retrieves weather data based on location and date-time:
First, here's a poor version:
Why is it bad?
there's no precision of the format that should be used for
date_time
there's no detail on how location should be specified.
there's no logging mechanism trying to make explicit failure cases like location not being in a proper format, or date_time not being properly formatted.
the output format is hard to understand
If the tool call fails, the error trace logged in memory can help the LLM reverse engineer the tool to fix the errors. But why leave it with so much heavy lifting to do?
A better way to build this tool would have been the following:
In general, to ease the load on your LLM, the good question to ask yourself is: "How easy would it be for me, if I was dumb and using this tool for the first time ever, to program with this tool and correct my own errors?".
Give more arguments to the agent
To pass some additional objects to your agent beyond the simple string describing the task, you can use the additional_args
argument to pass any type of object:
For instance, you can use this additional_args
argument to pass images or strings that you want your agent to leverage.
How to debug your agent
1. Use a stronger LLM
In an agentic workflows, some of the errors are actual errors, some other are the fault of your LLM engine not reasoning properly. For instance, consider this trace for an CodeAgent
that I asked to create a car picture:
The user sees, instead of an image being returned, a path being returned to them. It could look like a bug from the system, but actually the agentic system didn't cause the error: it's just that the LLM brain did the mistake of not saving the image output into a variable. Thus it cannot access the image again except by leveraging the path that was logged while saving the image, so it returns the path instead of an image.
The first step to debugging your agent is thus "Use a more powerful LLM". Alternatives like Qwen2/5-72B-Instruct
wouldn't have made that mistake.
2. Provide more guidance / more information
You can also use less powerful models, provided you guide them more effectively.
Put yourself in the shoes of your model: if you were the model solving the task, would you struggle with the information available to you (from the system prompt + task formulation + tool description) ?
Would you need some added clarifications?
To provide extra information, we do not recommend to change the system prompt right away: the default system prompt has many adjustments that you do not want to mess up except if you understand the prompt very well. Better ways to guide your LLM engine are:
If it's about the task to solve: add all these details to the task. The task could be 100s of pages long.
If it's about how to use tools: the description attribute of your tools.
3. Change the system prompt (generally not advised)
If above clarifications are not sufficient, you can change the system prompt.
Let's see how it works. For example, let us check the default system prompt for the CodeAgent (below version is shortened by skipping zero-shot examples).
Here is what you get:
As you can see, there are placeholders like "{{tool_descriptions}}"
: these will be used upon agent initialization to insert certain automatically generated descriptions of tools or managed agents.
So while you can overwrite this system prompt template by passing your custom prompt as an argument to the system_prompt
parameter, your new system prompt must contain the following placeholders:
"{{tool_descriptions}}"
to insert tool descriptions."{{managed_agents_description}}"
to insert the description for managed agents if there are any.For
CodeAgent
only:"{{authorized_imports}}"
to insert the list of authorized imports.
Then you can change the system prompt as follows:
This also works with the ToolCallingAgent.
4. Extra planning
We provide a model for a supplementary planning step, that an agent can run regularly in-between normal action steps. In this step, there is no tool call, the LLM is simply asked to update a list of facts it knows and to reflect on what steps it should take next based on those facts.