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.

⏱ 10 min• Beginner• Agent Frameworks & Libraries

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)
Yaad rakho: Architecture ka test: har box kisi ek failure ko rokne ke liye hai. Agar box ka jawab "kyunki trend hai" ho — hata do. Har layer ka guard clear hona chahiye.

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

Ship rule: Checklist ka ek bhi item missing = demo hai, production nahi. Pehle audit log aur budgets daalo — incident ke baad daalne ka koi fayda nahi.

Kaise extend aur interview mein present karo

Interview angle: "Agent banaya" se sawaal aage badhta hai — budgets kya the, kitne steps, failure kaise handle, kitne log use kar rahe. Numbers apne project ke, guess nahi.

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

Desi example: Hotel ka front desk

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):

text
guest -> receptionist -> department (tool) -> update register (audit log)
         | big request -> manager approval (HITL) -> tabhi aage
         | guest gaya? register phir bhi hai (persistence -> next visit)
Interview angle — yahan se sawaal aate hain
  • 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.
Lesson complete?

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.

Popular courses: Data Science Course in Lucknow · AI Course in Lucknow · Python Course in Lucknow · Power BI Course · Data Analytics Course