Table of Contents

Method TryGet

Namespace
LMKit.Agents
Assembly
LM-Kit.NET.dll

TryGet(string, out Agent)

Attempts to retrieve an agent with the specified name.

public bool TryGet(string name, out Agent agent)

Parameters

name string

The name of the agent to retrieve. Case-sensitive.

agent Agent

When this method returns true, contains the agent with the specified name. When this method returns false, contains null.

Returns

bool

true if an agent with the specified name was found; otherwise, false.

Examples

Example: Safe agent retrieval with fallback

using LMKit.Agents;
using LMKit.Model;

var registry = new AgentRegistry();
// ... agents registered ...

string requestedAgent = "specialist";

if (registry.TryGet(requestedAgent, out Agent agent))
{
    var result = await agent.RunAsync("Handle this specialized task.");
    Console.WriteLine(result.Content);
}
else
{
    Console.WriteLine($"No agent named '{requestedAgent}' is available.");
    Console.WriteLine($"Available agents: {string.Join(", ", registry.Names)}");
}

Remarks

This method is the preferred way to retrieve agents when you are not certain whether the agent exists. It avoids the exception thrown by the indexer when the agent is not found.

Returns false for null, empty, or whitespace names without throwing an exception.