Skip to main content

tool_loop

Function tool_loop 

Source
pub async fn tool_loop<Ctx: LoopDepth + Send + Sync + 'static>(
    provider: &dyn DynProvider,
    registry: &ToolRegistry<Ctx>,
    params: ChatParams,
    config: ToolLoopConfig,
    ctx: &Ctx,
) -> Result<ToolLoopResult, LlmError>
Expand description

Runs the LLM in a tool-calling loop until completion.

Each iteration:

  1. Calls provider.stream_boxed() with the current messages
  2. If the response contains tool calls, executes them via the registry
  3. Appends tool results as messages and repeats
  4. Stops when the LLM returns without tool calls, or max iterations is reached

§Depth Tracking

If Ctx implements LoopDepth, nested calls are tracked automatically. When config.max_depth is set and the context’s depth exceeds the limit, returns Err(LlmError::MaxDepthExceeded).

§Errors

Returns LlmError if:

  • The provider returns an error
  • Max depth is exceeded (returns LlmError::MaxDepthExceeded)
  • Max iterations is exceeded (returns in result with TerminationReason::MaxIterations)