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, it passes a context with incremented depth so that nested loops can enforce depth limits via max_depth in ToolLoopConfig.

§Blanket Implementation

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

use llm_stack::tool::LoopDepth;

// () always returns 0, ignores depth changes
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.

A depth of 0 means this is the top-level loop (not nested).

Source

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

Returns a new context with the specified depth.

Called by tool_loop when passing context to tool handlers, incrementing depth for any nested loops.

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§