pub struct TokenEstimator;Expand description
Estimates token count for messages.
Uses a simple heuristic of ~4 characters per token, which provides a reasonable approximation for most English text and code.
For more accurate counting, consider using a tokenizer library
specific to your model (e.g., tiktoken for OpenAI models).
Implementations§
Source§impl TokenEstimator
impl TokenEstimator
Sourcepub const fn estimate_text(text: &str) -> usize
pub const fn estimate_text(text: &str) -> usize
Estimate tokens for a text string.
Sourcepub fn estimate_message(message: &Message) -> usize
pub fn estimate_message(message: &Message) -> usize
Estimate tokens for a single message.
Sourcepub fn estimate_block(block: &ContentBlock) -> usize
pub fn estimate_block(block: &ContentBlock) -> usize
Estimate tokens for a content block.
Sourcepub fn estimate_history(messages: &[Message]) -> usize
pub fn estimate_history(messages: &[Message]) -> usize
Estimate total tokens for a message history.
Auto Trait Implementations§
impl Freeze for TokenEstimator
impl RefUnwindSafe for TokenEstimator
impl Send for TokenEstimator
impl Sync for TokenEstimator
impl Unpin for TokenEstimator
impl UnwindSafe for TokenEstimator
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