oraios/serena · error · ValueError
Unknown token count estimator: {self}
Error message
Unknown token count estimator: {self} What it means
RegisteredTokenCountEstimator._create_estimator() maps each registered estimator enum value to a concrete TokenCountEstimator implementation; an unmatched value raises ValueError. This should be unreachable unless a new enum member was added without a corresponding case in _create_estimator — i.e. an internal consistency bug, not user error.
Source
Thrown at src/serena/analytics.py:109
CHAR_COUNT = "CHAR_COUNT"
@classmethod
def get_valid_names(cls) -> list[str]:
"""
Get a list of all registered token count estimator names.
"""
return [estimator.name for estimator in cls]
def _create_estimator(self) -> TokenCountEstimator:
match self:
case RegisteredTokenCountEstimator.TIKTOKEN_GPT4O:
return TiktokenCountEstimator(model_name="gpt-4o")
case RegisteredTokenCountEstimator.ANTHROPIC_CLAUDE_SONNET_4:
return AnthropicTokenCount(model_name="claude-sonnet-4-20250514")
case RegisteredTokenCountEstimator.CHAR_COUNT:
return CharCountEstimator(avg_chars_per_token=4)
case _:
raise ValueError(f"Unknown token count estimator: {self}")
def load_estimator(self) -> TokenCountEstimator:
estimator_instance = _registered_token_estimator_instances_cache.get(self)
if estimator_instance is None:
estimator_instance = self._create_estimator()
_registered_token_estimator_instances_cache[self] = estimator_instance
return estimator_instance
class ToolUsageStats:
"""
A class to record and manage tool usage statistics.
"""
def __init__(self, token_count_estimator: RegisteredTokenCountEstimator = RegisteredTokenCountEstimator.TIKTOKEN_GPT4O):
self._token_count_estimator = token_count_estimator.load_estimator()
self._token_estimator_name = token_count_estimator.value
self._tool_stats: dict[str, ToolUsageStats.Entry] = defaultdict(ToolUsageStats.Entry)View on GitHub (pinned to 7fcbca7e62)
Solutions
- Upgrade Serena to a version that implements the requested estimator in _create_estimator().
- Change the token_count_estimator setting (in serena_config.yml) to a supported value, e.g. tiktoken-gpt-4o, anthropic-claude-sonnet-4, or char-count.
- If you added the enum member yourself, add a matching 'case' returning a TokenCountEstimator subclass in _create_estimator().
Example fix
// before
case _: raise ValueError(f"Unknown token count estimator: {self}")
// after
case RegisteredTokenCountEstimator.MY_NEW_ESTIMATOR:
return MyNewEstimator()
case _: raise ValueError(f"Unknown token count estimator: {self}") Defensive patterns
Strategy: try-catch
Validate before calling
def estimator_supported(value, supported: set) -> bool:
return value in supported Try / catch
try:
estimator = registered_estimator.load_estimator()
except ValueError as e:
log.warning('Falling back to CharCountEstimator: %s', e)
estimator = CharCountEstimator(avg_chars_per_token=4) Prevention
- Keep the token_count_estimator config value to one of the documented registered names.
- When adding a new RegisteredTokenCountEstimator member, always add the matching case in _create_estimator (and a test via load_estimator).
- Upgrade Serena and config in lockstep to avoid referencing estimators unknown to the installed code.
When it happens
Trigger: Calling load_estimator() (directly or via analytics token counting) with a RegisteredTokenCountEstimator value that has no case in _create_estimator — typically after upgrading Serena or adding a custom/registered estimator enum member.
Common situations: A version mismatch where config/analytics references an estimator newly added upstream while running older code; contributor added a RegisteredTokenCountEstimator member but forgot the match arm; corrupted/patched enum value.
Related errors
- Unknown language backend '{backend_str}': valid values are {
- Invalid line_ending: {value!r}. Valid values are: {valid}
- Invalid language server: '{orig_language_str}'.\nValid value
- Unhandled comment normalisation: {comment_normalisation}
AI-assisted analysis of oraios/serena@7fcbca7e62 (2026-08-29).
Data as JSON: /api/errors/b712189041c87fae.
Report an issue: GitHub.