Context and retrieval
Context Engineering
The context window holds everything the model sees: instructions, conversation history, retrieved documents, tool schemas and results, and room for the answer. Context engineering is the practice of filling that budget with the most useful tokens.
What Is Context Engineering?0:46 · intermediate Prompt Caching Explained0:46 · intermediate
A token budgetBeginner
Reserve tokens for the answer first, then allocate the rest. Use the planner below to see how instructions, documents, history, and tool definitions compete for the same window.
Lost in the middleIntermediate
Research (Liu et al., 2023, “Lost in the Middle”) found that models use information at the beginning and end of long contexts more reliably than information in the middle. Put the most important material near the start or end, and keep contexts as short as the task allows.
Conversation history and memoryIntermediate
Long conversations outgrow the window. Common strategies: keep the last N turns, summarize older turns, or store facts in a retrieval-based memory and fetch only what is relevant to the current turn.
Truncation, summarization, compressionIntermediate
Truncation drops content; summarization replaces it with a shorter version produced by a model; compression removes redundancy while keeping wording (as the optimizer does). Each loses something; test which loss your task tolerates.
Prompt cachingAdvanced
Providers can reuse the computation for a prompt prefix they have seen recently and bill those tokens at a lower cached rate. Some providers cache automatically above a minimum prompt length; others need explicit cache breakpoints. Check each provider’s documentation, and keep stable content first so the prefix matches.
Dynamic context assemblyAdvanced
Production systems build the context per request: select relevant documents, the tools the task needs, and the history that matters, then order them deliberately. PromptFlowEngine’s context budget analyzer (PromptOps Lab → Context budget) measures what each file adds.
Key takeaways
- Reserve output tokens first.
- Important content at the start or end.
- Stable prefix first, variable content last.