Method Register
Register(string, Agent, bool)
Registers an agent with the specified name.
public void Register(string name, Agent agent, bool overwrite = false)
Parameters
namestringThe unique name for this agent. Used for lookup during delegation operations. Names are case-sensitive and cannot be null, empty, or whitespace.
agentAgentThe agent instance to register.
overwriteboolWhen
true, replaces any existing agent with the same name. Whenfalse(default), throws an exception if an agent with the same name already exists.
Examples
Example: Registering agents with explicit names
using LMKit.Agents;
using LMKit.Model;
var registry = new AgentRegistry();
using var model = new LM("path/to/model.gguf");
var translator = Agent.CreateBuilder()
.WithModel(model)
.WithPersona("Translator")
.WithInstruction("You translate text between languages accurately.")
.Build();
// Register with an explicit name
registry.Register("translator_en_fr", translator);
// Attempting to register again without overwrite throws
try
{
registry.Register("translator_en_fr", translator, overwrite: false);
}
catch (InvalidOperationException ex)
{
Console.WriteLine($"Registration failed: {ex.Message}");
}
// With overwrite enabled, the agent is replaced
var improvedTranslator = Agent.CreateBuilder()
.WithModel(model)
.WithPersona("Translator")
.WithInstruction("You are a professional translator with cultural awareness.")
.Build();
registry.Register("translator_en_fr", improvedTranslator, overwrite: true);
Console.WriteLine("Translator agent upgraded successfully.");
Remarks
Choose descriptive names that reflect the agent's purpose or specialization. This makes it easier to select the appropriate agent during delegation.
The overwrite parameter enables hot-swapping of agents
at runtime, which is useful for updating agent configurations without
restarting the application.
Exceptions
- ArgumentNullException
Thrown when
nameoragentisnull.- ArgumentException
Thrown when
nameis empty or consists only of whitespace.- InvalidOperationException
Thrown when an agent with the same name already exists and
overwriteisfalse.
Register(Agent, bool)
Registers an agent using its persona as the registration name.
public void Register(Agent agent, bool overwrite = false)
Parameters
agentAgentThe agent to register. Must have a non-empty persona defined in its AgentIdentity.
overwriteboolWhen
true, replaces any existing agent with the same persona. Whenfalse(default), throws an exception if an agent with the same persona is already registered.
Examples
Example: Registering agents by their personas
using LMKit.Agents;
using LMKit.Model;
var registry = new AgentRegistry();
using var model = new LM("path/to/model.gguf");
var analyst = Agent.CreateBuilder()
.WithModel(model)
.WithPersona("DataAnalyst")
.WithInstruction("You analyze data and provide actionable insights.")
.Build();
var strategist = Agent.CreateBuilder()
.WithModel(model)
.WithPersona("BusinessStrategist")
.WithInstruction("You develop comprehensive business strategies.")
.Build();
// Register using personas as names
registry.Register(analyst);
registry.Register(strategist);
// Agents can now be retrieved by their personas
var dataAgent = registry["DataAnalyst"];
var bizAgent = registry["BusinessStrategist"];
Remarks
This overload provides a convenient way to register agents when their
persona naturally serves as a unique identifier. It is equivalent to
calling Register(agent.Identity.Persona, agent, overwrite).
Ensure agents have distinct, meaningful personas when using this method to avoid naming conflicts.
Exceptions
- ArgumentNullException
Thrown when
agentisnull.- InvalidOperationException
Thrown when the agent has no persona defined, or when an agent with the same persona already exists and
overwriteisfalse.