Agent tools functions
Tools provide specialized capabilities to your agents, allowing them to perform actions and make decisions within the Group Chat environment. Similar to how real-world professionals use tools to accomplish specific tasks, AG2 agents use tools to extend their functionality beyond simple conversation.
Why Tools Matter in a Group Chat
In a multi-agent conversation, tools serve several critical purposes:
- Specialized Actions - Agents can perform domain-specific tasks like data processing, calculations, or accessing external systems
- Structured Data Exchange - Tools provide a consistent way for agents to exchange structured information
- Workflow Control - Tools help direct the conversation flow, and can determine which agent speaks next
- Enhanced Capabilities - Tools extend what agents can do, making them more powerful and useful
ReplyResult: The Key to Tool Operations
The core component of tools is the ReplyResult
object, which represents the outcome of a tool’s operation and has three key properties:
- message: The text response to be shown in the conversation
- target: (Optional) Where control should go next
- context_variables: (Optional) Updated shared state (we’ll explore this in the next section)
This simple but powerful structure allows tools to both communicate results and influence the conversation flow.
Implementing Basic Tools in a Group Chat
Creating a Simple Tool
Let’s take the triage example from the previous section and add a tool to it. In the original example, the triage agent used its LLM capabilities to decide where to route queries, but we can make this more explicit using a tool:
By adding the classify_query
function to the triage agent, we’ve given it a specific tool to use for routing queries instead of relying solely on its LLM capabilities. The function returns a ReplyResult
that provides the Group Manager with an indication of the type of query for it decide the appropriate next speaker.
Enhancing Tools with Type Annotations
We can improve our tool using type annotations and the docstring to make it more self-documenting and provide better guidance to the LLM:
The annotations and expanded docstring provide rich information about the function’s purpose and parameters, helping the LLM understand when and how to use the tool correctly.
Directing Conversation Flow with Tools
One of the most powerful aspects of tools is their ability to direct the conversation flow by specifying which agent should speak next. This creates purposeful, directed conversations rather than unpredictable exchanges.
Using the Target Parameter
The target
parameter in ReplyResult
allows a tool to specify which agent should receive control next.
Let’s update our classify_query
function to route the conversation to the appropriate agent based on the classification result:
Example
Now let’s bring it all together by extending our original example to use the tool-enhanced triage agent in a complete multi-agent workflow. In this example, the triage agent will solely rely on the classify_query
tool to classify the user query and route it to either the technical support agent or the general support agent.
Example Output
When you run the above code, you should see output similar to the following:
What’s Next?
Now that you understand how agents can perform actions using tools and direct conversation flow, the next step is to explore Context Variables. Context variables provide a shared memory for your agents, allowing them to maintain state across the conversation and make decisions based on that state.
In the Context Variables section, you’ll learn how to:
- Create and initialize context variables
- Read from and write to the shared context
- Pass information between agents
- Use context variables with tools to create more powerful workflows
Understanding context variables will provide the foundation for mastering Handoffs and Transitions, which give you precise control over the flow of conversation between agents.