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 = LM.LoadFromModelID("qwen3:8b");
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 = LM.LoadFromModelID("qwen3:8b");
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.
- WithDefinition(AgentDefinition, Func<string, ITool>, Func<string, AgentSkill>, AgentMemory, Func<IReadOnlyList<string>, IKnowledgeRetriever>)
Configures the agent from a portable AgentDefinition, resolved exactly as an LM-Kit server resolves it (ApplyTo(AgentInvocation)): its prompt, its pinned skill's instructions, its tools, its tool-call budget, its memory, its knowledge, its output schema and, when it simulates tools, the simulation of every tool that writes.
- 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.
- WithFilters(FilterPipeline)
Sets the filter pipeline for the agent.
- WithFilters(Action<FilterPipeline>)
Configures the filter pipeline using a setup action.
- WithHumanHandoff()
Lets the agent hand the conversation to a person (HumanHandoffTool): a run that does ends as HandedOff with the agent's reason, summary and collected facts on Handoff.
- 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.
- WithKnowledge(AgentKnowledge)
Grounds the agent's answers in knowledge sources: every user input searches them (or the agent searches on demand, per Mode), the passages found are presented with citation numbers, and the run's result carries its Grounding verdict.
- 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.
- WithOutputSchema(string)
Constrains every answer of the agent to a JSON Schema, enforced while decoding: the model may still call tools, and its final answer is a JSON document that conforms to the schema.
- WithPermissionPolicy(ToolPermissionPolicy)
Sets the permission policy that governs which tools the agent may invoke.
- WithPermissionPolicy(Action<ToolPermissionPolicy>)
Configures the permission policy using a setup action.
- WithPersona(string)
Sets the agent's persona.
- WithPlanning(PlanningStrategy)
Sets the planning strategy for the agent.
- WithProposalReport(ProposalReport)
Declares where the agent's structured answer reports the changes it proposed: once a run ends, the answer's actions and status are set from the changes its dry-run tools recorded (ProposalReport), so they say what the run did. Applies to tools simulated as dry runs (DryRun).
- 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.
- WithUserQuestions()
Lets the agent ask the user a question it cannot go on without (AskUserTool): the run stops as AwaitingInput and resumes with the answer.