To test whether evidence placement changes an answer, save the order in which sources reached the model. Keep a record of passages you shortened or left out. Compare answers only after you can reproduce what each run saw.
Separate source selection, placement and token admission
LangChain’s context-engineering overview separates writing, selecting, compressing and isolating context. Those are different interventions. This page focuses on placement and omission records after the application has selected its sources; token-budget admission owns the model-specific capacity calculation.
The ranking score below belongs to your application. The placement policy puts the highest-scored source first and the second-highest last, with remaining sources between them. It is an explicit policy to test, not a claim that this order is universally best for LLM accuracy. Avoid changing both the source set and its ordering when measuring an ordering effect.
Make shortened and missing sources visible
This local fixture uses characters so its truncation behavior is easy to inspect. It does not estimate tokens or enforce a model’s context limit. The limit applies only to concatenated source text, excluding labels, instructions and the response budget; use a real tokenizer and the complete serialized prompt for production admission.
import hashlib
def source_placement(documents, max_chars):
if not isinstance(max_chars, int) or max_chars < 0:
raise ValueError("Nonnegative character limit required")
ids = [d["source_id"] for d in documents]
if len(set(ids)) != len(ids):
raise ValueError("Duplicate source_id; resolve source identity first")
ranked = sorted(documents, key=lambda d: -d["score"]) # caller-defined relevance
ordered = ranked if len(ranked) < 3 else [ranked[0], *ranked[2:], ranked[1]]
remaining, parts, ledger = max_chars, [], []
for position, document in enumerate(ordered):
text = document["text"]
kept = text[:remaining]
remaining -= len(kept)
parts.append(kept)
ledger.append({"source_id": document["source_id"], "position": position,
"original_sha256": hashlib.sha256(text.encode()).hexdigest(),
"original_chars": len(text), "retained_chars": len(kept),
"status": "full" if len(kept) == len(text) else "partial" if kept else "omitted"})
return {"source_text": "".join(parts), "ledger": ledger}
The fixture checks empty input, a single source, stable ties, the intended first/last positions, partial and omitted sources, and duplicate identifiers. The original hash refers to the unshortened input. Keep a separate hash of the actual context sent to the model. Delimiters and source labels should be included in the production serialization and its token count.
Evaluate answers separately from the ordering function
A correct ordering function proves only the recorded order and retained content. To evaluate model behavior, freeze the question set, source versions, model, prompt and scoring rubric; compare citations and unsupported answers as well as task completion. This article does not supply a measured accuracy improvement or a universal percentage saving.
For format/token measurement, use the HTML and Markdown measurement method while its planned consolidation with the companion page is reviewed. For extracting an allowed source into text, see Reader API contract. Keep extraction success, source relevance, token admission and answer support as separate checks.