{"record":{"id":"54f339c8daa6fe0f","repo":"FoundationAgents/MetaGPT","slug":"num-tokens-from-messages-is-not-implemented-for","errorCode":null,"errorMessage":"num_tokens_from_messages() is not implemented for model {model}. See https://cookbook.openai.com/examples/how_to_count_tokens_with_tiktoken for information on how messages are converted to tokens.","messagePattern":"num_tokens_from_messages\\(\\) is not implemented for model (.+?)\\. See https://cookbook\\.openai\\.com/examples/how_to_count_tokens_with_tiktoken for information on how messages are converted to tokens\\.","errorType":"exception","errorClass":"NotImplementedError","httpStatus":null,"severity":"error","filePath":"metagpt/utils/token_counter.py","lineNumber":488,"sourceCode":"        tokens_per_name = 1\n    elif model == \"gpt-3.5-turbo-0301\":\n        tokens_per_message = 4  # every message follows <|start|>{role/name}\\n{content}<|end|>\\n\n        tokens_per_name = -1  # if there's a name, the role is omitted\n    elif \"gpt-3.5-turbo\" == model:\n        logger.info(\"Warning: gpt-3.5-turbo may update over time. Returning num tokens assuming gpt-3.5-turbo-0125.\")\n        return count_message_tokens(messages, model=\"gpt-3.5-turbo-0125\")\n    elif \"gpt-4\" == model:\n        logger.info(\"Warning: gpt-4 may update over time. Returning num tokens assuming gpt-4-0613.\")\n        return count_message_tokens(messages, model=\"gpt-4-0613\")\n    elif \"open-llm-model\" == model:\n        \"\"\"\n        For self-hosted open_llm api, they include lots of different models. The message tokens calculation is\n        inaccurate. It's a reference result.\n        \"\"\"\n        tokens_per_message = 0  # ignore conversation message template prefix\n        tokens_per_name = 0\n    else:\n        raise NotImplementedError(\n            f\"num_tokens_from_messages() is not implemented for model {model}. \"\n            f\"See https://cookbook.openai.com/examples/how_to_count_tokens_with_tiktoken \"\n            f\"for information on how messages are converted to tokens.\"\n        )\n    num_tokens = 0\n    for message in messages:\n        num_tokens += tokens_per_message\n        for key, value in message.items():\n            content = value\n            if isinstance(value, list):\n                # for gpt-4v\n                for item in value:\n                    if isinstance(item, dict) and item.get(\"type\") in [\"text\"]:\n                        content = item.get(\"text\", \"\")\n            num_tokens += len(encoding.encode(content))\n            if key == \"name\":\n                num_tokens += tokens_per_name\n    num_tokens += 3  # every reply is primed with <|start|>assistant<|message|>","sourceCodeStart":470,"sourceCodeEnd":506,"githubUrl":"https://github.com/FoundationAgents/MetaGPT/blob/11cdf466d042aece04fc6cfd13b28e1a70341b1f/metagpt/utils/token_counter.py#L470-L506","documentation":"Error \"num_tokens_from_messages() is not implemented for model {model}. See https://cookbook.openai.com/examples/how_to_count_tokens_with_tiktoken for information on how messages are converted to tokens.\" thrown in FoundationAgents/MetaGPT.","triggerScenarios":"Raised when num_tokens_from_messages() is called with a model whose name is not matched by any supported prefix (gpt-3.5, gpt-4, gpt-4o, o1, open-llm-model, etc.) in metagpt/utils/token_counter.py.","commonSituations":"Configuring a custom, newly released, or self-hosted model name not in the supported list; a typo in the model name in config2.yaml; using a deployment/alias name instead of the underlying model name.","solutions":["Use a model supported by num_tokens_from_messages() (e.g. a gpt-3.5-turbo or gpt-4 variant recognized by tiktoken), or map the model to a supported tokenizer.","If the model is new or custom, extend token_counter.num_tokens_from_messages() with the token-counting logic for that model.","See https://cookbook.openai.com/examples/how_to_count_tokens_with_tiktoken for how messages are converted to tokens and implement the conversion for your model."],"exampleFix":"# Pass a supported model name when counting tokens, e.g.:\ntokens = num_tokens_from_messages(messages, model='gpt-3.5-turbo')\n# or add the new model to the supported-model branch inside num_tokens_from_messages() in metagpt/utils/token_counter.py.","handlingStrategy":null,"validationCode":null,"typeGuard":null,"tryCatchPattern":null,"preventionTips":[],"tags":[],"backgroundTag":null,"analyzedSha":"11cdf466d042aece04fc6cfd13b28e1a70341b1f","analyzedAt":"2026-08-14T23:20:02.994Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}