pub struct AnthropicTokenCounter { /* private fields */ }Expand description
Token counter for Anthropic Claude models.
Uses cl100k_base tokenizer with a 1.1x approximation factor, since Claude’s actual tokenizer isn’t publicly available. This provides conservative estimates (slightly over-counting).
§Context Windows
| Model | Context Window |
|---|---|
| claude-3-5-sonnet | 200K |
| claude-3-opus | 200K |
| claude-3-haiku | 200K |
| claude-2.x | 100K |
§Example
use multi_llm::{AnthropicTokenCounter, TokenCounter};
let counter = AnthropicTokenCounter::new("claude-3-5-sonnet-20241022")?;
let tokens = counter.count_tokens("Hello, world!")?;§Accuracy Note
Token counts are approximate. The 1.1x factor provides a safety margin to avoid accidentally exceeding context limits.
Implementations§
Trait Implementations§
Source§impl Debug for AnthropicTokenCounter
impl Debug for AnthropicTokenCounter
Source§impl TokenCounter for AnthropicTokenCounter
impl TokenCounter for AnthropicTokenCounter
Source§fn 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). Read more
Source§fn max_context_tokens(&self) -> u32
fn max_context_tokens(&self) -> u32
Get the maximum context window size for this tokenizer.
Auto Trait Implementations§
impl Freeze for AnthropicTokenCounter
impl RefUnwindSafe for AnthropicTokenCounter
impl Send for AnthropicTokenCounter
impl Sync for AnthropicTokenCounter
impl Unpin for AnthropicTokenCounter
impl UnwindSafe for AnthropicTokenCounter
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