Tools
Agents gain significant utility through tools as they provide access to external data, APIs, and functionality.
In AG2, using tools is done in two parts, an agent suggests which tools to use (via their LLM) and another executes the tool.
In the swarm example above, we attached tools to our agents and, as part of the swarm, AG2 created a tool executor agent to run recommended tools. Typically, you’ll create two agents, one to decide which tool to use and another to execute it.
-
Here’s the tool, a function, that we’ll attach to our agents, the
Annotated
parameter will be included in the call to the LLM so it understands what thedate_string
needs. -
The date_agent will determine whether to use the tool, using its LLM.
-
The executor_agent will run the tool and return the output as its reply.
-
Registering the tool with the agents and giving it a description to help the LLM determine.
-
We have a two-way chat, so after the date_agent the executor_agent will run and, if it sees that the date_agent suggested the use of the tool, it will execute it.
Alternatively, you can use decorators to register a tool. So, instead of using register_function
, you can register them with the function definition.