Skip to main content

LoopDepth

Trait LoopDepth 

Source
pub trait LoopDepth:
    Clone
    + Send
    + Sync {
    // Required methods
    fn loop_depth(&self) -> u32;
    fn with_depth(&self, depth: u32) -> Self;
}
Expand description

Trait for contexts that support automatic depth tracking in nested tool loops.

When tool_loop executes tools, LoopCore calls ctx.with_depth(ctx.loop_depth() + 1) and passes the result to tool handlers. If a tool handler then enters its own tool_loop, the depth is checked against ToolLoopConfig::max_depth and the loop is rejected if the limit is reached.

§Blanket Implementation

The unit type () has a blanket implementation that always returns depth 0 and ignores with_depth. Use this for simple cases where depth tracking isn’t needed:

use llm_stack::tool::LoopDepth;

assert_eq!(().loop_depth(), 0);
assert_eq!(().with_depth(5), ());

§Custom Implementation

For agent systems with nesting, implement this on your context type:

use llm_stack::tool::LoopDepth;

#[derive(Clone)]
struct MyContext {
    session_id: String,
    loop_depth: u32,
}

impl LoopDepth for MyContext {
    fn loop_depth(&self) -> u32 {
        self.loop_depth
    }

    fn with_depth(&self, depth: u32) -> Self {
        Self {
            loop_depth: depth,
            ..self.clone()
        }
    }
}

Required Methods§

Source

fn loop_depth(&self) -> u32

Returns the current nesting depth (0 = top-level, not nested).

Source

fn with_depth(&self, depth: u32) -> Self

Returns a new context with the specified depth.

Called internally by LoopCore::new() as ctx.with_depth(ctx.loop_depth() + 1) — tool handlers receive a context one level deeper than their parent loop.

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.

Implementations on Foreign Types§

Source§

impl LoopDepth for ()

Blanket implementation for unit type — always depth 0, no tracking.

This allows simple use cases to work without implementing the trait:

use llm_stack::tool::{ToolLoopConfig, ToolRegistry};

// Works with () context, no depth tracking
let registry: ToolRegistry<()> = ToolRegistry::new();
Source§

fn loop_depth(&self) -> u32

Source§

fn with_depth(&self, _depth: u32) -> Self

Implementors§