Lesson 73 — Beginner
Capstone: Build a Production-Style Agent
Capstone mein ek production-style agent ka design aur skeleton banate hain jo level ke saare ideas jodta hai: agent loop with budgets, well-designed tools, MCP exposure, memory layers, sandboxed code, human approval aur progress events. Example refund-handling support agent hai — apne use-case mein badal lena.
Design — kaunsa hissa kahan
WHAT
Ek production agent = loop + gated tools + memory + persistence + budgets + observability. Har layer L5 ke kisi lesson se aayi hai — capstone sirf unhe ek blueprint mein jodta hai.
WHEN
Jab demo ko aage badhana ho — pehle ek use-case chuno jisme read tool, ek consequential tool aur user approval teeno ho (refund, booking, publish jaisa).
WHERE
Service ke roop mein deploy — API/chat UI se judta hai, events bhejta hai, audit log rakhta hai, aur MCP server se same tools doosre apps ko bhi milte hain.
user -> API / chat UI (progress events, cancel button, approval prompt)
|
v
agent service
+-- agent loop: max steps + token/time budget (L5.2, L5.9)
+-- tools: read-only (lookup) + gated (refund, approval ke saath) (L5.3)
+-- sandbox runner: code tool sirf isolated env mein (L5.8)
+-- memory: short-term window + long-term store (L5.5)
+-- persistence: thread_id + checkpointer — resume (L5.7)
+-- audit log + escalation queue (L5.9)
|
+-- MCP server: same tools/resources doosre AI apps ke liye (L5.4)
Skeleton code — loop + tools + budget + approval
GATED = {"issue_refund"} # consequential — approval chahiye
def run_refund_agent(user_msg, thread_id, approved=False):
budget = Budget(max_steps=8, max_tokens=60000) # L5.9
state = {"messages": load_memory(thread_id) + [{"role": "user", "content": user_msg}]}
while True:
budget.step() # steps guard
reply = llm.bind_tools(ALL_TOOLS).invoke(state["messages"])
budget.charge(count(reply)) # cost guard
if not reply.tool_calls:
save_memory(thread_id, state["messages"]) # memory persist (L5.5)
return reply.content
for call in reply.tool_calls:
if call.name in GATED and not approved:
emit({"type": "approval_needed", "tool": call.name})
state["messages"].append(tool_msg(call.id, "approval chahiye — approved flag ke saath wapas bhejo"))
continue
emit({"type": "tool_start", "tool": call.name}) # progress UX
obs = safe_tool(call) # errors + truncation (L5.3)
state["messages"].append(tool_msg(call.id, obs))
audit.log(thread_id, call, obs) # kya hua, kisne allow kiya
if budget.exhausted():
save_state(thread_id, state)
return "Budget khatam — progress save hai, resume kar sakte hain."
Yeh loop L5.2 wala hi hai, bas production fittings lage hue: budget har turn, gated tool par approval check, events UI ke liye, audit log, aur persistence. Skeleton ko utha kar apne tools se replace karo — structure wahin rahega.
Production checklist
- Budgets: max steps + time + tokens, exceed par graceful stop aur saved state.
- Tool tiers: read-only default, write tools approval ke saath, descriptions crisp.
- Sandbox: agent ka koi bhi code-generated execution isolated env mein, network off, timeout ke saath.
- Memory: window cap + trigger-based summarization + long-term facts retrieval.
- Persistence: thread_id + checkpointer — crash/interrupt ke baad resume.
- UX: progress events (task, tool, result), approval prompts, cancel/pause/redirect.
- Audit + escalation: har tool call, result aur approval logged; budget/confusion par human queue.
- Evals: pehle se scenario tests (L7) — agent badalte hi regression pakdo.
Kaise extend aur interview mein present karo
- Apna use-case: refund ki jagah booking ya document workflow — tool names badlo, guards wahin rakho.
- Framework swap: yehi skeleton LangGraph (L5.7) ya kisi framework (L5.10) mein dobara likho — framework kya chhupa hai, khud dekhoge.
- MCP se expose: same tools MCP server banao (L5.4) — ek integration, kai apps.
- Present karte waqt: problem → architecture ka diagram → kaunsa guard kis failure ke liye → ek real failure aur uska recovery. Yehi story interview mein chalti hai.
Exercise
Question: Consequential tool (refund jaisa) par lagne wala gate kya kehlata hai? (approval / confirmation, ek shabd)
Question: Crash ke baad agent ko wahi jagah se chalane ke liye kis cheez ko persist karna zaroori hai? (ek shabd)
Question: Har tool call, result aur approval ko record karne wala log kya kehlata hai? (do shabd)
Question: Agent ka likha code chalta kahan hai — production process mein ya sandbox mein?
Common mistakes
- Capstone ko demo tak simitna: Sirf tool calls dikhana production-style nahi. Budgets, audit log, escalation aur approval gates daalo — yahi cheez resume mein alag karti hai.
- Sab tools bina gate ke dena: Refund/delete/send direct model ke haath mein = ek bad day. Gated tools + approval flow pehle se define karo, har consequential action par.
- Memory aur persistence skip karna: Thread khatam hote hi context gaya — user phir sab dohrata. Memory store + checkpointer dono chahiye, wahi toh "production" ka matlab hai.
- Observability last mein add karna: Bina events/audit ke agent chup-chaap fail hota hai aur debug karna mushkil. Progress events aur log pehle se likho — debugging aasaan ho jaayegi.
Guest aata hai (user), receptionist (agent loop) department bhejta hai (tools), special cases mein manager approval (HITL) leta hai, aur register mein sab likha jaata hai (audit log):
guest -> receptionist -> department (tool) -> update register (audit log)
| big request -> manager approval (HITL) -> tabhi aage
| guest gaya? register phir bhi hai (persistence -> next visit)- Q: Production-style agent ka minimum architecture batao — chhe components ke saath.
- Q: Ek refund flow ko gated tool kaise banaoge — approval ka path sketch karo.
- Q: Agent ko cancel/resume karne ka mechanism kya hoga — state aur persistence ke saath?
- Q: Apne capstone ko kaise present karoge — problem, design aur failure handling wala structure do.
L5 AI Agents complete! Agent loop se production-style agent tak — poora raasta tay kiya. Agla level Context Engineering hai, jahan context ko budget ki tarah manage karna seekhoge.