Priority order: (1) system prompt, (2) the immediate question + retrieved top-K, (3) recent conversation, (4) older turns. Trimming strategy: keep recent N turns verbatim, summarize older turns. Recency bias is real; the model gives more weight to the last 2-4K tokens.
Compose: system (200 tok) + top-K (1500 tok @ 300 each) + last_5_turns (~2000 tok) = ~3700 tok.
Trim: when total > budget, summarize older turns into a single “summary_of_earlier” message.
Drop: extraneous tool outputs that the answer doesn’t reference.
Always pre-compute token estimates (o200k_base) before sending.
system 200
top-K 1500
recent 2000
---
total ~4000 tokens (gpt-4o has 128K but quality drops past ~32K)
Budget the context yourself. Long prompts degrade quality; trim and summarize aggressively.