pub struct TokenTrackingMiddleware { /* private fields */ }Expand description
Token tracking middleware that wraps an LLM to monitor usage
Implementations§
Source§impl TokenTrackingMiddleware
impl TokenTrackingMiddleware
pub fn new( config: TokenTrackingConfig, inner_model: Arc<dyn LanguageModel>, event_dispatcher: Option<Arc<EventDispatcher>>, ) -> Self
Sourcepub fn get_usage_stats(&self) -> Vec<TokenUsage>
pub fn get_usage_stats(&self) -> Vec<TokenUsage>
Get accumulated usage statistics
Sourcepub fn get_total_usage(&self) -> TokenUsageSummary
pub fn get_total_usage(&self) -> TokenUsageSummary
Get total usage summary
Sourcepub fn clear_stats(&self)
pub fn clear_stats(&self)
Clear usage statistics
Trait Implementations§
Source§impl AgentMiddleware for TokenTrackingMiddleware
impl AgentMiddleware for TokenTrackingMiddleware
Source§fn modify_model_request<'life0, 'life1, 'life2, 'async_trait>(
&'life0 self,
_ctx: &'life1 mut MiddlewareContext<'life2>,
) -> Pin<Box<dyn Future<Output = Result<()>> + Send + 'async_trait>>where
Self: 'async_trait,
'life0: 'async_trait,
'life1: 'async_trait,
'life2: 'async_trait,
fn modify_model_request<'life0, 'life1, 'life2, 'async_trait>(
&'life0 self,
_ctx: &'life1 mut MiddlewareContext<'life2>,
) -> Pin<Box<dyn Future<Output = Result<()>> + Send + 'async_trait>>where
Self: 'async_trait,
'life0: 'async_trait,
'life1: 'async_trait,
'life2: 'async_trait,
Apply middleware-specific mutations to the pending model request.
Source§fn before_tool_execution<'life0, 'life1, 'life2, 'life3, 'async_trait>(
&'life0 self,
_tool_name: &'life1 str,
_tool_args: &'life2 Value,
_call_id: &'life3 str,
) -> Pin<Box<dyn Future<Output = Result<Option<AgentInterrupt>>> + Send + 'async_trait>>where
Self: 'async_trait,
'life0: 'async_trait,
'life1: 'async_trait,
'life2: 'async_trait,
'life3: 'async_trait,
fn before_tool_execution<'life0, 'life1, 'life2, 'life3, 'async_trait>(
&'life0 self,
_tool_name: &'life1 str,
_tool_args: &'life2 Value,
_call_id: &'life3 str,
) -> Pin<Box<dyn Future<Output = Result<Option<AgentInterrupt>>> + Send + 'async_trait>>where
Self: 'async_trait,
'life0: 'async_trait,
'life1: 'async_trait,
'life2: 'async_trait,
'life3: 'async_trait,
Hook called before tool execution - can return an interrupt to pause execution. Read more
Source§impl LanguageModel for TokenTrackingMiddleware
impl LanguageModel for TokenTrackingMiddleware
Source§fn generate<'life0, 'async_trait>(
&'life0 self,
request: LlmRequest,
) -> Pin<Box<dyn Future<Output = Result<LlmResponse>> + Send + 'async_trait>>where
Self: 'async_trait,
'life0: 'async_trait,
fn generate<'life0, 'async_trait>(
&'life0 self,
request: LlmRequest,
) -> Pin<Box<dyn Future<Output = Result<LlmResponse>> + Send + 'async_trait>>where
Self: 'async_trait,
'life0: 'async_trait,
Generate a complete response (non-streaming)
Source§fn generate_stream<'life0, 'async_trait>(
&'life0 self,
request: LlmRequest,
) -> Pin<Box<dyn Future<Output = Result<ChunkStream>> + Send + 'async_trait>>where
Self: 'async_trait,
'life0: 'async_trait,
fn generate_stream<'life0, 'async_trait>(
&'life0 self,
request: LlmRequest,
) -> Pin<Box<dyn Future<Output = Result<ChunkStream>> + Send + 'async_trait>>where
Self: 'async_trait,
'life0: 'async_trait,
Generate a streaming response
Default implementation falls back to non-streaming generate()
Auto Trait Implementations§
impl Freeze for TokenTrackingMiddleware
impl !RefUnwindSafe for TokenTrackingMiddleware
impl Send for TokenTrackingMiddleware
impl Sync for TokenTrackingMiddleware
impl Unpin for TokenTrackingMiddleware
impl !UnwindSafe for TokenTrackingMiddleware
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