This model's maximum context length is N tokens
The request is longer than the model can hold. Your prompt, the whole conversation history and the space reserved for the answer all count. The history is usually the culprit, 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. It is no limit on your message alone. Three things share it: everything you sent, everything the model 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 actually send, not what you believe you send. In most failing scripts the system prompt and the last user message are small and the accumulated history is huge.
Then reserve output space. With max_tokens at 4000 and a limit of 128000, your input has to fit in 124000. Forget this and you get the same error at a length that looks like it should fit.
Then trim. Keep the system prompt and the last few exchanges, squeeze the middle into a few lines and drop the rest. A rolling summary beats cutting from the front, which throws away the decisions the conversation was built on.
On Ollama the cause differs. The error looks the same but the culprit is usually num_ctx, which defaults low whatever the model supports. Set it in the Modelfile instead of trimming a document that would have fitted.
For information only, not professional advice. You use it at your own risk and we are not liable for any loss or damage. See the disclaimer.
Tagged