Context window anatomy
system prompttool schemasmessage historylatest turnheadroom
- One finite token budget; no hidden state outside it.
- System prompt and tool schemas form a fixed prefix.
- Each turn appends reasoning and tool_result tokens.
- At saturation, the oldest turns are truncated: context rot.
Notes & references
Use when
Budgeting tokens across prefix, history and output.
Avoid
Eager schema loading and raw log dumps: context rot precedes the hard limit.
context = [
system_prompt, # always there
*tool_schemas, # always there
*conversation, # grows every turn
]
if count_tokens(context) > LIMIT:
# drop, shorten or summarise the oldest turns
- Effective context engineering for AI agentsAnthropic
- Context windowsAnthropic
- Context RotChroma
- Lost in the MiddleLiu et al.
- Context engineering for agentsLangChain