Class AgentBuilder
Fluent builder for constructing Agent instances with a chainable API.
public sealed class AgentBuilder
- Inheritance
-
AgentBuilder
- Inherited Members
Examples
Building a simple conversational agent:
using LMKit.Model;
using LMKit.Agents;
using var model = new LM("path/to/model.gguf");
var agent = new AgentBuilder()
.WithModel(model)
.WithPersona("Friendly Assistant")
.WithInstruction("Be helpful, concise, and friendly in your responses.")
.Build();
var result = agent.Run("Hello! How are you?");
Console.WriteLine(result.Content);
Building a tool-using research agent:
using LMKit.Model;
using LMKit.Agents;
using LMKit.Agents.Tools;
using var model = new LM("path/to/model.gguf");
var agent = new AgentBuilder()
.WithModel(model)
.WithIdentity(
persona: "Research Assistant",
instruction: "Search for information and synthesize findings. " +
"Always cite your sources."
)
.WithPlanning(PlanningStrategy.ReAct)
.WithTools(tools =>
{
tools.Register(new WebSearchTool());
tools.Register(new WikipediaTool());
})
.WithMaxIterations(15)
.Build();
var result = await agent.RunAsync("What are the main causes of climate change?");
Console.WriteLine(result.Content);
Remarks
The AgentBuilder provides a clean, readable way to configure complex agents. All configuration methods return the builder instance, enabling method chaining. Call Build() to create the configured agent.
Builder Pattern Benefits
- Readable, self-documenting configuration code.
- Optional parameters without constructor overload explosion.
- Configuration validation at build time.
Validation
The builder does not validate configuration until Build() is called. Use BuildAndValidate(out IReadOnlyList<string>) to get validation errors, or call Validate(out IReadOnlyList<string>) on the resulting agent.
Constructors
- AgentBuilder()
Initializes a new instance of the AgentBuilder class.
Methods
- BuildAndValidate(out IReadOnlyList<string>)
Builds the agent and validates its configuration.
- WithDelegates(AgentRegistry)
Sets the registry of delegate agents for task handoff.
- WithDelegates(Action<AgentRegistry>)
Configures delegate agents using a setup action.
- WithDelegationEnabled()
Enables automatic registration of the delegate tool when the agent is built.
- WithIdentity(AgentIdentity)
Sets the agent's identity from an existing AgentIdentity instance.
- WithIdentity(string, string)
Sets both persona and instruction for the agent.
- WithInstruction(string)
Sets the agent's base instruction.
- WithMaxIterations(int)
Sets the maximum number of iterations allowed during task execution.
- WithMemory(AgentMemory)
Sets the memory system for the agent.
- WithModel(LM)
Sets the language model for the agent.
- WithPersona(string)
Sets the agent's persona.
- WithPlanning(PlanningStrategy)
Sets the planning strategy for the agent.
- WithSkills(SkillRegistry)
Sets the skill registry for the agent.
- WithSkills(Action<SkillRegistry>)
Configures skills using a setup action.
- WithToolPolicy(ToolCallPolicy)
Sets the tool call policy for the agent.
- WithToolPolicy(Action<ToolCallPolicy>)
Configures the tool call policy using a setup action.
- WithTools(ToolRegistry)
Sets the tool registry for the agent.
- WithTools(Action<ToolRegistry>)
Configures tools using a setup action.