fix(providers): provider-aware token counting#255
Merged
Conversation
Token counting was hardcoded to tiktoken/gpt-4 regardless of the configured provider, so Anthropic (direct, Bedrock, Bedrock Mantle), Gemini, and local-model providers all chunked and accounted using the OpenAI tokenizer. ChatProvider now exposes count_tokens() with a chars/4 default heuristic. OpenAI/Azure override with tiktoken; Anthropic and Bedrock Mantle use a 3.5 chars/token heuristic; Bedrock, Ollama, vLLM and llama.cpp dispatch on the configured model id against a per-family ratio table (Llama 2/3, Mistral/Mixtral, Qwen, DeepSeek, Gemma/Gemini, Phi, Cohere, Titan) sourced from the Llama 3 paper, Mistral docs, Qwen2 tech report, Google token docs and HF tokenizer configs. split_snippet and process_diff_file accept a token_counter callable; ReviewGraph and ReviewService pass llm_provider.count_tokens. The embedding-usage callback keeps the model-name dispatch in utils.count_tokens since it only sees a model id string.
mpekatsoula
approved these changes
Jun 22, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Token counting was hardcoded to tiktoken/gpt-4 regardless of the configured provider, so Anthropic (direct, Bedrock, Bedrock Mantle), Gemini, and local-model providers all chunked and accounted using the OpenAI tokenizer.
ChatProvider now exposes count_tokens() with a chars/4 default heuristic. OpenAI/Azure override with tiktoken; Anthropic and Bedrock Mantle use a 3.5 chars/token heuristic; Bedrock, Ollama, vLLM and llama.cpp dispatch on the configured model id against a per-family ratio table (Llama 2/3, Mistral/Mixtral, Qwen, DeepSeek, Gemma/Gemini, Phi, Cohere, Titan) sourced from the Llama 3 paper, Mistral docs, Qwen2 tech report, Google token docs and HF tokenizer configs.
split_snippet and process_diff_file accept a token_counter callable; ReviewGraph and ReviewService pass llm_provider.count_tokens. The embedding-usage callback keeps the model-name dispatch in utils.count_tokens since it only sees a model id string.