Skip to main content

TokenCounter

Trait TokenCounter 

Source
pub trait TokenCounter: Send + Sync {
    // Required methods
    fn count_text(&self, text: &str) -> usize;
    fn count_block(&self, block: &ModelContentBlock) -> usize;

    // Provided method
    fn count_messages(&self, messages: &[ModelMessage]) -> usize { ... }
}
Expand description

Counts the tokens a piece of conversation will cost.

Required Methods§

Source

fn count_text(&self, text: &str) -> usize

Tokens in a plain string.

Source

fn count_block(&self, block: &ModelContentBlock) -> usize

Tokens in one content block, including whatever that block type costs beyond its text.

Provided Methods§

Source

fn count_messages(&self, messages: &[ModelMessage]) -> usize

Tokens in a conversation.

Content only: per-message API framing (role, JSON punctuation) is a handful of tokens per message and is deliberately not modelled, so an implementation that ignores it agrees with the default.

Dyn Compatibility§

This trait is dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety".

Implementors§