pub struct TokenCounter {
pub tokenizer: BpeApproxTokenizer,
pub model_contexts: HashMap<String, usize>,
}Expand description
High-level token counter for a specific model/family combination.
Fields§
§tokenizer: BpeApproxTokenizerUnderlying BPE approximation tokeniser.
model_contexts: HashMap<String, usize>Per-model context window sizes (tokens).
Implementations§
Source§impl TokenCounter
impl TokenCounter
Sourcepub fn new(family: TokenizerFamily) -> Self
pub fn new(family: TokenizerFamily) -> Self
Create a counter for the given tokeniser family, pre-populated with context windows for all major models.
Sourcepub fn count(&self, text: &str) -> TokenCount
pub fn count(&self, text: &str) -> TokenCount
Count tokens in a single text string.
Sourcepub fn count_messages(&self, messages: &[(String, String)]) -> TokenCount
pub fn count_messages(&self, messages: &[(String, String)]) -> TokenCount
Count tokens across a list of (role, content) message pairs.
Each message incurs a ~4-token overhead for the role marker and formatting tokens used by chat APIs.
Sourcepub fn count_with_system(
&self,
system: &str,
messages: &[(String, String)],
) -> TokenCount
pub fn count_with_system( &self, system: &str, messages: &[(String, String)], ) -> TokenCount
Count tokens for a system prompt plus a message list.
Sourcepub fn remaining_context(&self, model: &str, used: usize) -> Option<usize>
pub fn remaining_context(&self, model: &str, used: usize) -> Option<usize>
How many tokens remain in model’s context window after used tokens.
Returns None if the model is not in the built-in table.
Sourcepub fn batch_count(&self, texts: &[&str]) -> Vec<TokenCount>
pub fn batch_count(&self, texts: &[&str]) -> Vec<TokenCount>
Count tokens for each text in texts, returning one TokenCount per entry.
Sourcepub fn model_context_window(model: &str) -> Option<usize>
pub fn model_context_window(model: &str) -> Option<usize>
Look up the context window (in tokens) for a known model.
Returns None for unrecognised model identifiers.
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
impl<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
§impl<T> FutureExt for T
impl<T> FutureExt for T
§fn with_context(self, otel_cx: Context) -> WithContext<Self>
fn with_context(self, otel_cx: Context) -> WithContext<Self>
§fn with_current_context(self) -> WithContext<Self>
fn with_current_context(self) -> WithContext<Self>
§impl<T> Instrument for T
impl<T> Instrument for T
§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
Source§impl<T> IntoRequest<T> for T
impl<T> IntoRequest<T> for T
Source§fn into_request(self) -> Request<T>
fn into_request(self) -> Request<T>
T in a tonic::Request