Skip to main content

Overview

Tools extend agent capabilities by allowing them to execute code, query databases, call APIs, and interact with external systems. The .NET SDK provides AITool and AIFunction abstractions for defining and using tools with agents.

Core Concepts

AIFunction

AIFunction represents a callable function that an agent can invoke. Functions are created from .NET methods with:
  • Parameters: Strongly-typed parameters with descriptions
  • Return Values: Typed return values
  • Metadata: Name, description, and parameter information
  • Execution: Automatic parameter binding and invocation

AITool

AITool is the base abstraction that represents any tool an agent can use. AIFunction is a specific type of AITool.

Creating Functions

Using AIFunctionFactory

The simplest way to create functions is using AIFunctionFactory.Create():
Use [Description] attributes from System.ComponentModel to provide descriptions for the function and parameters. These help the AI model understand when and how to use the function.

Async Functions

Functions can be asynchronous:

Complex Parameters

Functions can accept complex types:

Adding Tools to Agents

At Agent Creation

Per-Request Tools

Add tools for specific requests using AgentRunOptions:

With ChatClientAgentOptions

Function Execution

Automatic Invocation

By default, when an agent decides to call a function, the framework automatically:
  1. Extracts parameters from the model’s response
  2. Invokes the function with the parameters
  3. Returns the result to the model
  4. Continues the conversation

Manual Function Approval

For sensitive operations, require human approval before execution:

Human-in-the-Loop

Learn about implementing approval workflows

Function Examples

Database Query Function

API Call Function

File System Function

Calculator Function

Agents as Tools

Convert an agent into a tool that can be used by other agents:

Multi-Agent Systems

Learn about building multi-agent architectures

Function Calling with Streaming

Function calls work with streaming responses:

Custom Function Metadata

Customize function metadata with AIFunctionFactoryOptions:

Best Practices

Use descriptive [Description] attributes for functions and parameters. This helps the AI model understand when and how to use the function.
Functions should handle errors and return meaningful messages:
Support cancellation for long-running operations:
Validate parameters before execution:
Each function should do one thing well. Break complex operations into multiple focused functions.
  • Validate and sanitize all inputs
  • Implement authorization checks
  • Require approval for destructive operations
  • Limit file system and network access
  • Never expose sensitive credentials

Tool Configuration

Function Choice Control

Control how the model uses functions:

Parallel Function Calls

Some models support calling multiple functions in parallel:

Next Steps

Middleware

Add middleware for function approval and logging

Workflows

Orchestrate agents with tools in workflows

MCP Tools

Use Model Context Protocol tools

RAG

Combine tools with retrieval-augmented generation