Strange error: This model's maximum context length is 4097 tokens. However, you requested 4097 tokens

The API throws an error when the context length is at the maximum.
The limit was 4096 before, now it’s 4097.
It doesn’t matter what the system_prompt, max_tokens or user prompt are.
As soon as the total num_tokens_from_messages equals the maximum, the API throws an error.
Tested with model = “gpt-3.5-turbo”.

The workaround is easy, just make sure you don’t exceed 4096 tokens. But it appears that there’s an indexing error or comparison error in the API.
I had the error before with 4096 tokens (and reported the bug last week). It was driving me crazy, but now I started testing again, and the ‘workaround’ of OpenAI to raise the limit by 1 will help with my old code, but it’s still off.

Another question: is the cookbook’s example for num_tokens_from_messages at How to format inputs to ChatGPT models | OpenAI Cookbook still up-to-date with the latest version of gpt-3.5-turbo, or has it changed? Because from my testing it calculates exactly 10 tokens less than expected, for a complete messages object.

Error message:
openai.error.InvalidRequestError: This model’s maximum context length is 4097 tokens. However, you requested 4097 tokens (1647 in the messages, 2450 in the completion). Please reduce the length of the messages or completion.

Example message below, if you remove one number from user content or two words from system content, it will work again:

{
“model”: “gpt-3.5-turbo”,
“temperature”: 0.2,
“max_tokens”: 16,
“messages”: [
{
“role”: “system”,
“content”: “I am system prompt”
},
{
“role”: “user”,
“content”: “4097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974097409740974”
}
]
}

2 Likes

I can confirm this error. I use ‘gpt-3-encoder’ to determine length of prompt and then switch between models based on remaining tokens versus the intended response length.
I also receive the following error;

{
  message: "This model's maximum context length is 4097 tokens. However, you requested 4097 tokens (999 in the messages, 3098 in the completion). Please reduce the length of the messages or completion.",
  type: 'invalid_request_error',
  param: 'messages',
  code: 'context_length_exceeded'
}

If you are sending messages to the chat completions endpoints for the 3.5-turbo or gpt-4 endpoints that the gpt-3-encoder is not the correct token model to use, it is now cl100K_base and can be selected in the tiktoken API,

from tiktoken import get_encoding
tokenizer = get_encoding("cl100k_base")

Good to know, thanks for the update on tokens - I had missed that