For the complete documentation index, see llms.txt. This page is also available as Markdown.

MaxToolCallsMiddleware

Enforce a strict per-run cap on tool calls to prevent runaway loops

Class: bxModules.bxai.models.middleware.core.MaxToolCallsMiddleware

Caps the number of tool calls in one agent run. Useful as a hard safety boundary.

Features

  • Tracks a per-run call counter

  • Resets counter on each new run

  • Cancels execution when call cap is exceeded

Constructor

middleware = new bxModules.bxai.models.middleware.core.MaxToolCallsMiddleware(
    maxCalls: 10
)

Configuration

Option
Type
Default
Description

maxCalls

numeric

10

Maximum tool calls allowed per agent run

Hooks Used

  • beforeAgentRun (counter reset)

  • beforeToolCall (counter increment and cap enforcement)

Example

Notes

  • Exceeding the cap returns AiMiddlewareResult.cancel(...).

  • Combine with RetryMiddleware and GuardrailMiddleware for stronger control of autonomous loops.

Last updated