# 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`](https://docs.ag2.ai/0.13.2/docs/api-reference/autogen/tools/Tool/#autogen.tools.Tool.register_for_llm) to make the tool available for LLM-driven selection and [`register_for_execution`](https://docs.ag2.ai/0.13.2/docs/api-reference/autogen/tools/Tool/#autogen.tools.Tool.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:

- **Strings** — Returned as-is.
- **None** — Treated as an empty string.
- **Lists and tuples** — Serialized as JSON (e.g. `[5, 3, 10]` or `(5, 3, 10)`). Lists in OpenAI message format (list of dicts with a "type" key, such as `text` or `image_url`) are handled specially for multimodal content.
- **Dicts and primitives** — Numbers, booleans, and dicts are JSON-serialized; other types fall back to `str()` if they are not JSON-serializable.

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:

- **Enhanced Security**: Keeps secrets out of LLM interactions and telemetry.
- **Simplified Development**: Securely passes credentials without hardcoding them.
- **Flexible Integration**: Allows agents to access necessary credentials while maintaining security.

## Interoperability with External Frameworks

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

- **LangChain**: Provides numerous pre-built tools for API calls, web scraping, and more.
- **CrewAI**: Offers specialized tools for web scraping, search, and automation.
- **PydanticAI**: Supports structured data handling and dependency injection for context-driven tool execution.

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