Sub topic 6
Where to put things in the context
This chapter assumes you know what a context window is, how tokens are budgeted, and how a context divides into system prompt, tools, history, retrieved data, and user input. It also assumes you can read short Python code. Here you decide the order of that content, because order changes how well the model uses it.
Why placement matters
A model reads the whole context, but it does not weigh every position equally. Two effects are widely reported for long contexts:
- Content at the very start and the very end tends to be used more reliably than content buried in the middle.
- Instructions that are far from the point where the model starts answering are more likely to be missed or diluted.
The size of these effects varies by model and by context length. Treat them as tendencies to test, not guarantees.
A practical ordering
A layout that works for most tasks:
- System rules and tool descriptions first. They change rarely and set the frame for everything else.
- Reference documents next. Put the longest, least critical material in the middle.
- Conversation history after that. Recent turns sit closer to the question.
- The user's question last. The model begins generating right after it.
- A short reminder of critical constraints at the end, if they are easy to lose.
Repeating one or two critical rules at the end costs few tokens. Repeating many rules wastes budget and can create clashes if the copies drift apart.
Ordering documents by relevance
Within the document section, the same logic applies. If you retrieve ten passages, do not leave the best one in the middle. Place the strongest evidence at the edges of the block.
def order_for_edges(ranked_docs):
# ranked_docs is best-first. Put the best first, the second best last,
# and the weakest in the middle, where attention tends to be lowest.
front, back = [], []
for i, doc in enumerate(ranked_docs):
if i % 2 == 0:
front.append(doc)
else:
back.append(doc)
return front + list(reversed(back))
docs = ["best", "second", "third", "fourth", "fifth"]
print(order_for_edges(docs))This prints ['best', 'third', 'fifth', 'fourth', 'second']. The best passage opens the block and the second best closes it.
Mark the boundaries
Placement works better when the model can tell sections apart. Wrap each section in clear labels or tags, so that a document that contains the words "ignore the above" is not confused with your instructions.
def labeled_prompt(rules, docs, question):
doc_block = "\n".join(
f"<document index=\"{i}\">\n{d}\n</document>" for i, d in enumerate(docs, 1)
)
return (
f"<rules>\n{rules}\n</rules>\n\n"
f"<documents>\n{doc_block}\n</documents>\n\n"
f"<question>\n{question}\n</question>"
)
print(labeled_prompt("Answer only from the documents.", ["Paris is in France."], "Where is Paris?"))Common errors
- Question first, documents last. The model may treat the documents as a continuation and skip the question.
- Critical rule only in the middle of a long history. It gets diluted as more turns accumulate. Keep rules in the system section and restate them near the end.
- Reordering on every call without need. Moving stable content around breaks prompt caching, which a later chapter covers. Keep the stable prefix fixed.
- Assuming one layout fits all models. Test your layout, as the evaluation chapter explains.
Exercise
You retrieve eight passages. The most relevant is currently in position 4 of 8, and the answers often ignore it. Name two changes to placement that could help.
Show answer
Move the most relevant passage to the start or end of the document block, for example with an edge-first ordering. Also move the user's question to the end of the prompt and restate any critical constraint there. Then check the result on a fixed test set.