0Pricing
AI Agents with LangChain & Autonomous Workflows · Lezione

Aggiungere memoria e stato della conversazione agli agenti

Aggiungete memoria a breve e lungo termine agli agenti LangChain, così che ricordino il contesto tra un turno e l’altro e producano conversazioni coerenti in più passaggi.

Aggiungere memoria e stato della conversazione agli agenti è una lezione AI Agents with LangChain & Autonomous Workflows 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 AI Agents with LangChain & Autonomous Workflows, e i tuoi progressi si sincronizzano tra il web e l'app CoddyKit. Il corso AI Agents with LangChain & Autonomous Workflows include 4 lezioni in totale.

Parti di questa lezione non sono ancora state tradotte e vengono mostrate in inglese.

Why Agents Need Memory

LLMs are stateless: each call knows nothing about the last unless you tell it. Without memory, an agent forgets your name the instant you say it.

The Context Window

Memory ultimately means stuffing prior info into the context window. That window is finite, so the real challenge is deciding what to keep and what to drop.

Buffer Memory

Buffer memory stores the whole conversation and replays it every turn. Accurate, but it grows without bound and eventually overflows the context window.

from langchain.memory import ConversationBufferMemory
memory = ConversationBufferMemory()
memory.save_context({'input': 'Hi, I am Lena'}, {'output': 'Hello Lena!'})

Windowed Memory

Windowed memory keeps only the last N exchanges. It bounds size and forgets older context — perfect when only recent turns matter.

from langchain.memory import ConversationBufferWindowMemory
memory = ConversationBufferWindowMemory(k=4)

Summary Memory

Summary memory periodically condenses older turns with the LLM, keeping the gist plus recent messages — preserving meaning in a small footprint.

from langchain.memory import ConversationSummaryMemory
memory = ConversationSummaryMemory(llm=llm)

Short-Term vs Long-Term

Two flavors serve different needs: short-term memory holds the current chat in the prompt, while long-term persists facts across sessions in a store.

Long-Term Memory with Vectors

For knowledge that must survive sessions, store messages as embeddings in a vector store and retrieve the most relevant ones by similarity instead of replaying everything.

from langchain.memory import VectorStoreRetrieverMemory
memory = VectorStoreRetrieverMemory(retriever=vectorstore.as_retriever())

Wiring Memory into a Chain

Wire memory into a conversation chain. Each call loads prior context, runs the model, and saves the new exchange automatically.

from langchain.chains import ConversationChain
chain = ConversationChain(llm=llm, memory=memory)
print(chain.predict(input='What is my name?'))

Session and User Scoping

Real apps serve many users at once. Scope memory by session or user id so separate conversations never leak into each other.

store = {}
def get_memory(session_id):
    if session_id not in store:
        store[session_id] = ConversationBufferMemory()
    return store[session_id]

Cost and Privacy Tradeoffs

More memory means more tokens — higher cost and latency. Long-term stores may hold sensitive data, so mind retention limits and what you're allowed to keep.

Choosing a Memory Strategy

Choosing a strategy: buffer or window for short chats, summary for long ones, vector for cross-session facts — always scoped per user and mindful of cost.

Quick Check

You've met several memory types — which fits when? Time to put it to the test.

Recap

Recap: memory feeds prior context into a finite window. Buffer, window, and summary trade accuracy for size, vector stores enable long-term recall — always scope and watch cost.

Domande Frequenti

La lezione «Aggiungere memoria e stato della conversazione agli agenti» è gratuita?

Sì — il testo completo di «Aggiungere memoria e stato della conversazione agli agenti» è 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 AI Agents with LangChain & Autonomous Workflows, passa a CoddyKit PRO. Il corso AI Agents with LangChain & Autonomous Workflows include 4 lezioni in totale.

Cosa imparerò in «Aggiungere memoria e stato della conversazione agli agenti»?

Aggiungete memoria a breve e lungo termine agli agenti LangChain, così che ricordino il contesto tra un turno e l’altro e producano conversazioni coerenti in più passaggi. Eserciti AI Agents with LangChain & Autonomous Workflows 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 AI Agents with LangChain & Autonomous Workflows?

Non è richiesta alcuna esperienza precedente. AI Agents with LangChain & Autonomous Workflows 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 «Aggiungere memoria e stato della conversazione agli agenti»?

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 AI Agents with LangChain & Autonomous Workflows?

Sì. Ogni lezione AI Agents with LangChain & Autonomous Workflows 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

  1. Comprendere gli agenti AI e gli LLM
  2. Spiegazione dei componenti fondamentali di LangChain
  3. Costruire il primo agente semplice
  4. Aggiungere memoria e stato della conversazione agli agenti
← Torna a AI Agents with LangChain & Autonomous Workflows