Search⌘ K
AI Features

Evidence Ordering, Recitation, and Just-in-Time Loading

Learn to improve context selection by placing strong evidence at the edges, replacing lower-priority notes with compact stubs, and loading full text through a tool when needed. You will also implement evidence recitation, verify quotes against loaded content, and build a loop that catches unsupported citations.

We'll cover the following...

In the previous lesson, filtered_search() dropped the stale runbook and the unapproved draft, so the on-call assistant now gets only current, approved notes. On the running question, five notes survive. Each one is allowed in the prompt, but that still leaves three open questions:

  • Order: In what order should the five notes appear?

  • Size: Does every note need its full text, or can some wait until the model asks for them?

  • Proof: When the answer cites a note, how do we know the model actually read that note’s text?

In this lesson, we’ll answer all three. We’ll reorder the notes so the strongest ones sit at the start and end of the context, send short stubs for the rest with a tool to load them on demand, ask the model to quote its evidence, and write a check that catches quotes the model couldn’t have read.

Order the surviving notes

filtered_search() returns notes in score order, best first. With five notes, that puts the third-best note in the middle of the list, and the weakest one last.

In Chapter 2, we looked at the position-bias debate. Liu et al. found that models answered worse when the key document sat in the middle of the context. Chroma’s tests found flat accuracy in some settings. We don’t know which result holds for our model, but there’s a cheap way to hedge: put the strongest notes at the edges, the start and the end, and let the weakest notes take the middle.

The idea is to walk through the score-ordered list and alternate. The best note goes first, the second-best goes last, the third goes second, the fourth goes second to last, and so on, until the weakest note lands in the middle.

edge_order.py · Python 3.14.0
from hybrid_search import build_indexes
from note_metadata import ALL_NOTES
from filtered_search import filtered_search
from rival_notes import QUESTION
def edge_order(ranked):
front, back = [], []
for position, doc_id in enumerate(ranked):
(front if position % 2 == 0 else back).append(doc_id)
return front + back[::-1] # best first, second best last, weakest in the middle
if __name__ == "__main__":
ranked = filtered_search(build_indexes(ALL_NOTES), QUESTION, top_k=5)["documents"]
print("score order:", ranked)
print("edge order: ", edge_order(ranked))

In the code above:

  • Lines 7–11: We define edge_order(). It sends notes at even positions to the front list and the rest to the back list, then reverses ...