Tools - AG2

Tools

Agents in AG2 leverage tools to extend their functionality, allowing them to interact with external systems, fetch real-time data, and execute complex tasks beyond the scope of a language model's internal knowledge. This enables a structured approach where agents decide which tool to use and then execute it accordingly.

How Tool Usage Works in AG2

In AG2, tool execution follows a two-step process:

  1. Selection: An agent (driven by its LLM) decides which tool is appropriate based on the given task.
  2. Execution: A separate executor agent invokes the tool and returns the results.

To ensure smooth execution, AG2 provides options to enforce the executor agent to follow the selector agent in a conversation flow.

To register tools, you can use methods like register_for_llm to make the tool available for LLM-driven selection and register_for_execution to allow the agent to execute the tool directly when needed.

Tool return types

Tool functions can return a variety of types; AG2 normalizes them to strings for use in the conversation:

This allows tools to return structured data (e.g. list[int], dict, or Pydantic models serialized to dict) without causing errors in group chats or with providers that expect string tool results.

Secure Tool Usage with Secrets

When tools require sensitive information like API keys or credentials, AG2 employs dependency injection to keep such data secure. This method ensures that private information is not exposed to the LLM while still allowing seamless execution of agent tasks. Some key benefits include:

Interoperability with External Frameworks

AG2 supports interoperability with popular LLM tool frameworks, making it easier to integrate a wide range of tools:

Each of these frameworks can be easily installed and configured to work with AG2, allowing developers to enhance their agents with powerful capabilities.