pub struct TokenCounter { /* private fields */ }Expand description
Estimates token counts using the cl100k_base tokenizer (GPT-4 / Claude
compatible). Accuracy is within ~5% across non-OpenAI providers; the budget
is a soft guarantee.
Implementations§
Source§impl TokenCounter
impl TokenCounter
Sourcepub fn new() -> Result<Self, PackError>
pub fn new() -> Result<Self, PackError>
Create a new token counter backed by the cl100k_base encoding.
§Errors
Returns PackError::Io if the tokenizer cannot be loaded (should only
happen if the tiktoken-rs data files are missing).
Sourcepub fn count_bytes(&self, text: &[u8]) -> usize
pub fn count_bytes(&self, text: &[u8]) -> usize
Count the number of tokens in a byte slice, handling non-UTF-8 data gracefully via lossy conversion.
Auto Trait Implementations§
impl Freeze for TokenCounter
impl RefUnwindSafe for TokenCounter
impl Send for TokenCounter
impl Sync for TokenCounter
impl Unpin for TokenCounter
impl UnsafeUnpin for TokenCounter
impl UnwindSafe for TokenCounter
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