Memoria e stato nel RAG agentico
Doti il Suo agente RAG di memoria a breve e lungo termine, così che possa sostenere conversazioni e ricordare informazioni tra un turno e l’altro.
Memoria e stato nel RAG agentico è una lezione LangChain / RAG / Vector DBs gratuita su CoddyKit. Questa è la lezione 4 di 4. Puoi leggere la lezione completa qui gratuitamente — poi esercitati direttamente nel browser con un editor di codice integrato e un tutor IA disponibile 24/7. Fa parte del percorso di apprendimento LangChain / RAG / Vector DBs, e i tuoi progressi si sincronizzano tra il web e l'app CoddyKit. Il corso LangChain / RAG / Vector DBs include 4 lezioni in totale.
Parti di questa lezione non sono ancora state tradotte e vengono mostrate in inglese.
Why Agents Need Memory
A stateless agent forgets everything after each call. Memory lets an agent track the conversation, remember user preferences, and avoid repeating retrievals.
Short-Term vs. Long-Term
Short-term memory holds the current conversation in the context window. Long-term memory persists facts across sessions, often in a vector store or database.
Conversation Buffer
The simplest memory keeps the full message history and replays it each turn so the model has full context.
history = []
def chat(user_msg):
history.append({"role": "user", "content": user_msg})
reply = llm.invoke(history)
history.append({"role": "assistant", "content": reply})
return replyThe Context Window Limit
Replaying the entire history eventually overflows the model context. You must summarize or trim older turns to stay within the token budget.
Summary Memory
Periodically compress old turns into a running summary, keeping recent turns verbatim. This preserves gist while freeing tokens.
if len(history) > 20:
summary = llm.invoke("Summarize: " + str(history[:-6]))
history[:] = [{"role": "system", "content": summary}] + history[-6:]Long-Term Memory via Vectors
Store durable facts as embeddings. On each turn, retrieve relevant memories by similarity and inject them, just like RAG over a knowledge base.
memory_store.add_texts(["User prefers metric units"])
relevant = memory_store.similarity_search(user_msg, k=3)Choosing What to Remember
Do not store everything. Extract durable, reusable facts (preferences, decisions, entities) and skip ephemeral chatter to keep long-term memory clean.
State Beyond Chat
Agents also track non-conversational state: which tools ran, intermediate results, and a scratchpad of reasoning steps used to plan the next action.
Thread and Session Keys
In multi-user systems, scope memory by a thread_id or user_id so conversations never leak between people.
def get_history(thread_id):
return store.get(thread_id, [])
def save(thread_id, messages):
store[thread_id] = messagesPersistence
For memory to survive restarts, back it with a database or checkpointer rather than an in-process dictionary. LangGraph offers checkpointers for exactly this.
Putting It Together
Combine a trimmed conversation buffer for recency, summary memory for the middle, and vector long-term memory for durable facts, all keyed by session.
Quick Check
Test your understanding of agent memory.
Recap
You added memory to agents:
- Short-term buffer for the current chat
- Summary memory to fit the context window
- Long-term vector memory for durable facts
- Scope by session and persist for durability
Impara LangChain / RAG / Vector DBs con un tutor IA — gratis
Scrivi ed esegui vero codice nel tuo browser, ricevi aiuto istantaneo da un tutor IA disponibile 24/7, e riprendi da dove hai lasciato sul web o nell'app.
- Corsi
- 12
- Lezioni
- 48
Domande Frequenti
La lezione «Memoria e stato nel RAG agentico» è gratuita?
Sì — il testo completo di «Memoria e stato nel RAG agentico» è gratuito qui sul web. Per esercitarvi in modo interattivo (un editor di codice integrato e un tutor IA 24/7) e sbloccare il resto del corso LangChain / RAG / Vector DBs, passa a CoddyKit PRO. Il corso LangChain / RAG / Vector DBs include 4 lezioni in totale.
Cosa imparerò in «Memoria e stato nel RAG agentico»?
Doti il Suo agente RAG di memoria a breve e lungo termine, così che possa sostenere conversazioni e ricordare informazioni tra un turno e l’altro. Eserciti LangChain / RAG / Vector DBs con codice pratico che esegui direttamente nel browser, e un tutor IA 24/7 risponde alle tue domande mentre lavori sulla lezione.
Ho bisogno di esperienza per iniziare LangChain / RAG / Vector DBs?
Non è richiesta alcuna esperienza precedente. LangChain / RAG / Vector DBs su CoddyKit è strutturato per principianti e studenti avanzati, quindi puoi iniziare da qui o dall'inizio e procedere al tuo ritmo. Questa è la lezione 4 di 4.
Quanto tempo richiede la lezione «Memoria e stato nel RAG agentico»?
La maggior parte delle lezioni CoddyKit richiede circa 5–10 minuti. Ogni lezione è breve e interattiva, quindi fai progressi costanti e riprendi esattamente da dove hai lasciato su web e app.
Posso scrivere ed eseguire codice in questa lezione LangChain / RAG / Vector DBs?
Sì. Ogni lezione LangChain / RAG / Vector DBs include un editor di codice integrato, quindi scrivi ed esegui codice reale direttamente nel tuo browser e ricevi feedback istantaneo dall'IA — nessuna configurazione locale necessaria.
Tutte le lezioni di questo corso
- Agenti LangChain e concetti di tool
- Creazione di workflow RAG multi-agente
- Integrazione di API esterne come tool
- Memoria e stato nel RAG agentico