Evaluation-Driven Development › Recording Traces
Language AI HandbookAll chapters A trace helps diagnose a scored failure by recording what happened before the answer. A failed run can become a repeatable test once you specify which behavior ought to change. Evaluate agent workflows
This fixture records retrieval and generation as ordered events. Fields store enough detail to inspect the simulated decisions. Log only the necessary, permitted fields in production. Tracing and Observability for AI Systems
PythonIn [13]Expand
@dataclass
class Step:
name: str
payload: dict
@dataclass
class Trace:
case_id: str
steps: list[Step]
answer: str = ""
score: float = 0.0
def add(self, name: str, **payload) -> None:
self.steps.append(Step(name, payload))
def find(self, name: str) -> list[Step]:
return [s for s in self.steps if s.name == name]The simulated retriever selects the first matching keyword. The scripted generator copies the document unless its configuration injects an omission or a wrong duration. There is no language model in this fixture.
PythonIn [14]Expand
KNOWLEDGE = {
"refund": "Refunds are available within 30 days with the original receipt.",
"password": "Go to Settings, then Email, and click the reset link.",
"upload": "Check the app logs and your version number before reporting.",
"reembolso": "Los reembolsos están disponibles dentro de 30 días.",
"passwort": "Gehen Sie zu Einstellungen und dann E-Mail.",
}
def fake_system(question: str, trace: Trace, config: dict) -> str:
# 1. Retrieval: naive keyword match
doc = ""
for key, text in KNOWLEDGE.items():
if key in question.lower():
if not (key == "upload" and config.get("retrieval_miss")):
doc = text
break
trace.add("retrieve", doc=doc, found=bool(doc))
# 2. Generation: stand-in that may drop or add facts per config
if not doc:
ans = "I don't have information on that."
else:
ans = doc
if question == "Can I get a refund after 45 days?":
ans = "No. " + ans
if config.get("drop_receipt_hint") and "receipt" in doc:
ans = ans.replace(" with the original receipt", "")
if (
config.get("wrong_window")
and question == "Can I get a refund after 45 days?"
):
ans = ans.replace("30 days", "60 days")
if (
config.get("retrieval_miss")
and question == "App crashes on upload. What do I do?"
):
assert not doc
trace.add("generate", question_chars=len(question), answer=ans)
return ans
def run_case(case: Case, config: dict) -> Trace:
tr = Trace(case.case_id, [])
tr.answer = fake_system(case.question, tr, config)
tr.score = case.score(tr.answer)
return trConsoleOut [15]Expand
answer: Refunds are available within 30 days with the original receipt.
score: 1.0
retrieve -> {'doc': 'Refunds are available within 30 days with the original receipt.', 'found': True}
generate -> {'question_chars': 30, 'answer': 'Refunds are available within 30 days with the original receipt.'}The refund fixture records retrieval of a matching document, a copied answer, and a full score. The next run holds retrieval fixed and changes one generation flag.
A deliberate failure, traced
Let's break the system on purpose and read the trace. We flip drop_receipt_hint, which simulates the generator dropping a required fact.
ConsoleOut [16]Expand
answer: Refunds are available within 30 days. score: 0.5 retrieval found doc: True
Retrieval found the document and the answer lost a required substring. The score falls from 1 to 0.5. This controlled flag isolates the cause in the fixture. In a real system, a trace can suggest a cause that you then test by changing one input or replaying one component.
Comments
Add to the conversation
A question, a correction, or an example from your own work.
No comments yet. Be the first to share your thoughts!