Method RunAsync
RunAsync(string, CancellationToken)
Executes the agent with the given input asynchronously.
public Task<AgentExecutionResult> RunAsync(string input, CancellationToken cancellationToken = default)
Parameters
inputstringThe user input or task prompt.
cancellationTokenCancellationTokenA token to cancel the operation.
Returns
- Task<AgentExecutionResult>
A task that resolves to the execution result.
Examples
Running an async task with cancellation:
using LMKit.Model;
using LMKit.Agents;
using var model = new LM("path/to/model.gguf");
var agent = new Agent(model);
agent.Identity = new AgentIdentity("Analyst", "Provide detailed analysis.");
using var cts = new CancellationTokenSource(TimeSpan.FromMinutes(1));
try
{
var result = await agent.RunAsync(
"Analyze the pros and cons of remote work.",
cts.Token
);
Console.WriteLine(result.Content);
}
catch (OperationCanceledException)
{
Console.WriteLine("Task was cancelled.");
}
Remarks
Creates a temporary AgentExecutor for this execution. For multi-turn conversations or repeated executions, create and reuse an executor directly for better performance.
Exceptions
- ArgumentNullException
Thrown when
inputis null or empty.- InvalidOperationException
Thrown when the agent is not properly configured.
RunAsync(string, AgentExecutionOptions, CancellationToken)
Executes the agent with the given input asynchronously using the specified options.
public Task<AgentExecutionResult> RunAsync(string input, AgentExecutionOptions options, CancellationToken cancellationToken = default)
Parameters
inputstringThe user input or task prompt.
optionsAgentExecutionOptionsExecution options, or null to use defaults.
cancellationTokenCancellationTokenA token to cancel the operation.
Returns
- Task<AgentExecutionResult>
A task that resolves to the execution result.
Examples
Running with options and inspecting tool calls:
using LMKit.Model;
using LMKit.Agents;
using var model = new LM("path/to/model.gguf");
var agent = new Agent(model);
agent.EnsureTools().Register(new MySearchTool());
agent.Planning = PlanningStrategy.ReAct;
var options = new AgentExecutionOptions
{
MaxIterations = 10,
Timeout = TimeSpan.FromMinutes(2)
};
var result = await agent.RunAsync("Find recent news about AI.", options);
Console.WriteLine($"Response: {result.Content}");
Console.WriteLine($"Tool calls made: {result.ToolCalls.Count}");
Console.WriteLine($"Inference count: {result.InferenceCount}");
foreach (var toolCall in result.ToolCalls)
{
Console.WriteLine($" - {toolCall.ToolName}: {toolCall.ResultType}");
}