FoundationAgents/MetaGPT · error · NotImplementedError

num_tokens_from_messages() is not implemented for model {mod

Error message

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.

What it means

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.

Source

Thrown at metagpt/utils/token_counter.py:488

        tokens_per_name = 1
    elif model == "gpt-3.5-turbo-0301":
        tokens_per_message = 4  # every message follows <|start|>{role/name}\n{content}<|end|>\n
        tokens_per_name = -1  # if there's a name, the role is omitted
    elif "gpt-3.5-turbo" == model:
        logger.info("Warning: gpt-3.5-turbo may update over time. Returning num tokens assuming gpt-3.5-turbo-0125.")
        return count_message_tokens(messages, model="gpt-3.5-turbo-0125")
    elif "gpt-4" == model:
        logger.info("Warning: gpt-4 may update over time. Returning num tokens assuming gpt-4-0613.")
        return count_message_tokens(messages, model="gpt-4-0613")
    elif "open-llm-model" == model:
        """
        For self-hosted open_llm api, they include lots of different models. The message tokens calculation is
        inaccurate. It's a reference result.
        """
        tokens_per_message = 0  # ignore conversation message template prefix
        tokens_per_name = 0
    else:
        raise NotImplementedError(
            f"num_tokens_from_messages() is not implemented for model {model}. "
            f"See https://cookbook.openai.com/examples/how_to_count_tokens_with_tiktoken "
            f"for information on how messages are converted to tokens."
        )
    num_tokens = 0
    for message in messages:
        num_tokens += tokens_per_message
        for key, value in message.items():
            content = value
            if isinstance(value, list):
                # for gpt-4v
                for item in value:
                    if isinstance(item, dict) and item.get("type") in ["text"]:
                        content = item.get("text", "")
            num_tokens += len(encoding.encode(content))
            if key == "name":
                num_tokens += tokens_per_name
    num_tokens += 3  # every reply is primed with <|start|>assistant<|message|>

View on GitHub (pinned to 11cdf466d0)

Solutions

  1. 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.
  2. If the model is new or custom, extend token_counter.num_tokens_from_messages() with the token-counting logic for that model.
  3. 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.

Example fix

# Pass a supported model name when counting tokens, e.g.:
tokens = num_tokens_from_messages(messages, model='gpt-3.5-turbo')
# or add the new model to the supported-model branch inside num_tokens_from_messages() in metagpt/utils/token_counter.py.

When it happens

Trigger: 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.

Common situations: 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.


AI-assisted analysis of FoundationAgents/MetaGPT@11cdf466d0 (2026-08-14). Data as JSON: /api/errors/54f339c8daa6fe0f. Report an issue: GitHub.