Intermediate ? 22 min Prerequisite: Introduction

Agent Architecture

Kaise agents ka design hota hai, kaise components connect hote hain, aur different architectures kya kya kar sakte hain � ye sab samajhte hain.

Why Agent Architecture?

Agent architecture samajhna zaroori hai kyunki ye decide karta hai ki tumhara agent kitna capable hoga. Sahi architecture choose karne se agent efficiently task complete kar sakta hai, galat architecture se agent stuck ho jaata hai.

Key Insight

Ek simple chatbot aur ek powerful agent mein difference sirf architecture ka hai. Architecture decides karta hai ki agent kitna soch sakta hai, kitne tools use kar sakta hai, aur kitna context yaad rakh sakta hai.

Core Concepts

REACT Pattern
Reason + Act cycle. Agent pehle sochta hai (reason), phir action leta hai, aur phir result observe karta hai. Ye sab repeat hota hai jab tak goal achieve na ho jaaye.
💡
Function Calling
Tool execution ka tarika. LLM directly functions call kar sakta hai � search, calculate, database query � koi bhi external tool ho sakta hai.
💡
Observation Loop
Perceive-Think-Act cycle. Agent environment se perceive karta hai, sochta hai, aur phir act karta hai. Continuous feedback loop se agent improve hota rehta hai.
💡
State Management
Context tracking. Agent ko yaad rakhna hota hai ki kya kiya, kya results aaye, aur ab kya karna hai. Ye sab state mein store hota hai.

✓ Agent Architecture Overview

User Input Agent Brain (LLM) Reason ✓ Decide ✓ Plan Tools Memory External APIs Response
Agent Architecture: User ✓ Brain ✓ Tools/Memory/APIs ✓ Observation ✓ Response

REACT Pattern � Deep Dive

REACT (Reason + Act) sabse popular agent pattern hai. Ye 3 steps mein kaam karta hai:

Reason
✓ Act
✓ Observe
Repeat

Step 1: Reason (Socho)

Agent sochta hai ki current situation mein kya karna chahiye. Ye apne memory aur context ko dekh kar decide karta hai.

Step 2: Act (Karo)

Agent koi tool call karta hai � jaise search karna, calculate karna, ya database se data nikalna.

Step 3: Observe (Dekho)

Agent result dekhta hai. Kya goal achieve hua✓ Agar nahi, toh wapas reason pe jaata hai.

ReAct Agent � Code

Ye ek complete ReAct agent implementation hai. Isko samjho � har function ka kya kaam hai:

Python
# ReAct Agent Pattern
class ReActAgent:
 def __init__(self, tools):
 self.tools = tools
 self.memory = []
 
 def reason(self, task):
 # Think about what to do
 thought = f"I need to: {task}"
 self.memory.append({"thought": thought})
 return thought
 
 def act(self, tool_name, input_data):
 # Execute action
 if tool_name in self.tools:
 result = self.tools[tool_name](input_data)
 self.memory.append({
 "action": tool_name,
 "result": result
 })
 return result
 return "Tool not found"
 
 def observe(self, result):
 # Check if goal achieved
 return len(self.memory) > 3

# Usage
tools = {
 "search": lambda x: f"Results for: {x}",
 "calculate": lambda x: eval(x)
}
agent = ReActAgent(tools)
print(agent.reason("Find Python tutorials"))
print(agent.act("search", "Python tutorials"))

✓ Function Calling Pattern

Function calling mein agent directly tools ko call karta hai. Ye structured output deta hai:

Python
import json

class FunctionCallingAgent:
 def __init__(self):
 self.functions = {}
 
 def register(self, name, func, schema):
 self.functions[name] = {
 "func": func,
 "schema": schema
 }
 
 def get_schemas(self):
 return {
 name: info["schema"]
 for name, info in self.functions.items()
 }
 
 def execute(self, name, args):
 if name in self.functions:
 return self.functions[name]["func"](**args)
 return {"error": f"Unknown function: {name}"}

# Register tools
agent = FunctionCallingAgent()

agent.register(
 "search_web",
 lambda query=None: {"results": [f"Found: {query}"]},
 {
 "name": "search_web",
 "description": "Search the web",
 "parameters": {
 "type": "object",
 "properties": {
 "query": {"type": "string"}
 }
 }
 }
)

# Get schemas for LLM
print(json.dumps(agent.get_schemas(), indent=2))

✓ Observation Loop � Complete Cycle

Observation loop mein agent continuously perceive-think-act karta hai. Ye real-time systems ke liye perfect hai:

Python
class ObservationLoopAgent:
 def __init__(self, env):
 self.env = env
 self.state = {}
 self.history = []
 
 def perceive(self):
 # Observe environment
 observation = self.env.get_state()
 self.history.append({
 "type": "perception",
 "data": observation
 })
 return observation
 
 def think(self, observation):
 # Decide action based on observation
 if observation.get("danger"):
 return {"action": "flee"}
 elif observation.get("opportunity"):
 return {"action": "approach"}
 return {"action": "wait"}
 
 def act(self, decision):
 # Execute the chosen action
 result = self.env.execute(decision["action"])
 self.history.append({
 "type": "action",
 "decision": decision,
 "result": result
 })
 return result
 
 def run(self, max_steps=100):
 for i in range(max_steps):
 obs = self.perceive()
 decision = self.think(obs)
 result = self.act(decision)
 
 if result.get("done"):
 break
 return self.history

Interactive Editor � Build Your Agent

Neeche code likho ya edit karo, phir "Run" pe click karo. Dekho kaise agent kaam karta hai:

Agent Editor
Output
Click "Run" to execute...

✓ State Management

Agent ko context yaad rakhna hota hai. Ye 3 types ka hota hai:

Short-term Memory
Current conversation ka context. Jitna zyada conversation, utna zyada memory. Limited hota hai.
Long-term Memory
Persistent storage. User preferences, past interactions � ye sab store hota hai database mein.
Working Memory
Current task ka state. Kya kar rahe hain, kya steps baaki hain � ye sab working memory mein hota hai.
Episodic Memory
Past experiences. "Last time ye kaam kiya tha" � ye pattern yaad rakhna episodic memory hai.

Key Takeaways

  • REACT Pattern sabse fundamental hai � Reason ✓ Act ✓ Observe cycle
  • Function Calling se agent tools ko structured way mein call kar sakta hai
  • Observation Loop continuous feedback se agent improve hota rehta hai
  • State Management agent ko context yaad rakhne mein help karta hai
  • Sahi architecture choose karna project requirements pe depend karta hai
Exercise
Question: ReAct pattern kya hai?
A) Ek database management system
B) Ek machine learning algorithm
C) Reason karke act karo, phir observe karo � repeat
D) Ek web development framework
Sahi jawab: C
ReAct pattern ka matlab hai Reason + Act. Agent pehle sochta hai (reason), phir koi action leta hai (act), aur phir result dekhta hai (observe). Ye cycle tab tak repeat hoti hai jab tak goal achieve na ho jaaye.