Creates a new TokenCounter instance.
Token counter configuration
Count tokens for Anthropic/Claude models via API.
Count tokens for Gemini models via API.
Count tokens for OpenAI models using local tiktoken.
Estimate tokens offline using character-based heuristic. Uses ~4 characters per token as a general approximation.
Estimate tokens for a specific provider.
Clear the token count cache.
Get current cache statistics.
Frees resources (tiktoken encoder). Call this when done with the counter.
Universal token counter supporting multiple providers.
Provides accurate token counting via provider APIs (Anthropic, Gemini) or local tiktoken (OpenAI), with fallback to character-based estimation.
Example