Class Agent
Represents an AI agent capable of executing tasks using a language model, tools, and reasoning strategies.
public class Agent
- Inheritance
-
Agent
- Inherited Members
- Extension Methods
Examples
Creating and running a simple agent:
using LMKit.Model;
using LMKit.Agents;
// Load a language model
using var model = new LM("path/to/model.gguf");
// Create an agent with an identity
var agent = new Agent(
new AgentIdentity(
persona: "Technical Documentation Writer",
instruction: "Write clear, concise documentation for software APIs."
),
model
);
// Run a task
var result = agent.Run("Explain what a REST API is in two sentences.");
if (result.IsSuccess)
{
Console.WriteLine(result.Content);
}
Creating an agent with tools using the builder:
using LMKit.Model;
using LMKit.Agents;
using LMKit.Agents.Tools;
using var model = new LM("path/to/model.gguf");
var agent = Agent.CreateBuilder(model)
.WithPersona("Research Assistant")
.WithInstruction("Help users find and summarize information.")
.WithPlanning(PlanningStrategy.ReAct)
.WithTools(tools =>
{
tools.Register(new MySearchTool());
})
.WithMaxIterations(15)
.Build();
var result = await agent.RunAsync("What are the main features of .NET 9?");
Console.WriteLine(result.Content);
Remarks
The Agent class is the core building block of the LM-Kit Agent Framework. An agent encapsulates the configuration required to accomplish tasks: a language model for inference, an identity defining its role, optional tools for external actions, and a planning strategy for complex reasoning.
Agent Components
- Model: The language model used for inference (required).
- Identity: Defines the agent's persona and behavioral instructions.
- Capabilities: Tools, skills, and memory available to the agent.
- Planning: The reasoning strategy for task decomposition.
- ToolPolicy: Rules governing when and how tools are used.
- Delegates: Other agents this agent can delegate subtasks to.
Creating Agents
For simple agents, use the constructors directly. For more complex configurations, use AgentBuilder via CreateBuilder().
Executing Agents
Call Run(string, CancellationToken) or RunAsync(string, CancellationToken) for one-off executions. For multi-turn conversations or fine-grained control, create an AgentExecutor and manage the session explicitly.
Thread Safety
Agent instances are not thread-safe. Use separate instances for concurrent execution or synchronize access externally.
Constructors
- Agent(AgentIdentity, LM)
Initializes a new instance of the Agent class with the specified identity and model.
Properties
- CanDelegate
Gets a value indicating whether this agent can delegate to other agents.
- CanUseTools
Gets a value indicating whether this agent can use tools.
- Capabilities
Gets or sets the capabilities available to this agent.
- Delegates
Gets or sets the registry of agents this agent can delegate tasks to.
- HasModel
Gets a value indicating whether this agent has a model assigned.
- Identity
Gets or sets the identity defining the agent's persona and base instruction.
- MaxIterations
Gets or sets the maximum number of iterations allowed during a single task execution.
- Model
Gets or sets the language model used by this agent for inference.
- Planning
Gets or sets the planning strategy used for reasoning and task decomposition.
- ToolPolicy
Gets or sets the policy governing tool usage during execution.
Methods
- CreateBuilder()
Creates a builder for constructing an agent with fluent configuration.
- CreateBuilder(LM)
Creates a builder initialized with the specified model.
- CreateDelegationManager()
Creates a DelegationManager for this agent.
- EnableDelegation()
Creates and registers a DelegateTool for this agent.
- EnsureSkills()
Gets the skill registry from capabilities, creating it if necessary.
- EnsureTools()
Gets the tool registry from capabilities, creating it if necessary.
- Run(string, AgentExecutionOptions, CancellationToken)
Executes the agent with the given input synchronously using the specified options.
- Run(string, CancellationToken)
Executes the agent with the given input synchronously.
- RunAsync(string, AgentExecutionOptions, CancellationToken)
Executes the agent with the given input asynchronously using the specified options.
- RunAsync(string, CancellationToken)
Executes the agent with the given input asynchronously.
- Validate(out IReadOnlyList<string>)
Validates the agent configuration, ensuring it is ready for execution.