pub struct HeuristicTokenCounter { /* private fields */ }Expand description
Heuristic token counter using character-based estimation.
Uses the approximation: tokens ≈ characters / 4, with a 10% safety margin plus additional overhead for message metadata (role, timestamps, etc.).
This is intentionally conservative to avoid underestimating token usage.
Implementations§
Source§impl HeuristicTokenCounter
impl HeuristicTokenCounter
Sourcepub fn new(
chars_per_token: f64,
safety_margin: f64,
metadata_overhead: u32,
) -> HeuristicTokenCounter
pub fn new( chars_per_token: f64, safety_margin: f64, metadata_overhead: u32, ) -> HeuristicTokenCounter
Create a new heuristic counter with custom parameters.
Sourcepub fn with_defaults() -> HeuristicTokenCounter
pub fn with_defaults() -> HeuristicTokenCounter
Create with default parameters (chars/4 + 10% margin + 10 metadata overhead).
Trait Implementations§
Source§impl Clone for HeuristicTokenCounter
impl Clone for HeuristicTokenCounter
Source§fn clone(&self) -> HeuristicTokenCounter
fn clone(&self) -> HeuristicTokenCounter
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 HeuristicTokenCounter
impl Debug for HeuristicTokenCounter
Source§impl Default for HeuristicTokenCounter
impl Default for HeuristicTokenCounter
Source§fn default() -> HeuristicTokenCounter
fn default() -> HeuristicTokenCounter
Returns the “default value” for a type. Read more
Source§impl TokenCounter for HeuristicTokenCounter
impl TokenCounter for HeuristicTokenCounter
Source§fn count_message(&self, message: &Message) -> u32
fn count_message(&self, message: &Message) -> u32
Count tokens in a single message.
Source§fn count_text(&self, text: &str) -> u32
fn count_text(&self, text: &str) -> u32
Count tokens in a plain text string.
Source§fn count_messages(&self, messages: &[Message]) -> u32
fn count_messages(&self, messages: &[Message]) -> u32
Count tokens in multiple messages.
Auto Trait Implementations§
impl Freeze for HeuristicTokenCounter
impl RefUnwindSafe for HeuristicTokenCounter
impl Send for HeuristicTokenCounter
impl Sync for HeuristicTokenCounter
impl Unpin for HeuristicTokenCounter
impl UnsafeUnpin for HeuristicTokenCounter
impl UnwindSafe for HeuristicTokenCounter
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