How do multi-step agents work?
The ReAct framework (Yao et al., 2022) is currently the main approach to building agents.
The name is based on the concatenation of two words, βReasonβ and βAct.β Indeed, agents following this architecture will solve their task in as many steps as needed, each step consisting of a Reasoning step, then an Action step where it formulates tool calls that will bring it closer to solving the task at hand.
All agents in smolagents
are based on singular MultiStepAgent
class, which is an abstraction of ReAct framework.
On a basic level, this class performs actions on a cycle of following steps, where existing variables and knowledge is incorporated into the agent logs like below:
Initialization: the system prompt is stored in a SystemPromptStep
, and the user query is logged into a TaskStep
.
While loop (ReAct loop):
- Use
agent.write_inner_memory_from_logs()
to write the agent logs into a list of LLM-readable chat messages. - Send these messages to a
Model
object to get its completion. Parse the completion to get the action (a JSON blob forToolCallingAgent
, a code snippet forCodeAgent
). - Execute the action and logs result into memory (an
ActionStep
). - At the end of each step, we run all callback functions defined in
agent.step_callbacks
.
Optionally, when planning is activated, a plan can be periodically revised and stored in a PlanningStep
. This includes feeding facts about the task at hand to the memory.
For a CodeAgent
, it looks like the figure below.
Here is a video overview of how that works:
We implement two versions of agents:
- CodeAgent is the preferred type of agent: it generates its tool calls as blobs of code.
- ToolCallingAgent generates tool calls as a JSON in its output, as is commonly done in agentic frameworks. We incorporate this option because it can be useful in some narrow cases where you can do fine with only one tool call per step: for instance, for web browsing, you need to wait after each action on the page to monitor how the page changes.
We also provide an option to run agents in one-shot: just pass single_step=True
when launching the agent, like agent.run(your_task, single_step=True)
Read Open-source LLMs as LangChain Agents blog post to learn more about multi-step agents.