Tool Calling Agents

Tool Calling Agents are the second type of agent available in smolagents. They utilize built-in tool-calling methods and generate tool calls in JSON format. While smolagents primarily focuses on CodeAgents and recommends using them, it also supports ToolCallingAgents.

Code vs JSON Actions

How Do Tool Calling Agents Work?

This agent type follows the same multi-step workflow as CodeAgent. If you have any doubts, refer to the previous section. Just like CodeAgents, the LLM generates and executes actions, often involving external tool calls. However, in this case, the actions are structured as JSON/text blobs, specifying tool names and arguments. The system then parses these instructions to determine which tool to execute.

Example: Running a Tool Calling Agent

To illustrate, let’s run the same example as before, but this time using ToolCallingAgent. This allows us to compare their differences. Notice that we only change the agent type, while the framework handles the rest automatically:

from smolagents import ToolCallingAgent, DuckDuckGoSearchTool, HfApiModel

agent = ToolCallingAgent(tools=[DuckDuckGoSearchTool()], model=HfApiModel())

agent.run("How many seconds would it take for a leopard at full speed to run through Pont des Arts?")

If you check the trace generated by the agent, you won’t see the Executing parsed code: output. Instead, you’ll see something like:

Calling tool: 'web_search' with arguments: {'query': 'leopard full speed and Pont des Arts length'}

Here, the agent generates a structured tool call that needs to be processed to produce the actual output, rather than directly generating and executing code as in CodeAgent.

Further Reading

< > Update on GitHub