Agentic AIC#verifiedVerified

Tool Use Agent Pattern in C#

Augment an LLM with callable external tools — APIs, code interpreters, databases — so it can take actions and retrieve real-time information beyond its training data.

How to Implement the Tool Use Agent Pattern in C#

1Step 1: Define the tool interface and tool registry

public interface ITool
{
    string Name { get; }
    string Description { get; }
    Task<string> ExecuteAsync(string input);
}

public class ToolRegistry
{
    private readonly Dictionary<string, ITool> _tools = [];

2Step 2: Register and look up tools by name

    public void Register(ITool tool) => _tools[tool.Name] = tool;

    public ITool? Get(string name) =>
        _tools.GetValueOrDefault(name);

    public IReadOnlyList<string> ListNames() =>
        _tools.Keys.ToList().AsReadOnly();
}

3Step 3: LLM decides which tool to call

public record ToolCall(string ToolName, string Input);

public static class ToolUseLoop
{
    public static async Task<string> RunAsync(
        string query,
        ToolRegistry registry,
        Func<string, IReadOnlyList<string>, Task<ToolCall?>> llm)
    {
        var toolNames = registry.ListNames();

        // LLM picks a tool (or null for direct answer)
        var call = await llm(query, toolNames);
        if (call is null) return "Direct answer (no tool needed)";

        var tool = registry.Get(call.ToolName)
            ?? throw new InvalidOperationException(
                $"Unknown tool: {call.ToolName}");

        return await tool.ExecuteAsync(call.Input);
    }
}

Tool Use Agent Pattern Architecture

hourglass_empty

Rendering diagram...

lightbulb

Tool Use Agent Pattern in the Real World

A lawyer (the LLM) in a courtroom knows the law but needs a paralegal team (the tools) to pull case files, run searches, and retrieve exhibits. The lawyer directs which file to fetch, the paralegal returns it, and the lawyer integrates that information into their argument — the lawyer's intelligence is amplified by the support staff's ability to reach into the real world.