Interface IToolInvocationFilter
- Namespace
- LMKit.TextGeneration.Filters
- Assembly
- LM-Kit.NET.dll
A filter that intercepts tool invocations during the automatic tool-calling loop.
public interface IToolInvocationFilter
Examples
Logging every tool call and its result:
using LMKit.TextGeneration.Filters;
using System;
using System.Diagnostics;
using System.Threading.Tasks;
public sealed class ToolLoggingFilter : IToolInvocationFilter
{
public async Task OnToolInvocationAsync(
ToolInvocationFilterContext context,
Func<ToolInvocationFilterContext, Task> next)
{
Console.WriteLine($"[Tool] Calling {context.ToolCall.Name} " +
$"with args: {context.ToolCall.ArgumentsJson}");
var sw = Stopwatch.StartNew();
await next(context);
sw.Stop();
Console.WriteLine($"[Tool] {context.ToolCall.Name} completed in " +
$"{sw.ElapsedMilliseconds} ms: {context.Result?.ResultType}");
}
}
Terminating the loop when a specific result is found:
using LMKit.TextGeneration.Filters;
using System;
using System.Threading.Tasks;
public sealed class EarlyTerminationFilter : IToolInvocationFilter
{
public async Task OnToolInvocationAsync(
ToolInvocationFilterContext context,
Func<ToolInvocationFilterContext, Task> next)
{
await next(context);
// Stop if we found what we need
if (context.Result?.Content?.Contains("ANSWER_FOUND") == true)
{
context.Terminate = true;
}
}
}
Remarks
Tool invocation filters execute in a middleware (onion) pattern around each individual
tool call. They fire after the permission policy has been evaluated and the tool has been
approved, but before the actual InvokeAsync call. Code before await next(context)
runs before the tool executes; code after runs with the result available.
Common use cases:
- Logging: record every tool call, its arguments, and results.
- Rate limiting: throttle tool calls to external services.
- Result caching: return cached results for repeated tool calls.
- Error recovery: catch exceptions and provide fallback results.
- Early termination: stop the tool-calling loop when a condition is met.
Cancellation: Set Cancel to true
and do not call next to skip the tool execution.
Result override: Set Result to provide a custom result without executing the tool.
Loop termination: Set Terminate to true
to stop the model from requesting additional tool calls after this batch completes.
Methods
- OnToolInvocationAsync(ToolInvocationFilterContext, Func<ToolInvocationFilterContext, Task>)
Called when a tool is about to be invoked during the automatic tool-calling loop.