Skip to main content

AI Agent as a function tool

Use an AI agent as a function tool, so that one agent can call another agent as a tool

Published 2 min readMarkdown
Part of seriesThe Azure Behind the Madness
AI Agent as a function tool

Agent Tools

Tools refer to capabilities or external functions that an agent can invoke to accomplish tasks it can’t do on its own (like accessing data, running code, or integrating with APIs). Tools are functions, APIs, or skills that the agent can call when reasoning through a task.

Let’s take a look at how we can use an existing AI agent as a function tool.


AI Agent as a tool

Initially, what we need to have is an agent defined. In the example below, I use an agent that generates a random base salary.

[Description("Generates a base salary")]
static int GetBaseSalary()
{
    var start = 1000;
    var end = 10000;
    var rand = new Random();
    var salary = rand.Next(start, end);
    Console.WriteLine("Generated Base Salary: " + salary);
    return salary;
}

AIAgent salaryAgent = new AzureOpenAIClient(
    endpoint,
    new AzureKeyCredential(apiKey))
     .GetChatClient(deploymentName)
     .CreateAIAgent(
        instructions: "You are an agent that generates salary.",
        name: "SalaryAgent",
        description: "An agent that generates a base salary.",
        tools: [AIFunctionFactory.Create(GetBaseSalary)]);

Next, we create a new agent and specify the existing agent as a tool by using AsAIFunction():

AIAgent salaryBonusAgent = new AzureOpenAIClient(
     endpoint,
     new AzureKeyCredential(apiKey))
     .GetChatClient(deploymentName)
     .CreateAIAgent(instructions: "You are a helpful assistant that always adds $10 bonus amount to the generated base salary.", tools: [salaryAgent.AsAIFunction()]);

Lastly, let’s run the agent with a simple prompt:

Console.WriteLine(await salaryBonusAgent.RunAsync("How much money am I getting today?"));

As we can see, we get the correct output, which added $10 to the base salary.

Generated Base Salary: 7099

Today, you are getting a total of $7,109 (which includes a $10 bonus).

This approach allows us to compose agents and build more complex workflows.

About the author

  • Expert insights on Azure AI architecture and implementation. Real-world solutions for building intelligent enterprise systems.

No comments yet. Be the first to comment.

The Azure Behind the Madness

Part 8 of 17

Explore the world of Microsoft Azure - from AI and cloud architecture to data and DevOps. The Azure Behind the Madness brings insights, stories, and hands-on guidance for building intelligent, scalable solutions in the cloud.

Up nextAgent Background Responses - Microsoft Agent FrameworkUsing background responses for handling long-running operations that may take a lot of time to complete