Sub topic 4
Choosing what to include and what to leave out
This chapter assumes you know the parts of a context (system prompt, tools, history, retrieved data, user input) and the idea of a token budget. It covers how to decide which candidate items earn a place in the window on a given call.
Relevance depends on the current step
Relevance is a property of an item and a specific step, not of the item alone. A refund policy is essential when the user asks for a refund. It is noise when the same agent is formatting a shipping label. Select for the step the model is about to perform.
Fitting under the budget is not enough. Each extra item competes for the model's attention, so an item that is merely "possibly useful" has a cost. The default for borderline items is to leave them out and load them later if needed.
Three questions for each candidate
Ask these in order for every item you might include:
- Does the model need this to produce the next output? If not, drop it.
- Is this the most compact form that still carries the needed facts? If not, shorten it.
- Could the model fetch it later if it turns out to be needed? If so, consider leaving it out for now.
A minimal selector
This sketch scores each document by how many of the question's words it contains, drops anything under a threshold, and keeps the top few. Real systems use embeddings or a reranker. The structure stays the same: score, threshold, cap.
def score(query, text):
q = set(query.lower().split())
t = set(text.lower().split())
return len(q & t) / len(q) if q else 0.0
def select(query, candidates, min_score=0.2, max_items=3):
scored = [(score(query, c), c) for c in candidates]
kept = [(s, c) for s, c in scored if s >= min_score]
kept.sort(key=lambda pair: pair[0], reverse=True)
return [c for _, c in kept[:max_items]]
docs = [
"Reset your password from the account settings page.",
"Our office is closed on public holidays.",
"Password rules: at least 12 characters.",
]
print(select("how do I reset my password", docs))The output is ['Reset your password from the account settings page.']. The holiday notice scores 0. The password-rules document scores about 0.17, so the threshold removes it even though it is loosely related.
The threshold and the cap do different jobs. The threshold removes items that are not relevant enough. The cap limits how many relevant items can crowd out everything else. Use both.
Common errors
- Including everything "just in case." Long contexts raise cost and latency, and irrelevant text can pull the model off task. The next chapter covers this failure mode (distraction).
- Ranking only by similarity. A passage can be similar to the question and still not help. Check that top results answer the question rather than merely mention its words.
- A fixed top-k for every query. Some questions need one document and others need five. Combine a score threshold with a cap instead of always taking exactly k.
- Cutting the wrong thing under pressure. When the window is tight, trim low-scoring retrieved items first. Do not trim the system prompt or the user's question.
- Never checking what was dropped. Log which items were selected and which were rejected. Without that log you cannot tell whether a bad answer came from missing evidence or from bad reasoning.
Exercise
A retriever returns the top 5 documents for every question, regardless of score. Users report that answers to simple questions sometimes include unrelated details. Which two changes to the selection step would you make, and why?
Show answer
Add a minimum relevance score so that weak matches are dropped, and keep a cap on the number of items so that strong matches cannot crowd out other context. Unrelated details appear because low-scoring documents are admitted just to fill the top 5. A threshold lets simple questions get one or two documents, while the cap still bounds the cost for broad questions.