Three numbers to tune
| Number | Meaning | Effect |
|---|---|---|
k_retrieve | Candidates fetched from the index | Higher → better recall, slower search |
k_rerank | Candidates scored by the reranker | Higher → better ordering, more reranker cost/latency |
k_context | Chunks sent to the LLM | Higher → more input tokens, risk of distracting the model |
A common shape: retrieve 50 → rerank 50 → send 5–8. Your data decides the right values.
Build an evaluation set
- 100–300 real questions with the chunk(s) or document(s) that should be used.
- Metrics: recall@k for retrieval, answer correctness/groundedness for the final answer (see the LLM-as-a-judge post).
Run a sweep
| k_retrieve | k_rerank | k_context | Recall@context | Answer score | Input tokens | p95 latency |
|---|---|---|---|---|---|---|
| 20 | 20 | 10 | measure | measure | measure | measure |
| 50 | 50 | 5 | measure | measure | measure | measure |
| 50 | 50 | 8 | measure | measure | measure | measure |
| 100 | 50 | 5 | measure | measure | measure | measure |
Pick the cheapest configuration within tolerance of the best answer score.
Make each chunk count
| Technique | Benefit |
|---|---|
| Reranker score threshold | Drop weak chunks instead of always sending k |
| Deduplicate near-identical chunks | Fewer wasted tokens |
| Merge adjacent chunks from the same document | Coherent context, fewer headers |
| Put the most relevant chunks first (and/or last) | Models attend better to the edges of long contexts |
| Trim chunk metadata to what's needed for citations | Fewer tokens |
ranked = rerank(question, candidates)[:k_rerank]
context = [c for c in ranked if c.score >= 0.35][:k_context]
context = dedupe(context, similarity=0.95)
Dynamic budgets
- Simple factual questions: fewer chunks.
- Comparison or summary questions: more chunks, or a map-reduce pattern.
- Classify the question type cheaply and set k accordingly.
Tip
Track average input tokens per answer alongside answer quality. When someone raises
k_contextto fix one bad answer, the cost change is visible immediately.
Checklist
- Evaluation set with expected sources
- k_retrieve / k_rerank / k_context swept
- Score threshold and deduplication applied
- Context ordering considered
- Dynamic budget by question type where useful
- Tokens and quality tracked together