This model's maximum context length is N tokens
The request is longer than the model can hold, counting your prompt, the whole conversation history and the space reserved for the answer. Usually it is the history, not the prompt.
Tested on GPT-5 ·
This model's maximum context length is 128000 tokens. However, your messages resulted in 131515 tokens. Please reduce the length of the messages.The number in the message is the total budget, not the limit on your message. Three things share it: everything you sent, everything the model has already said in this conversation, and the room set aside for the reply.
The usual cause is a chat loop that appends every turn and never trims. It works for twenty messages and fails on the twenty-first, which is why it looks sudden.
Notes from the author
Check the history first. Print the token count of what you are actually sending, not what you think you are sending. In most failing scripts the system prompt and the last user message are small and the accumulated history is enormous.
Then reserve output space. If max_tokens is 4000 and the limit is 128000, your input has to fit in 124000. Leaving this out produces the same error at a length that looks like it should fit.
Then trim. Keep the system prompt and the last few exchanges, summarise the middle into a few lines and drop the rest. A rolling summary works better than truncating from the front, which cuts away the decisions the conversation was built on.
On Ollama this is different. The error is the same but the cause is usually num_ctx, which defaults low regardless of what the model supports. Set it in the Modelfile rather than trimming a document that would have fitted.
Tagged