pub struct TokenBudgetHook { /* private fields */ }Expand description
Pre-call rate-limiting hook based on a token budget + call quota.
budget: cumulative token cap for the whole run.on_before_completionpre-checks “confirmed usage + estimated input”, returningRejectwhen exceeded;on_after_completionaccumulates the real usage.max_calls: optional maximum number of LLM calls.- Without a custom counter, input is estimated with
CharRatioCounter::new(4)(characters / 4).
§Example
ⓘ
use lc_agents::hooks::TokenBudgetHook;
let executor = AgentExecutor::new(agent, tools)
.hook(TokenBudgetHook::new(10_000).with_max_calls(20));Implementations§
Source§impl TokenBudgetHook
impl TokenBudgetHook
Sourcepub fn new(budget: usize) -> Self
pub fn new(budget: usize) -> Self
Creates a token-budget hook. budget is the cumulative token cap for the whole run.
Sourcepub fn with_max_calls(self, max_calls: usize) -> Self
pub fn with_max_calls(self, max_calls: usize) -> Self
Sets the maximum number of LLM calls.
Sourcepub fn with_counter(self, counter: Arc<dyn TokenCounter>) -> Self
pub fn with_counter(self, counter: Arc<dyn TokenCounter>) -> Self
Uses a precise token counter instead of character-ratio estimation.
Sourcepub fn tokens_used(&self) -> usize
pub fn tokens_used(&self) -> usize
Cumulative token usage (real usage from completed LLM calls).
Trait Implementations§
Source§impl AgentHook for TokenBudgetHook
impl AgentHook for TokenBudgetHook
Source§fn on_before_completion(&self, ctx: &mut CompletionContext) -> CompletionAction
fn on_before_completion(&self, ctx: &mut CompletionContext) -> CompletionAction
Called before an LLM completion. Can modify messages or reject the call.
Source§fn on_after_completion(
&self,
ctx: &mut CompletionResult,
) -> Result<(), HookError>
fn on_after_completion( &self, ctx: &mut CompletionResult, ) -> Result<(), HookError>
Called after an LLM completion. Can modify the response.
Source§fn on_before_tool_call(&self, _ctx: &mut ToolCallContext) -> ToolCallAction
fn on_before_tool_call(&self, _ctx: &mut ToolCallContext) -> ToolCallAction
Called before a tool call. Can approve, reject, modify, or skip.
Source§fn on_after_tool_call(
&self,
_ctx: &mut ToolResultContext,
) -> Result<(), HookError>
fn on_after_tool_call( &self, _ctx: &mut ToolResultContext, ) -> Result<(), HookError>
Called after a tool call. Can modify the result.
Source§fn on_stream_chunk(&self, chunk: &str) -> StreamAction
fn on_stream_chunk(&self, chunk: &str) -> StreamAction
Called for each streaming token. Can filter, replace, or forward.
Source§fn on_agent_start(&self, _input: &str) -> Result<(), HookError>
fn on_agent_start(&self, _input: &str) -> Result<(), HookError>
Called when the agent starts execution.
Source§fn on_agent_end(&self, _output: &str) -> Result<(), HookError>
fn on_agent_end(&self, _output: &str) -> Result<(), HookError>
Called when the agent finishes execution.
Source§fn on_error(&self, _error: &HookError) -> ErrorAction
fn on_error(&self, _error: &HookError) -> ErrorAction
Called when an error occurs. Can retry, ignore, or propagate.
Auto Trait Implementations§
impl !Freeze for TokenBudgetHook
impl !RefUnwindSafe for TokenBudgetHook
impl !UnwindSafe for TokenBudgetHook
impl Send for TokenBudgetHook
impl Sync for TokenBudgetHook
impl Unpin for TokenBudgetHook
impl UnsafeUnpin for TokenBudgetHook
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more