Table of Contents

Class ToolInvocationFilterContext

Namespace
LMKit.TextGeneration.Filters
Assembly
LM-Kit.NET.dll

Provides contextual information to IToolInvocationFilter implementations.

public sealed class ToolInvocationFilterContext
Inheritance
ToolInvocationFilterContext
Inherited Members

Examples

Caching tool results:

public async Task OnToolInvocationAsync(
    ToolInvocationFilterContext context,
    Func<ToolInvocationFilterContext, Task> next)
{
    string cacheKey = $"{context.ToolCall.Name}:{context.ToolCall.ArgumentsJson}";

    if (_cache.TryGetValue(cacheKey, out var cached))
    {
        context.Result = cached;
        return; // Skip actual tool execution
    }

    await next(context);

    if (context.Result?.ResultType == ToolCallResultType.Success)
    {
        _cache[cacheKey] = context.Result;
    }
}

Remarks

The context is created for each individual tool call within the automatic tool-calling loop. Filters can inspect the tool call details, cancel execution, override the result, or terminate the entire tool-calling loop.

Execution order within one LLM response: When the model requests multiple tool calls in a single response, each call gets its own context with ToolIndex indicating its position in the batch and ToolCount indicating the total.

Properties

Cancel

Gets or sets a value indicating whether to cancel this tool invocation.

CancellationToken

Gets the cancellation token for this operation.

PermissionResult

Gets the permission result from the ToolPermissionPolicy, or null if no policy is configured.

Properties

Gets the properties dictionary for passing arbitrary state between filters.

RequestIndex

Gets the zero-based index of the current LLM request cycle in the tool-calling loop.

Result

Gets or sets the tool call result.

Terminate

Gets or sets a value indicating whether to terminate the tool-calling loop after the current batch of tool calls completes.

Tool

Gets the tool instance that will be invoked.

ToolCall

Gets the tool call requested by the model.

ToolCount

Gets the total number of tool calls in the current batch.

ToolIndex

Gets the zero-based index of this tool call within the current batch.