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>>, ) -> TokenTrackingMiddleware
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<(), Error>> + Send + 'async_trait>>where
'life0: 'async_trait,
'life1: 'async_trait,
'life2: 'async_trait,
TokenTrackingMiddleware: 'async_trait,
fn modify_model_request<'life0, 'life1, 'life2, 'async_trait>(
&'life0 self,
_ctx: &'life1 mut MiddlewareContext<'life2>,
) -> Pin<Box<dyn Future<Output = Result<(), Error>> + Send + 'async_trait>>where
'life0: 'async_trait,
'life1: 'async_trait,
'life2: 'async_trait,
TokenTrackingMiddleware: '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>, Error>> + Send + 'async_trait>>where
'life0: 'async_trait,
'life1: 'async_trait,
'life2: 'async_trait,
'life3: 'async_trait,
Self: '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>, Error>> + Send + 'async_trait>>where
'life0: 'async_trait,
'life1: 'async_trait,
'life2: 'async_trait,
'life3: 'async_trait,
Self: '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, Error>> + Send + 'async_trait>>where
'life0: 'async_trait,
TokenTrackingMiddleware: 'async_trait,
fn generate<'life0, 'async_trait>(
&'life0 self,
request: LlmRequest,
) -> Pin<Box<dyn Future<Output = Result<LlmResponse, Error>> + Send + 'async_trait>>where
'life0: 'async_trait,
TokenTrackingMiddleware: '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<Pin<Box<dyn Stream<Item = Result<StreamChunk, Error>> + Send>>, Error>> + Send + 'async_trait>>where
'life0: 'async_trait,
TokenTrackingMiddleware: 'async_trait,
fn generate_stream<'life0, 'async_trait>(
&'life0 self,
request: LlmRequest,
) -> Pin<Box<dyn Future<Output = Result<Pin<Box<dyn Stream<Item = Result<StreamChunk, Error>> + Send>>, Error>> + Send + 'async_trait>>where
'life0: 'async_trait,
TokenTrackingMiddleware: '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
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
Source§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
Converts
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
Converts
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreCreates a shared type from an unshared type.