pub struct TokenCounter { /* private fields */ }Expand description
Estimates token counts from text using a configurable chars-per-token ratio.
This is a heuristic estimator — real tokenization varies per model. The default ratio of 4.0 chars/token approximates GPT-family and Claude models.
§Example
use neuron_context::TokenCounter;
let counter = TokenCounter::new();
let estimate = counter.estimate_text("Hello, world!");
assert!(estimate > 0);Implementations§
Source§impl TokenCounter
impl TokenCounter
Sourcepub fn with_ratio(chars_per_token: f32) -> Self
pub fn with_ratio(chars_per_token: f32) -> Self
Creates a new TokenCounter with a custom chars-per-token ratio.
Sourcepub fn estimate_text(&self, text: &str) -> usize
pub fn estimate_text(&self, text: &str) -> usize
Estimates the number of tokens in a text string.
Sourcepub fn estimate_messages(&self, messages: &[Message]) -> usize
pub fn estimate_messages(&self, messages: &[Message]) -> usize
Estimates the total token count for a slice of messages.
Iterates all content blocks and sums their estimated token counts.
Sourcepub fn estimate_tools(&self, tools: &[ToolDefinition]) -> usize
pub fn estimate_tools(&self, tools: &[ToolDefinition]) -> usize
Estimates the total token count for a slice of tool definitions.
Trait Implementations§
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