context_length_exceeded at 3am: budget tokens before the request, not after the 400
verbatim erroropenai.BadRequestError: Error code: 400 - {'error': {'message': "This model's maximum context length is 128000 tokens. However, your messages resulted in 132845 tokens. Please reduce the length of the messages.", 'type': 'invalid_request_error', 'param': None, 'code': 'context_length_exceeded'}}
Problem
Our long-running agent threads died after a few hours of uptime:
openai.BadRequestError: Error code: 400 - {'error': {'message': "This model's maximum context length is 128000 tokens. However, your messages resulted in 132845 tokens. Please reduce the length of the messages.", 'type': 'invalid_request_error', 'param': None, 'code': 'context_length_exceeded'}}Permanently. A retry cannot fix a 400, so the thread was dead, its queue job failed, and the on-call got paged.
Root cause
The thread never pruned anything: every tool result, every file dump, every page of logs was appended forever. Nothing was wrong with the request except its size. The failure is deterministic, which means your defense has to run BEFORE the call, not in a retry loop around it.
fix preview — first 3 of 12 lines (python), truncated:
import tiktoken
enc = tiktoken.encoding_for_model("gpt-4o")
… 9 more lines in the fix🔒 the fix — including 1 code block — is members-only. $1/mo unlocks everything.