Skip to content

Interface: ToolUseStrategy

@providerprotocol/ai


@providerprotocol/ai / @providerprotocol/ai / ToolUseStrategy

Defined in: src/types/tool.ts:257

Strategy for controlling tool execution behavior.

Provides hooks for monitoring, controlling, and transforming the tool execution loop during LLM inference.

const strategy: ToolUseStrategy = {
maxIterations: 5,
onToolCall: (tool, params) => {
console.log(`Calling ${tool.name} with`, params);
},
// Transform input parameters
onBeforeCall: (tool, params) => {
if (tool.name === 'search') {
return { proceed: true, params: { ...params, limit: 10 } };
}
return true;
},
// Transform output results
onAfterCall: (tool, params, result) => {
if (tool.name === 'fetch_data') {
return { result: sanitize(result) };
}
},
onMaxIterations: (iterations) => {
console.warn(`Reached max iterations: ${iterations}`);
}
};

optional maxIterations: number

Defined in: src/types/tool.ts:259

Maximum number of tool execution rounds (default: 10)

optional onAfterCall(tool, params, result): void | AfterCallResult | Promise<void | AfterCallResult>

Defined in: src/types/tool.ts:289

Called after tool execution completes. Can transform the result.

Tool

The tool that was executed

unknown

The parameters that were used

unknown

The result from the tool

void | AfterCallResult | Promise<void | AfterCallResult>

Void to use original result, or AfterCallResult to transform it


optional onBeforeCall(tool, params): boolean | BeforeCallResult | Promise<boolean | BeforeCallResult>

Defined in: src/types/tool.ts:279

Called before tool execution. Can skip execution or transform parameters.

Tool

The tool about to be executed

unknown

The parameters for the call

boolean | BeforeCallResult | Promise<boolean | BeforeCallResult>

One of:

  • false to skip execution
  • true to proceed with original params
  • BeforeCallResult object to control execution and optionally transform params

optional onError(tool, params, error): void | Promise<void>

Defined in: src/types/tool.ts:298

Called when a tool execution throws an error.

Tool

The tool that failed

unknown

The parameters that were used

Error

The error that was thrown

void | Promise<void>


optional onMaxIterations(iterations): void | Promise<void>

Defined in: src/types/tool.ts:305

Called when the maximum iteration limit is reached.

number

The number of iterations that were performed

void | Promise<void>


optional onToolCall(tool, params): void | Promise<void>

Defined in: src/types/tool.ts:267

Called when the model requests a tool call.

Tool

The tool being called

unknown

The parameters for the call

void | Promise<void>