pub struct UsageMetadataCallbackHandler { /* private fields */ }Expand description
Callback Handler that tracks AIMessage.usage_metadata.
This handler collects token usage metadata from chat model responses, aggregating the usage by model name. It is thread-safe and can be used across multiple concurrent LLM calls.
§Example
ⓘ
use agent_chain_core::callbacks::UsageMetadataCallbackHandler;
use std::sync::Arc;
let handler = UsageMetadataCallbackHandler::new();
// Use with a callback manager
let mut manager = CallbackManager::new();
manager.add_handler(Arc::new(handler.clone()), true);
// After LLM calls complete
let usage = handler.usage_metadata();
for (model, metadata) in usage.iter() {
println!("{}: {} tokens", model, metadata.total_tokens);
}Implementations§
Source§impl UsageMetadataCallbackHandler
impl UsageMetadataCallbackHandler
Sourcepub fn usage_metadata(&self) -> HashMap<String, UsageMetadata>
pub fn usage_metadata(&self) -> HashMap<String, UsageMetadata>
Get the collected usage metadata.
Returns a clone of the current usage metadata map, keyed by model name.
Trait Implementations§
Source§impl BaseCallbackHandler for UsageMetadataCallbackHandler
impl BaseCallbackHandler for UsageMetadataCallbackHandler
Source§fn name(&self) -> &str
fn name(&self) -> &str
Get a unique name for this handler.
Note: This is a Rust-specific addition for debugging purposes.
Source§fn raise_error(&self) -> bool
fn raise_error(&self) -> bool
Whether to raise an error if an exception occurs.
Source§fn run_inline(&self) -> bool
fn run_inline(&self) -> bool
Whether to run the callback inline.
Source§fn ignore_llm(&self) -> bool
fn ignore_llm(&self) -> bool
Whether to ignore LLM callbacks.
Source§fn ignore_retry(&self) -> bool
fn ignore_retry(&self) -> bool
Whether to ignore retry callbacks.
Source§fn ignore_chain(&self) -> bool
fn ignore_chain(&self) -> bool
Whether to ignore chain callbacks.
Source§fn ignore_agent(&self) -> bool
fn ignore_agent(&self) -> bool
Whether to ignore agent callbacks.
Source§fn ignore_retriever(&self) -> bool
fn ignore_retriever(&self) -> bool
Whether to ignore retriever callbacks.
Source§fn ignore_chat_model(&self) -> bool
fn ignore_chat_model(&self) -> bool
Whether to ignore chat model callbacks.
Source§fn ignore_custom_event(&self) -> bool
fn ignore_custom_event(&self) -> bool
Whether to ignore custom events.
Source§impl CallbackManagerMixin for UsageMetadataCallbackHandler
impl CallbackManagerMixin for UsageMetadataCallbackHandler
Source§fn on_llm_start(
&mut self,
serialized: &HashMap<String, Value>,
prompts: &[String],
run_id: Uuid,
parent_run_id: Option<Uuid>,
tags: Option<&[String]>,
metadata: Option<&HashMap<String, Value>>,
)
fn on_llm_start( &mut self, serialized: &HashMap<String, Value>, prompts: &[String], run_id: Uuid, parent_run_id: Option<Uuid>, tags: Option<&[String]>, metadata: Option<&HashMap<String, Value>>, )
Run when LLM starts running.
Source§fn on_chat_model_start(
&mut self,
serialized: &HashMap<String, Value>,
messages: &[Vec<BaseMessage>],
run_id: Uuid,
parent_run_id: Option<Uuid>,
tags: Option<&[String]>,
metadata: Option<&HashMap<String, Value>>,
)
fn on_chat_model_start( &mut self, serialized: &HashMap<String, Value>, messages: &[Vec<BaseMessage>], run_id: Uuid, parent_run_id: Option<Uuid>, tags: Option<&[String]>, metadata: Option<&HashMap<String, Value>>, )
Run when a chat model starts running.
Source§fn on_retriever_start(
&mut self,
serialized: &HashMap<String, Value>,
query: &str,
run_id: Uuid,
parent_run_id: Option<Uuid>,
tags: Option<&[String]>,
metadata: Option<&HashMap<String, Value>>,
)
fn on_retriever_start( &mut self, serialized: &HashMap<String, Value>, query: &str, run_id: Uuid, parent_run_id: Option<Uuid>, tags: Option<&[String]>, metadata: Option<&HashMap<String, Value>>, )
Run when the Retriever starts running.
Source§impl ChainManagerMixin for UsageMetadataCallbackHandler
impl ChainManagerMixin for UsageMetadataCallbackHandler
Source§fn on_chain_end(
&mut self,
outputs: &HashMap<String, Value>,
run_id: Uuid,
parent_run_id: Option<Uuid>,
)
fn on_chain_end( &mut self, outputs: &HashMap<String, Value>, run_id: Uuid, parent_run_id: Option<Uuid>, )
Run when chain ends running.
Source§fn on_chain_error(
&mut self,
error: &dyn Error,
run_id: Uuid,
parent_run_id: Option<Uuid>,
)
fn on_chain_error( &mut self, error: &dyn Error, run_id: Uuid, parent_run_id: Option<Uuid>, )
Run when chain errors.
Source§impl Clone for UsageMetadataCallbackHandler
impl Clone for UsageMetadataCallbackHandler
Source§fn clone(&self) -> UsageMetadataCallbackHandler
fn clone(&self) -> UsageMetadataCallbackHandler
Returns a duplicate of the value. Read more
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
Performs copy-assignment from
source. Read moreSource§impl Debug for UsageMetadataCallbackHandler
impl Debug for UsageMetadataCallbackHandler
Source§impl LLMManagerMixin for UsageMetadataCallbackHandler
impl LLMManagerMixin for UsageMetadataCallbackHandler
Source§fn on_llm_end(
&mut self,
response: &ChatResult,
_run_id: Uuid,
_parent_run_id: Option<Uuid>,
)
fn on_llm_end( &mut self, response: &ChatResult, _run_id: Uuid, _parent_run_id: Option<Uuid>, )
Run when LLM ends running.
Source§impl RunManagerMixin for UsageMetadataCallbackHandler
impl RunManagerMixin for UsageMetadataCallbackHandler
Auto Trait Implementations§
impl Freeze for UsageMetadataCallbackHandler
impl RefUnwindSafe for UsageMetadataCallbackHandler
impl Send for UsageMetadataCallbackHandler
impl Sync for UsageMetadataCallbackHandler
impl Unpin for UsageMetadataCallbackHandler
impl UnwindSafe for UsageMetadataCallbackHandler
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> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
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> PolicyExt for Twhere
T: ?Sized,
impl<T> PolicyExt for Twhere
T: ?Sized,
Source§impl<T> ToStringFallible for Twhere
T: Display,
impl<T> ToStringFallible for Twhere
T: Display,
Source§fn try_to_string(&self) -> Result<String, TryReserveError>
fn try_to_string(&self) -> Result<String, TryReserveError>
ToString::to_string, but without panic on OOM.