{"record":{"id":"b712189041c87fae","repo":"oraios/serena","slug":"unknown-token-count-estimator-self","errorCode":null,"errorMessage":"Unknown token count estimator: {self}","messagePattern":"Unknown token count estimator: (.+?)","errorType":"exception","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"src/serena/analytics.py","lineNumber":109,"sourceCode":"    CHAR_COUNT = \"CHAR_COUNT\"\n\n    @classmethod\n    def get_valid_names(cls) -> list[str]:\n        \"\"\"\n        Get a list of all registered token count estimator names.\n        \"\"\"\n        return [estimator.name for estimator in cls]\n\n    def _create_estimator(self) -> TokenCountEstimator:\n        match self:\n            case RegisteredTokenCountEstimator.TIKTOKEN_GPT4O:\n                return TiktokenCountEstimator(model_name=\"gpt-4o\")\n            case RegisteredTokenCountEstimator.ANTHROPIC_CLAUDE_SONNET_4:\n                return AnthropicTokenCount(model_name=\"claude-sonnet-4-20250514\")\n            case RegisteredTokenCountEstimator.CHAR_COUNT:\n                return CharCountEstimator(avg_chars_per_token=4)\n            case _:\n                raise ValueError(f\"Unknown token count estimator: {self}\")\n\n    def load_estimator(self) -> TokenCountEstimator:\n        estimator_instance = _registered_token_estimator_instances_cache.get(self)\n        if estimator_instance is None:\n            estimator_instance = self._create_estimator()\n            _registered_token_estimator_instances_cache[self] = estimator_instance\n        return estimator_instance\n\n\nclass ToolUsageStats:\n    \"\"\"\n    A class to record and manage tool usage statistics.\n    \"\"\"\n\n    def __init__(self, token_count_estimator: RegisteredTokenCountEstimator = RegisteredTokenCountEstimator.TIKTOKEN_GPT4O):\n        self._token_count_estimator = token_count_estimator.load_estimator()\n        self._token_estimator_name = token_count_estimator.value\n        self._tool_stats: dict[str, ToolUsageStats.Entry] = defaultdict(ToolUsageStats.Entry)","sourceCodeStart":91,"sourceCodeEnd":127,"githubUrl":"https://github.com/oraios/serena/blob/7fcbca7e62555ec2287ddb2f083caee805848ea6/src/serena/analytics.py#L91-L127","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","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()."],"exampleFix":"// before\ncase _: raise ValueError(f\"Unknown token count estimator: {self}\")\n// after\ncase RegisteredTokenCountEstimator.MY_NEW_ESTIMATOR:\n    return MyNewEstimator()\ncase _: raise ValueError(f\"Unknown token count estimator: {self}\")","handlingStrategy":"try-catch","validationCode":"def estimator_supported(value, supported: set) -> bool:\n    return value in supported","typeGuard":null,"tryCatchPattern":"try:\n    estimator = registered_estimator.load_estimator()\nexcept ValueError as e:\n    log.warning('Falling back to CharCountEstimator: %s', e)\n    estimator = CharCountEstimator(avg_chars_per_token=4)","preventionTips":["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."],"tags":["enum","analytics","internal-invariant","telemetry"],"backgroundTag":"unhandled-enum-case","analyzedSha":"7fcbca7e62555ec2287ddb2f083caee805848ea6","analyzedAt":"2026-08-29T00:04:09.619Z","schemaVersion":2},"datasetVersion":"2026-08-29T02:17:18.158Z"}