{"record":{"id":"940b94773d0ea547","repo":"langchain-ai/langchain","slug":"invalid-token-counter-shortcut-token-counter","errorCode":null,"errorMessage":"Invalid token_counter shortcut '{token_counter}'. Available shortcuts: {available_shortcuts}.","messagePattern":"Invalid token_counter shortcut '(.+?)'\\. Available shortcuts: (.+?)\\.","errorType":"exception","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"libs/core/langchain_core/messages/utils.py","lineNumber":1463,"sourceCode":"    if include_system and strategy == \"first\":\n        msg = \"include_system parameter is only valid with strategy='last'\"\n        raise ValueError(msg)\n\n    messages = convert_to_messages(messages)\n\n    # Handle string shortcuts for token counter\n    if isinstance(token_counter, str):\n        if token_counter in _TOKEN_COUNTER_SHORTCUTS:\n            actual_token_counter = _TOKEN_COUNTER_SHORTCUTS[token_counter]\n        else:\n            available_shortcuts = \", \".join(\n                f\"'{key}'\" for key in _TOKEN_COUNTER_SHORTCUTS\n            )\n            msg = (\n                f\"Invalid token_counter shortcut '{token_counter}'. \"\n                f\"Available shortcuts: {available_shortcuts}.\"\n            )\n            raise ValueError(msg)\n    else:\n        # Type narrowing: at this point token_counter is not a str\n        actual_token_counter = token_counter  # type: ignore[assignment]\n\n    if hasattr(actual_token_counter, \"get_num_tokens_from_messages\"):\n        list_token_counter = actual_token_counter.get_num_tokens_from_messages\n    elif callable(actual_token_counter):\n        if (\n            next(\n                iter(inspect.signature(actual_token_counter).parameters.values())\n            ).annotation\n            is BaseMessage\n        ):\n\n            def list_token_counter(messages: Sequence[BaseMessage]) -> int:\n                return sum(actual_token_counter(msg) for msg in messages)  # type: ignore[arg-type, misc]\n\n        else:","sourceCodeStart":1445,"sourceCodeEnd":1481,"githubUrl":"https://github.com/langchain-ai/langchain/blob/e32fa9a52eab3b61ad7a45399bfde59b3e580fc4/libs/core/langchain_core/messages/utils.py#L1445-L1481","documentation":"Raised by `trim_messages` when `token_counter` is given as a string that is not in the `_TOKEN_COUNTER_SHORTCUTS` registry. String shortcuts exist for a small set of built-in token counters; any other string is rejected with the list of valid options in the message.","triggerScenarios":"Calling `trim_messages(msgs, token_counter='gpt4')` when the shortcut is actually e.g. 'gpt-3.5-turbo'/'gpt-4o' style names defined in the registry; typos or provider names that were never registered.","commonSituations":"Assuming any model name works as a shortcut; using a shortcut removed or added in a different langchain-core version; copying a shortcut name from stale docs.","solutions":["Use one of the shortcuts listed in the error message exactly","Or pass a callable: `token_counter=len` or a function `lambda msg: my_tokenizer(msg.content)`","Or pass a model object that implements `get_num_tokens_from_messages`"],"exampleFix":"# before\ntrim_messages(msgs, max_tokens=500, token_counter='gpt4')\n\n# after\ntrim_messages(msgs, max_tokens=500, token_counter=len)  # or a valid shortcut name from the error message","handlingStrategy":"validation","validationCode":"from langchain_core.messages.utils import _TOKEN_COUNTER_SHORTCUTS\n\ndef valid_shortcut(name: str) -> bool:\n    return name in _TOKEN_COUNTER_SHORTCUTS\n\nif isinstance(token_counter, str) and not valid_shortcut(token_counter):\n    token_counter = len  # or raise your own config error","typeGuard":"def is_trim_token_counter(tc: object) -> bool:\n    if isinstance(tc, str):\n        from langchain_core.messages.utils import _TOKEN_COUNTER_SHORTCUTS\n        return tc in _TOKEN_COUNTER_SHORTCUTS\n    return callable(tc) or hasattr(tc, 'get_num_tokens_from_messages')","tryCatchPattern":null,"preventionTips":["Prefer passing a callable counter over string shortcuts for stability across versions","Validate shortcut names against the error's listed options when loading config"],"tags":["trim-messages","token-counting","argument-validation"],"backgroundTag":null,"analyzedSha":"e32fa9a52eab3b61ad7a45399bfde59b3e580fc4","analyzedAt":"2026-08-14T18:42:09.092Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}