pub trait TokenCounter:
Send
+ Sync
+ Debug {
// Required methods
fn count_tokens(&self, text: &str) -> LlmResult<u32>;
fn count_message_tokens(&self, messages: &[Value]) -> LlmResult<u32>;
fn max_context_tokens(&self) -> u32;
fn validate_token_limit(&self, text: &str) -> LlmResult<()>;
fn truncate_to_limit(
&self,
text: &str,
max_tokens: u32,
) -> LlmResult<String>;
}Expand description
Trait for counting tokens in text and messages.
Implement this trait to add support for new tokenizers.
Use TokenCounterFactory to create instances for supported providers.
§Example
use multi_llm::{TokenCounter, TokenCounterFactory};
let counter = TokenCounterFactory::create_counter("openai", "gpt-4")?;
// Count tokens
let count = counter.count_tokens("Hello, world!")?;
// Validate against limit
counter.validate_token_limit("Some text...")?;
// Truncate if needed
let truncated = counter.truncate_to_limit("Very long text...", 100)?;Required Methods§
Sourcefn count_tokens(&self, text: &str) -> LlmResult<u32>
fn count_tokens(&self, text: &str) -> LlmResult<u32>
Count tokens in a text string.
§Errors
Returns LlmError::ConfigurationError if the tokenizer
fails to encode the text.
Sourcefn count_message_tokens(&self, messages: &[Value]) -> LlmResult<u32>
fn count_message_tokens(&self, messages: &[Value]) -> LlmResult<u32>
Count tokens in a list of messages (includes formatting overhead).
The count includes tokens for role markers, message separators, and other provider-specific formatting.
Sourcefn max_context_tokens(&self) -> u32
fn max_context_tokens(&self) -> u32
Get the maximum context window size for this tokenizer.
Sourcefn validate_token_limit(&self, text: &str) -> LlmResult<()>
fn validate_token_limit(&self, text: &str) -> LlmResult<()>
Validate that text doesn’t exceed the token limit.
§Errors
Returns LlmError::TokenLimitExceeded if the text exceeds
the maximum context window.