Share via


Using an agent as a function tool

This tutorial shows you how to use an agent as a function tool, so that one agent can call another agent as a tool.

Prerequisites

For prerequisites and installing NuGet packages, see the Create and run a simple agent step in this tutorial.

Create and use an agent as a function tool

You can use an AIAgent as a function tool by calling .AsAIFunction() on the agent and providing it as a tool to another agent. This allows you to compose agents and build more advanced workflows.

First, create a function tool as a C# method, and decorate it with descriptions if needed. This tool will be used by your agent that's exposed as a function.

using System.ComponentModel;

[Description("Get the weather for a given location.")]
static string GetWeather([Description("The location to get the weather for.")] string location)
    => $"The weather in {location} is cloudy with a high of 15°C.";

Create an AIAgent that uses the function tool.

using System;
using Azure.AI.OpenAI;
using Azure.Identity;
using Microsoft.Agents.AI;
using Microsoft.Extensions.AI;
using OpenAI;

AIAgent weatherAgent = new AzureOpenAIClient(
    new Uri("https://<myresource>.openai.azure.com"),
    new AzureCliCredential())
     .GetChatClient("gpt-4o-mini")
     .CreateAIAgent(
        instructions: "You answer questions about the weather.",
        name: "WeatherAgent",
        description: "An agent that answers questions about the weather.",
        tools: [AIFunctionFactory.Create(GetWeather)]);

Now, create a main agent and provide the weatherAgent as a function tool by calling .AsAIFunction() to convert weatherAgent to a function tool.

AIAgent agent = new AzureOpenAIClient(
    new Uri("https://<myresource>.openai.azure.com"),
    new AzureCliCredential())
     .GetChatClient("gpt-4o-mini")
     .CreateAIAgent(instructions: "You are a helpful assistant who responds in French.", tools: [weatherAgent.AsAIFunction()]);

Invoke the main agent as normal. It can now call the weather agent as a tool, and should respond in French.

Console.WriteLine(await agent.RunAsync("What is the weather like in Amsterdam?"));

This tutorial shows you how to use an agent as a function tool, so that one agent can call another agent as a tool.

Prerequisites

For prerequisites and installing packages, see the Create and run a simple agent step in this tutorial.

Create and use an agent as a function tool

You can use a ChatAgent as a function tool by calling .as_tool() on the agent and providing it as a tool to another agent. This allows you to compose agents and build more advanced workflows.

First, create a function tool that will be used by your agent that's exposed as a function.

from typing import Annotated
from pydantic import Field

def get_weather(
    location: Annotated[str, Field(description="The location to get the weather for.")],
) -> str:
    """Get the weather for a given location."""
    return f"The weather in {location} is cloudy with a high of 15°C."

Create a ChatAgent that uses the function tool.

from agent_framework.azure import AzureOpenAIChatClient
from azure.identity import AzureCliCredential

weather_agent = AzureOpenAIChatClient(credential=AzureCliCredential()).create_agent(
    name="WeatherAgent",
    description="An agent that answers questions about the weather.",
    instructions="You answer questions about the weather.",
    tools=get_weather
)

Now, create a main agent and provide the weather_agent as a function tool by calling .as_tool() to convert weather_agent to a function tool.

main_agent = AzureOpenAIChatClient(credential=AzureCliCredential()).create_agent(
    instructions="You are a helpful assistant who responds in French.",
    tools=weather_agent.as_tool()
)

Invoke the main agent as normal. It can now call the weather agent as a tool, and should respond in French.

result = await main_agent.run("What is the weather like in Amsterdam?")
print(result.text)

You can also customize the tool name, description, and argument name when converting an agent to a tool:

# Convert agent to tool with custom parameters
weather_tool = weather_agent.as_tool(
    name="WeatherLookup",
    description="Look up weather information for any location",
    arg_name="query",
    arg_description="The weather query or location"
)

main_agent = AzureOpenAIChatClient(credential=AzureCliCredential()).create_agent(
    instructions="You are a helpful assistant who responds in French.",
    tools=weather_tool
)

Next steps