Lesson 67 — Beginner
Agent Memory Basics
Bina memory ke agent har turn par blank ho jaata hai. Is lesson mein pehle farak samjhenge conversation history, state, short-term aur long-term memory mein; phir memory ke chaaro operations (write → retrieve → update → forget), "kya yaad rakhein" wali selectivity aur trimming/summarization dekhenge.
History, state, short-term, long-term
WHAT
History = messages jo abhi chal rahe conversation mein hain. State = task ki working info (plan, ids, intermediate results). Short-term = current window, long-term = persistent store se laaye curated facts, preferences aur summaries.
WHEN
Har turn par history/state chahiye hi chahiye. Long-term tab jab user alag sessions mein laute ya purani baat kaam aaye — "pichhli baar wahi error aaya tha".
WHERE
History messages list mein, state app ke objects/DB mein, long-term ek store mein (SQLite, vector index) jisse relevant memories retrieve hoti hain.
layer | kya rakhta hai | kab tootta hai
-------------+---------------------------------------+--------------------
history | current conversation messages | session turn
state | task ki working info — plan, ids, temp | task khatam
short-term | recent turns + thoda working context | window cap hit
long-term | curated facts, prefs, summaries, past | kabhi nahi (forget)
Confusion door karo: history record hai, memory decision hai. History mein sab messages hain; memory mein sirf wo jo aage kaam aaye. Agent banate waqt in layers ko alag-alag functions do — milti-julti rehti hain par responsibilities alag hain.
Short-term memory — rolling window
class ChatMemory:
def __init__(self, keep_last=12, max_chars=6000):
self.messages = []
self.keep_last = keep_last
self.max_chars = max_chars
def add(self, role, text):
self.messages.append({"role": role, "content": text})
def window(self):
msgs = self.messages[-self.keep_last:] # recent turns hamesha
text = "\n".join(m["content"] for m in msgs)
if len(text) > self.max_chars: # char budget bhi
msgs = msgs[-6:] # aur kaat do
return msgs
Short-term ka kaam hai chhoti rakho, recent rakho — recency limit (last N messages) aur size limit (chars/tokens) dono. Sirf recency kaafi nahi: ek lamba tool output poori window kha sakta hai.
Long-term memory — store + retrieve
class MemoryStore:
def __init__(self):
self.rows = [] # production mein SQLite ya vector store
def write(self, kind, text):
self.rows.append({"kind": kind, "text": text}) # fact | pref | summary
def retrieve(self, query, k=3):
return top_k(self.rows, query, k) # keyword ya embedding similarity
def update(self, idx, text):
self.rows[idx]["text"] = text # galat/purana fact sudhaaro
def forget(self, idx):
self.rows.pop(idx) # purana/galat hata do
- Write kab: user ne preference bataya ("hamesha Hindi mein jawab"), fact confirm hua, task ka outcome mila — tab likho.
- Retrieve kab: tab jab zaroorat ho — task se relevant query daal ke top-k lao. Har turn par poora store mat bhejo.
- Update: pehle se stored fact badla toh usi entry ko update karo — duplicate facts rakh kar confusion mat paida karo.
- Forget: data minimization aur saaf-suthari dono ke liye zaroori. Stale memory bhi dikkat deti hai, same jaise stale context.
Kya yaad rakhein aur kya bhool jaayein
- Rakhne layak: stable facts (naam, role, environment), preferences (style, language), summaries (pichhla task kya hua), aur previous outcomes (kya try kiya, kya fail hua).
- Small talk, har ek message, redundant tool dumps — bhool jao. Sab store karna memory nahi, context debt hai.
- Rule: "agle call mein iska fayda hoga?" — haan toh write, warna skip. Isi ek sawaal se 80% decisions ho jaate hain.
- Retrieval > dump: poora history har call par daalne ke bajaye relevant memory chuno — window chhoti, context saaf.
Growth — trim ya summarize
def maybe_summarize(memory):
if count_tokens(memory.window()) > 5000: # threshold tumhara
summary = llm_summarize(memory.messages)
memory.messages = [
{"role": "system", "content": f"Pichhli baatchit ka summary: {summary}"},
*memory.messages[-4:] # thode recent bhi rakh do
]
# trim = purane messages hata do (sasta, fast)
# summarize = content ko short kar do (thoda mehnga, zyada rich)
# dono ka trigger hona chahiye — kabhi kabhi nahi, hamesha jab cap hit ho
- Trim: simple aur fast — recent N rakho, purane phenko. Chhoti chats ke liye perfect.
- Summarize: lambe kaam ke liye behtar — ek turn pehle summarization call karo, window chhoti rehti hai, nuance bachta hai.
- Trigger: size/threshold-based, time-based nahi — turn lamba ho toh pehle hi kaat do.
- Gehri context offloading/compression strategies alag level (L6) mein — yahan kaam ka base layer hai.
Exercise
Question: Current task ki working information (plan, ids, intermediate results) kis layer mein rakhte hain? (ek shabd)
Question: Long-term store se relevance ke basis par memories dhoondhna kya kehlata hai? (ek shabd, English)
Question: Purane messages ko hata kar recent rakhne wala step ka naam? (ek shabd, English)
Question: Memory ke chaaro operations — write, retrieve, update aur ...? (ek shabd, English)
Common mistakes
- Sab kuch store karna: Har message blind save karna memory nahi, dumping hai. Sirf facts, preferences, summaries aur outcomes likho — "agle call kaam aayega" wala filter lagao.
- History ko hi long-term samajhna: History session mein rehti hai; long-term persistent store + retrieval hai. Alag layer ke bina "pichhli baar" wali baat kabhi yaad nahi rahegi.
- Poori history har call par bhejna: Context blow-up ka sabse bada source. Relevant memories retrieve karo, window chhoti rakho — isi ka naam hai memory, dump nahi.
- Growth ka trigger na rakhna: Summarize/trim kab chalega — decide hi nahi toh kabhi nahi chalega. Threshold daalo, warna ek ghante mein budget phat jaayegi.
Roz ke class notes sab likhte hain (history/short-term) — par exam se pehle banaya cheat-sheet curated hai (long-term). Har page yaad rakhne ki zarurat nahi, sirf asli cheezon ki:
short-term: aaj ki class ke notes — session khatam toh file band
long-term: final wala cheat-sheet — facts, formulas, apni galtiyan
retrieve: paper mein zaroorat padi toh wahi page kholo, poora copy nahi- Q: Conversation history aur state mein farak — ek example ke saath.
- Q: Memory ke chaaro operations batao, har ek ka ek realistic example do.
- Q: Retrieved memory vs poora history — context aur reliability dono ka angle samjhao.
- Q: Trim aur summarize — kab choose karoge, trigger kaise set karoge?
Memory ke layers clear. Agla lesson — Multi-Agent Architectures — mein dekhenge kab ek agent kaafi nahi hota aur decomposition kaise karte hain.