Class AgentRegistry
A collection that manages named agents for lookup and delegation scenarios.
public sealed class AgentRegistry : IEnumerable<Agent>, IEnumerable
- Inheritance
-
AgentRegistry
- Implements
- Inherited Members
Examples
Example: Building a multi-agent delegation system
This example demonstrates creating a registry of specialized agents that a coordinator agent can delegate tasks to based on the nature of the request.
using LMKit.Agents;
using LMKit.Model;
// Load a language model
using var model = new LM("path/to/model.gguf");
// Create specialized agents
var codeAgent = Agent.CreateBuilder()
.WithModel(model)
.WithPersona("CodeAssistant")
.WithInstruction("You are an expert programmer who writes clean, efficient code.")
.Build();
var researchAgent = Agent.CreateBuilder()
.WithModel(model)
.WithPersona("Researcher")
.WithInstruction("You analyze topics thoroughly and provide well-sourced information.")
.Build();
var writerAgent = Agent.CreateBuilder()
.WithModel(model)
.WithPersona("Writer")
.WithInstruction("You craft compelling, well-structured written content.")
.Build();
// Register agents in the registry
var registry = new AgentRegistry();
registry.Register("code", codeAgent);
registry.Register("research", researchAgent);
registry.Register(writerAgent); // Uses persona "Writer" as the name
// Look up agents by name
if (registry.TryGet("code", out var programmer))
{
var result = await programmer.RunAsync("Write a binary search function in C#.");
Console.WriteLine(result.Content);
}
// Enumerate all registered agents
Console.WriteLine($"Available agents ({registry.Count}):");
foreach (string name in registry.Names)
{
Console.WriteLine($" - {name}");
}
Example: Dynamic agent registration with overwrite capability
This example shows how to update an existing agent registration when agent configurations need to change at runtime.
using LMKit.Agents;
using LMKit.Model;
var registry = new AgentRegistry();
using var model = new LM("path/to/model.gguf");
// Initial registration
var basicAgent = Agent.CreateBuilder()
.WithModel(model)
.WithPersona("Assistant")
.Build();
registry.Register("helper", basicAgent);
// Later, upgrade to a more capable configuration
var enhancedAgent = Agent.CreateBuilder()
.WithModel(model)
.WithPersona("Assistant")
.WithInstruction("You are a highly capable assistant with advanced reasoning.")
.WithPlanning(PlanningStrategy.ReAct)
.Build();
// Overwrite the existing registration
registry.Register("helper", enhancedAgent, overwrite: true);
Console.WriteLine($"Registry contains 'helper': {registry.Contains("helper")}");
Remarks
AgentRegistry provides a centralized mechanism for registering, retrieving, and managing agents by name. This is particularly useful in multi-agent systems where a primary agent needs to delegate tasks to specialized sub-agents based on their capabilities.
Agent names serve as unique identifiers within the registry. Names are case-sensitive and compared using ordinal string comparison. When registering an agent without an explicit name, the agent's persona (from its AgentIdentity) is used as the key.
Thread Safety
This class is not thread-safe for concurrent modifications. If multiple threads need to
modify the registry simultaneously, synchronize access externally using a lock or other
concurrency primitive.
Properties
- Agents
Gets a read-only collection of all registered agents.
- Count
Gets the number of agents currently registered in this registry.
- this[string]
Gets the agent registered with the specified name.
- Names
Gets a read-only collection of all registered agent names.
Methods
- Clear()
Removes all agents from the registry.
- Contains(string)
Determines whether an agent with the specified name is registered.
- GetEnumerator()
Returns an enumerator that iterates through the registered agents.
- GetNames()
Gets a snapshot of all registered agent names.
- Register(Agent, bool)
Registers an agent using its persona as the registration name.
- Register(string, Agent, bool)
Registers an agent with the specified name.
- Remove(string)
Removes the agent with the specified name from the registry.
- TryGet(string, out Agent)
Attempts to retrieve an agent with the specified name.