Sub topic 10
Grading results and trying again
This chapter assumes you have read the earlier chapters on the agent loop and on retrieval as a tool. It also assumes basic Python and JSON.
Why grade results
A search tool always returns something. Vector search returns the k nearest chunks even when none of them is relevant. If the agent treats every result as evidence, it answers from weak passages or invents a bridge between them.
Grading is a check between retrieval and answering. It asks whether these passages answer the question, and it has three outcomes.
- Enough: answer, and cite the passages.
- Partial or irrelevant: search again with a changed query.
- Still nothing after retries: say what is missing.
Two ways to grade
In-loop grading needs no extra code. The system prompt tells the model to read each tool result, decide whether it answers the question, and search again if it does not. This is cheap, but the judgment is hidden inside the model's reasoning, so it is hard to test.
A separate grader makes one extra model call that returns a structured verdict. It costs more, but you can log it, measure it, and test it on its own.
import json
GRADE_PROMPT = """Question: {question}
Passages:
{passages}
Do the passages contain enough to answer the question?
Reply with JSON only: {{"verdict": "enough" | "partial" | "irrelevant", "missing": "what is still needed"}}"""
VERDICTS = {"enough", "partial", "irrelevant"}
def grade(question, chunks, call_model):
passages = "\n".join(f"[{c['id']}] {c['text']}" for c in chunks)
raw = call_model(GRADE_PROMPT.format(question=question, passages=passages))
try:
result = json.loads(raw)
except json.JSONDecodeError:
return {"verdict": "partial", "missing": "grader returned invalid JSON"}
if result.get("verdict") not in VERDICTS:
return {"verdict": "partial", "missing": "grader returned an unknown verdict"}
return result
# Stub model so the example runs without an API key.
fake_model = lambda prompt: '{"verdict": "partial", "missing": "the default timeout value"}'
chunks = [{"id": "doc-7", "text": "Timeouts can be configured per request."}]
print(grade("What is the default timeout?", chunks, fake_model))A malformed grader reply falls back to partial, not enough. A grader that fails should never let weak evidence through.
Retrying with a changed query
The missing field is what makes a retry useful. Pass it back to the model so the next query targets the gap. Retrying the same query returns the same chunks.
Keep a set of queries already tried. If the model proposes a repeat, reject it and ask for a different angle: a synonym, a narrower term, or the same question split into parts.
Common errors
- Lenient grader: it calls everything "enough", so retries never happen. Test it with questions you know the corpus cannot answer.
- Strict grader: it demands a perfect match and burns the whole step budget. Ask for "enough to answer", not "complete".
- No exit for missing evidence: after the last retry, the agent must report the gap, not guess. The step-limit message in the agent loop does this.
- Grading the wrong thing: grade whether the passages support an answer, not whether they merely share words with the question.
Exercise
A grader returns "partial" with missing="the default timeout value". The agent's next query is identical to its first one. What should the loop do, and why?
Show answer
It should reject the repeated query and ask the model for a different one, for example one that names "default timeout" directly. An identical query returns the same chunks, so the retry spends budget without adding evidence.