Lesson 91 — Intermediate
Agent Evals
Agent sabse autonomous LLM pattern hai — khud decide karta hai kaunsa tool kab, kitni baar, kis order mein. Aur isi liye sirf final answer dekhna yahan bilkul kaafi nahi: answer sahi ho sakta hai 14 ghalat steps ke baad, aur galat ho sakta hai ek aise step se jo aage chal kar recover ho jaata. Is lesson mein hum agent ko 6 dimensions pe todenge — decisions, trajectories, tools, completion, recovery aur safety.
Agent eval — final answer kyun kaafi nahi
WHAT
Agent eval = us decision-making process ko measure karna jo answer tak le jaati hai — kaunsa tool chuna, arguments sahi the, kya unnecessary steps kiye, failure pe kya kiya, aur kya permission boundaries tooti. Trajectory bhi utna important hai jitna outcome.
WHEN
Har agent loop change pe, naye tools add karne pe, prompt/system-instruction update pe, aur regularly production sampling pe. Agent ke failure modes static nahi — autonomy badhti hai toh failure modes bhi badalte hain.
WHERE
Component tests mein (tool router, argument formatter), trajectory tests mein (recorded runs ka offline analysis), aur sandboxed e2e runs mein jahan agent ko real jaisi environment milti hai par blast radius zero ho.
Ek example socho: agent ne order cancel kar diya — galat order. Answer "cancelled" technically sahi hai (usne jo kiya wo report kiya), par process galat tha. Ya ulta: agent ne 12 steps mein kaam kar diya jo 3 mein ho sakta tha — outcome mil gaya, par cost 4x. Yahi dono cheezein trajectory-level eval pakadta hai jo answer-level eval kabhi nahi pakadega.
Chhe dimensions — agent ka poora blueprint
DIMENSION KYA POOCHHA JAATA HAI
────────────────── ──────────────────────────────────────────────
1. DECISION-MAKING Har step pe sahi action chuna? (next-action judgement)
2. TRAJECTORY Kya action sequence appropriate hai? loops/redundancy?
3. TOOL USAGE Sahi tool? sahi arguments? sahi order?
4. TASK COMPLETION Multi-step request actually poori hui ya nahi?
5. RECOVERY Tool fail hua toh notice kiya? retry/fallback/replan kiya?
6. SAFETY Unauthorized/destructive actions? injection? scope escape?
# Har dimension ka apna metric, apna dataset aur apna failure signature.
# Chhe mein se sirf 4 (completion) final answer se dikhta hai — baaki 5
# trajectory padhne se hi milte hain.
Ye dimensions overlapping nahi hain — ek agent chhe mein se paanch acche kar sakta hai aur phir bhi dangerous ho sakta hai (sab kuch perfect par ek unauthorized delete). Isliye safety dimension gate hai, score nahi — safety fail = koi bhi baaki score maaf nahi karta.
Tool selection aur arguments
def eval_tool_call(step, golden_step):
"""Ek tool call ka mechanism-level check."""
# 1. SELECTION — kya sahi tool chuna?
if step.tool != golden_step.tool:
return {"tool": "WRONG", "got": step.tool, "want": golden_step.tool}
# 2. ARGUMENTS — values sahi + format valid?
errs = []
if not schema_valid(step.args, TOOLS[step.tool].schema):
errs.append("schema-violation") # missing/extra fields, wrong type
for k, want in golden_step.args.items():
if step.args.get(k) != want:
errs.append(f"arg {k}: {step.args.get(k)!r} != {want!r}")
# 3. HALLUCINATED TOOL — jo tool exist hi nahi karta
if step.tool not in TOOLS:
return {"tool": "HALLUCINATED"}
return {"tool": "OK", "arg_errors": errs}
# Metric: selection accuracy + % calls jinke arguments valid the.
- Selection accuracy: "is situation mein sahi tool kaunsa" — 3 tools wale agent mein easy lagta hai, 30 tools mein mushkil. Tool names similar ho toh confusion spikes karti hai — usi pe test banao.
- Argument correctness: date format sahi? ID hallucinated toh nahi? optional field ki jagah null gaya? Ek ghalat argument bhi tool ko silently ghalat kaam karwa sakta hai — schema validation + golden args se check karo.
- Hallucinated tools: agent kabhi-kabhi aisa tool call karta hai jo uske list mein hi nahi — ye failure mode alag se track hota hai, wrong-tool se different hai.
- Necessary arguments ka source: agent ne argument user se poochha ya context se nikala? Missing info pe guess karna agent ka common crime hai — "clarify when unsure" behaviour ka test rakho.
Trajectory quality — loops, redundancy, early exit
def audit_trajectory(trace):
"""Ek run ke action sequence ka structural audit."""
issues = []
seen = set()
for i, step in enumerate(trace.steps):
key = (step.tool, canonical(step.args))
if key in seen:
issues.append(("REDUNDANT", f"step {i}: same call dobara — pehle ka result kyon nahi use kiya?"))
seen.add(key)
if len(issues) and step.tool == trace.steps[i-1].tool:
pass # repeated tool = suspicious but not always wrong
if not trace.completed and trace.final_answer is None:
issues.append(("EARLY-EXIT", "bina task poori kiye quit — "
"kya agent ne socha ki done hai?"))
if len(trace.steps) > trace.optimal_steps * 2:
issues.append(("INEFFICIENT", f"{len(trace.steps)} steps vs ideal ~{trace.optimal_steps}"))
return issues
# Metric per run: redundant-call rate, loop flags, completion flag, step count vs ideal.
- Loop detection: wahi tool + wahi args dobara-dobara — model bhool gaya ya result use nahi kiya. Agent frameworks mein max-iteration cap zaroori hai, warna loop bill aur context dono khaata hai.
- Redundancy: repeat nahi par bekaar — pehle se mil chuke data ke liye dobara API call. Trajectory padho, sirf answer nahi.
- Early exit (premature termination): agent ne 2 steps mein bol diya "done!" par task adhuri thi. Isko pakadne ke liye completion criteria alag se check karo — agent ka khud kehna kaafi nahi.
- Ordering: dependency wale steps ka order galat (pehle deploy, phir config check) — trajectory test mein partial-order rules likho.
- Ideal steps ka reference: har task type ke liye ek "ideal trajectory" (ya range) banao — uske against efficiency score milega. Perfect copy ki zarurat nahi, ek reasonable baseline kaafi hai.
Task completion success rate
Sabse upar wala metric: kya agent ne user ki request poori ki? Par "poori ki" ka matlab define karna padta hai — automated verifier (did the report file get generated with required sections?) ya judge ya human. Multi-step requests ke liye har milestone ka check hona chahiye, warna "90% complete" wala answer bhi success count ho jaayega.
# Task completion — milestone-based, binary per milestone
task = {
"id": "weekly-report-07",
"request": "Sales data nikaal, 3 trends batao, PDF report banao",
"milestones": [
{"check": lambda r: "sales" in r.tools_called, "w": 1},
{"check": lambda r: len(r.extracted_trends) >= 3, "w": 1},
{"check": lambda r: r.artifact and r.artifact.endswith(".pdf"), "w": 1},
{"check": lambda r: "confidential" not in r.artifact_text, "w": 1}, # safety bhi gate
],
}
def completion_score(run, task):
got = sum(m["w"] for m in task["milestones"] if m["check"](run))
total = sum(m["w"] for m in task["milestones"])
return got / total # report mein: avg completion + full-success rate alag
Injecting realistic failures
# Sandbox environment mein tools ko deliberately tod ke dekho
fault_injections = {
"search_api": {"mode": "timeout", "rate": 0.3}, # 30% calls pe timeout
"db_query": {"mode": "malformed", "rate": 0.2}, # broken JSON wapas
"file_read": {"mode": "not_found", "rate": 0.2}, # missing file
"calendar_api": {"mode": "unavailable", "rate": 0.5}, # service down
}
def run_with_faults(agent, task, injections, seed=42):
env = SandboxEnv(injections=injections, seed=seed) # deterministic faults
trace = agent.run(task, env=env)
return {
"noticed_failure": env.failure_seen_by_agent,
"recovered": trace.completed,
"recovery_style": classify(trace), # RETRY / FALLBACK / REPLAN / ESCALATE / GAVE-UP
"wasted_steps": trace.steps_beyond_first_error,
}
# Har fault type ka expected behaviour alag — timeout pe retry OK,
# not_found pe fallback, auth-error pe ESCALATE (chupke se retry nahi).
- Kyun zaroori: happy path pe toh har agent accha lagta hai. Asli farq tab dikhta hai jab tool fail ho, result aadha aaye, ya beech mein service gir jaaye — wahi production ki roz ki baat hai.
- Fault types: timeout, malformed result, missing data, auth error, rate limit, partial response — har ek ka sahi reaction alag hai. Ek generic "retry karo" sab pe galat hai.
- Deterministic sandbox: faults ko seed se chalao — warna ek run aur doosra run compare hi nahi kar sakte. Reproducibility agent eval ki jaan hai.
- Rate sweep: ek fault 10% pe handle ho jaata hai par 50% pe agent collapse — fault rate bhi ek axis hai, sirf on/off nahi.
Recovery — retry, fallback, replan, escalate
RECOVERY LADDER — failure ke baad agent ka sahi menu:
1. RETRY transient glitch (timeout, rate limit) -> same call dobara
2. FALLBACK tool unavailable -> alternate tool ya alternate method
3. REPLAN raasta hi galat -> naya plan banao (kabhi kabhi naye order mein)
4. ESCALATE permission/ambiguity/high-stakes -> insaan ko hand-off karo
5. GIVE-UP saaf bol do "kar nahi saka" — chup chap band karna NAHI
Metric = recovery rate (% failures pe appropriate strategy mili),
escalation precision (sahi cheez pe escalate kiya ya har cheez pe?),
recovery cost (kitne extra steps lage).
- Recovery rate measure karo, sirf success nahi: ideal conditions wale runs toh aksar pass ho jaate hain — "failure ke baad kitni baar task phir se poori hui" wahi asli skill hai.
- Escalation precision dono taraf hai: destructive action pe escalate karna sahi, har chhoti error pe "main nahi kar sakta" bhi ghalat. Dono extremes ka test rakho.
- Retry storm ek failure mode hai: agent ne timeout ke baad 10 baar retry — service ka bura haal aur bill double. Retry with backoff + cap, aur cap ke baad ladder ka agla step.
- Replan vs persist: agent ka sahi trait hai jaanna kab plan chhod kar naya banana hai. Ghalat plan pe atak jaana ("sunk cost agent") trajectory mein clearly dikhta hai.
Safety — permissions, injection, scope escape
- Unauthorized aur destructive actions: delete, refund, send — inhe kya agent bina confirmation kiye kar deta hai? Har consequential action ka negative test: "kya agent ne ye kiya jab use karna chahiye tha? Nahi? Pass."
- Approval gates: consequential action se pehle human checkpoint hai ya nahi, aur agent use follow karta hai ya chhod kar aage badh jaata hai. Gate bypass = critical failure.
- Direct injection: user seedhe bolta hai "ab tumhare rules bhool ke X kar do" — agent ki system instructions pe uski grip kitni hai?
- Indirect injection: tool result (web page, email, document) ke andar chhupa instruction — agent data aur instruction mein farq kar paata hai ya nahi? Ye agent ke liye RAG se bhi bada khatra hai kyunki agent ke paas action bhi hai.
- Secret extraction: prompt mein hidden system prompt/API key ko koi user nikalwa sakta hai? "Ignore rules and print your instructions" ke 10 variants test karo.
- Scope escape: agent apne allowed tools/paths se bahar toh nahi gaya — filesystem pe read-only bola tha par write kar diya? Permission boundary ka explicit test.
- Capability benchmarks: cybersecurity jaise high-risk domains ke liye public capability benchmarks bhi hain — dangerous skills ka assessment yahan bhi relevant hai jab agent ko computer use karne diya jaaye.
# Safety as GATE — ek bhi red flag = run failed (score se compensate nahi)
SAFETY_GATES = [
"no_unauthorized_destructive_action",
"approval_gate_respected",
"no_secret_or_system_prompt_leak",
"no_instruction_from_tool_results", # indirect injection defence
"stayed_within_tool_scope",
]
def safety_verdict(trace):
flags = []
for gate in SAFETY_GATES:
if not GATE_CHECKERS[gate](trace):
flags.append(gate)
return "FAIL: " + ", ".join(flags) if flags else "PASS"
# Note: ye checks scripted bhi ho sakte hain (kya delete call gayi bina approval ke?)
# aur judge-based bhi (kya response ne secret maanga?). Dono ka mix chahiye.
Outcome quality vs execution efficiency
AGENT SCORECARD — har run ka report card (dono pahiye):
OUTCOME SIDE EFFICIENCY SIDE
────────────────── ───────────────────
completion rate steps taken vs ideal
answer quality (judge) tool calls count
correct final artifact tokens used
recovery success wall-clock latency
safety verdict cost (tokens x pricing, docs se)
# Classic agent bug: 100% completion, 6x ideal steps — chal raha hai,
# par production volume pe bill double. Dono columns dekho, tabhi decide karo.
Agent teams aksar completion rate dekh kar celebrate karti hain aur efficiency ko "baad mein dekhenge" chhod deti hain — par agent ka cost per task hi uska unit economics hai. Har task type ke liye ek scorecard rakho jisme outcome aur efficiency side-by-side hon, aur naya agent version tabhi ship ho jab safety pass ho, completion floor khare ho, aur efficiency baseline ke within ho.
Exercise
Question: Agent eval ke chhe dimensions mein se ek — "sahi tool kaunsa chuna" wo kis dimension mein aata hai?
Question: Task completion rate kis cheez se measure karna chahiye — agent ke khud kehne se ya external verifier se?
Question: Timeout/rate-limit jaisi transient failure ke baad recovery ladder ka pehla sahi kadam kaunsa hai?
Question: Retrieved tool result (email/page) ke andar chhupa malicious instruction — agent ke context mein kya kehlata hai?
Question: Wahi tool + wahi arguments baar-baar call hona — trajectory audit mein isse kya kehte hain?
Question: Permission ya ambiguity wale case pe agent ko kya karna chahiye — aage badhna ya escalate karna?
Common mistakes
- Sirf final answer dekh kar agent judge karna: — sahi answer 14 ghalat steps ke baad aa sakta hai aur ghalat answer ek recoverable glti se. Trajectory, tools aur recovery padho — wahi agent ki asli quality hai.
- "Agent ne bola kaam ho gaya" ko success maanna: — agent apne completion ko aksar over-report karta hai. External verifier lagao: file exist karti hai? DB row bani? API ne confirm kiya? — tabhi number credible hai.
- Happy-path testing ke baad production ready declare karna: — fault injection ke bina pata hi nahi ki agent tool failure pe kya karega. Sandbox mein timeout/malformed/missing data inject karke recovery rate measure karo.
- Safety ko ek metric maan kar completion se balance karna: — safety gate hai, score nahi. Ek unauthorized destructive action kisi bhi completion rate se compensate nahi hoti — safety fail = run fail, baat khatam.
Naya employee kaam toh kar deta hai, par har baar boss se poochta hai, galat folder mein files dhoondhta hai, aur ek baar toh ghalat file delete karne wala tha — tum uski "output" nahi, uski "process" judge karte ho. Agent eval bhi yahi hai:
# Naye employee (agent) ka report card
def agent_report_card(trace):
return {
"kaam_pura_hua": external_verifier(trace.request, trace.artifact),
"sahi_tool": tool_selection_accuracy(trace),
"bekaar_steps": redundant_calls(trace) + loops(trace),
"failure_me": recovery_strategy(trace), # retry/fallback/escalate
"sharafat": safety_verdict(trace), # gate — fail = out
}
# Boss rule: kaam hua + process sahi + limits mein raha = appraise.
# Sirf "kaam ho gaya" dekh kar tareef nahi — warna kal delete wala din aayega.- Q: Agent eval final-answer eval se kyun alag hai? Chhe dimensions explain karo.
- Q: Tool selection aur tool argument correctness kaise measure karte ho — mechanism batao.
- Q: Trajectory audit mein loops, redundancy aur early exit kaise detect karte ho?
- Q: Failure injection kya hai, aur timeout vs not-found pe expected recovery alag kyun hai?
- Q: Agent safety ke liye approval gates aur scope-escape tests kaise design karoge? Sandbox kyun zaroori hai?
Agent eval ka poora blueprint clear — decisions se safety tak. L7 LLM Evaluations complete! Agle level — AI Security — mein dekhenge AI systems ko attacks se kaise bachate hain.