Input messages exceed model context limits.
Why this happens
Total input tokens plus output allowance exceed the maximum context window.
Answer 1: Encode and truncate tokens using tiktoken
from openai import OpenAI
import tiktoken
client = OpenAI()
enc = tiktoken.get_encoding("o200k_base")
tokens = enc.encode(long_text)
max_input_tokens = 128000 - 4096
if len(tokens) > max_input_tokens:
long_text = enc.decode(tokens[:max_input_tokens])
