Adding Skills to Agents in the Microsoft AI Agent Framework
The Microsoft AI Agent Framework lets you add custom tools, policies, and expertise to agents via Skills. Skills use a progressive disclosure pattern so they stay low overhead until the agent specifically needs them.
In C#, there are three primary ways to define and provide these skills to your agents:
- File-based — Exposing a folder on the filesystem that contains a
SKILL.md file. - Code-defined — Using the
AgentInlineSkill directly within your source code. - Class-based — Encapsulating the logic, scripts, and resources within a class deriving from
AgentClassSkill<T>.
1. File-based Skills
File-based skills are loaded automatically from a folder structure. All you need is a SKILL.md file inside a folder. The system will inject the YAML frontmatter describing the skill so the agent knows when it might be relevant.
Full Example (SKILL.md)
---
name: car-rental-policy
description: Guidelines and policies for renting vehicles. Use when asked about mileage limits, insurance, or fuel policies.
---
# Car Rental Policy
- Insurance: Basic coverage is included. Premium is $15/day extra.
- Mileage: 200 miles per day free. $0.25 per additional mile.
- Fuel: Return with a full tank to avoid a $40 refuel fee.
Full Example (CarRentalFileSkillDemo.cs)
using System;
using System.IO;
using System.Threading.Tasks;
using Microsoft.Agents.AI;
using Azure.Identity;
using OpenAI;
using System.ClientModel;
using Azure.AI.OpenAI;
namespace MicrosoftAgentFrameworkLesson.ConsoleApp.Agents;
public class CarRentalFileSkillDemo
{
public static async Task RunAsync()
{
Console.WriteLine("--- Car Rental File-Based Skill Demo ---");
// The path to the parent directory containing skill folders
string skillPath = Path.Combine(AppContext.BaseDirectory, "Agents", "Skills");
// Provide the file-based skills
var skillsProvider = new AgentSkillsProvider(skillPath);
// Assume standard endpoint/credentials setup (skipped for brevity, but needed for completeness)
string endpoint = Environment.GetEnvironmentVariable("AZURE_OPENAI_ENDPOINT") ?? "https://placeholder.openai.azure.com/";
var agent = new AzureOpenAIClient(new Uri(endpoint), new DefaultAzureCredential())
.GetChatClient("gpt-4o-mini")
.AsAIAgent(new ChatClientAgentOptions
{
Name = "CarRentalAgent",
ChatOptions = new()
{
Instructions = "You are a helpful car rental assistant.",
},
AIContextProviders = [skillsProvider] // Inject the skill!
});
Console.WriteLine("Agent configured with file-based skill successfully.");
await Task.CompletedTask;
}
}
2. Code-defined (Inline) Skills
Code-defined skills use the AgentInlineSkill class. This approach is highly dynamic and flexible, meaning you don't need external files to define resources or policy blocks.
Full Example (CarRentalInlineSkillDemo.cs)
using System;
using System.Threading.Tasks;
using Microsoft.Agents.AI;
using Azure.Identity;
using Azure.AI.OpenAI;
namespace MicrosoftAgentFrameworkLesson.ConsoleApp.Agents;
public class CarRentalInlineSkillDemo
{
public static async Task RunAsync()
{
Console.WriteLine("--- Car Rental Inline Skill Demo ---");
// Define a code-based skill inline
var pricingSkill = new AgentInlineSkill(
name: "car-pricing-skill",
description: "Calculates total cost for car rentals",
instructions: """
Use this skill to determine pricing for a requested car category.
1. Check the pricing-table resource for the daily rate.
2. Multiply the rate by the numbers of days requested.
""")
.AddResource(
"pricing-table",
"""
| Category | Daily Rate |
|----------|------------|
| Compact | $30 |
| Sedan | $45 |
| SUV | $65 |
""");
var skillsProvider = new AgentSkillsProvider(pricingSkill);
string endpoint = Environment.GetEnvironmentVariable("AZURE_OPENAI_ENDPOINT") ?? "https://placeholder.openai.azure.com/";
var agent = new AzureOpenAIClient(new Uri(endpoint), new DefaultAzureCredential())
.GetChatClient("gpt-4o-mini")
.AsAIAgent(new ChatClientAgentOptions
{
Name = "CarRentalAgent",
ChatOptions = new() { Instructions = "You are a helpful car rental computing assistant." },
AIContextProviders = [skillsProvider]
});
Console.WriteLine("Agent configured with inline skill successfully.");
await Task.CompletedTask;
}
}
3. Class-based Skills
Using the AgentClassSkill<T> generic type allows developers to bind attributes to methods and properties easily, establishing clear structural ties directly within C# source files. Properties act as resources read by the agent, and methods become script executions triggered by the agent.
Full Example (CarRentalClassSkillDemo.cs)
using System;
using System.ComponentModel;
using System.Threading.Tasks;
using Microsoft.Agents.AI;
using Azure.Identity;
using Azure.AI.OpenAI;
namespace MicrosoftAgentFrameworkLesson.ConsoleApp.Agents;
internal sealed class CarRentalAvailabilitySkill : AgentClassSkill<CarRentalAvailabilitySkill>
{
public override AgentSkillFrontmatter Frontmatter { get; } = new(
"car-availability",
"Checks availability of specific cars in the rental lot. Use when users ask if a particular model is available.");
protected override string Instructions => """
Use this skill to check if a specific car or category is available.
1. Read the inventory-status resource to see what cars are ready.
""";
[AgentSkillResource("inventory-status")]
[Description("Current list of available cars to rent.")]
public string InventoryStatus => """
# Current Available Inventory
- 2x Toyota Corolla (Sedan)
- 1x Honda CR-V (SUV)
- 0x Ford Mustang (Compact) - OUT OF STOCK
""";
}
public class CarRentalClassSkillDemo
{
public static async Task RunAsync()
{
Console.WriteLine("--- Car Rental Class-based Skill Demo ---");
var skill = new CarRentalAvailabilitySkill();
var skillsProvider = new AgentSkillsProvider(skill);
string endpoint = Environment.GetEnvironmentVariable("AZURE_OPENAI_ENDPOINT") ?? "https://placeholder.openai.azure.com/";
var agent = new AzureOpenAIClient(new Uri(endpoint), new DefaultAzureCredential())
.GetChatClient("gpt-4o-mini")
.AsAIAgent(new ChatClientAgentOptions
{
Name = "CarRentalAgent",
ChatOptions = new() { Instructions = "You are a car availability specialist." },
AIContextProviders = [skillsProvider]
});
Console.WriteLine("Agent configured with class-based skill successfully.");
await Task.CompletedTask;
}
}
Reference: Microsoft Agent Framework - Skills