# 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.

```csharp
[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()`:

```csharp
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:

```csharp
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.
