Skip to content

'num_tokens_from_messages() is not implemented for model gpt-5-nano #228

@egeres

Description

@egeres

I get:

Exception has occurred: KeyError
'num_tokens_from_messages() is not implemented for model gpt-5-nano.
See https://github.com/openai/openai-python/blob/main/chatml.md for how messages are converted to tokens.'
  File "/mnt/c/Github/NLP/sketch_cost_5.py", line 55, in <module>
    prompt_cost = calculate_prompt_cost(prompt, model)
                  ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
KeyError: 'num_tokens_from_messages() is not implemented for model gpt-5-nano.
See https://github.com/openai/openai-python/blob/main/chatml.md for how messages are converted to tokens.'

With:

from openai import OpenAI

client = OpenAI()
model = "gpt-5-nano"
prompt = [{ "role": "user", "content": "Say this is a test"}]

chat_completion = client.chat.completions.create(messages=prompt, model=model)

completion = chat_completion.choices[0].message.content
# "This is a test."

prompt_cost = calculate_prompt_cost(prompt, model)
completion_cost = calculate_completion_cost(completion, model)
print(f"{prompt_cost} + {completion_cost} = {prompt_cost + completion_cost}")
# 0.0000180 + 0.000010 = 0.0000280

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions