Agentic RAG のメモリと状態
RAG エージェントに短期・長期メモリを持たせ、会話を維持し、ターンをまたいで事実を想起できるようにします。
「Agentic RAG のメモリと状態」はCoddyKit上の無料LangChain / RAG / Vector DBsレッスンです。 これはレッスン4/4です。 下記で完全なレッスンを無料で読むことができます。その後、ブラウザ内の組み込みコードエディタと24時間対応のAIチューターでハンズオン演習できます。 これはLangChain / RAG / Vector DBs学習パスの一部であり、ウェブとCoddyKitアプリ全体で進捗が同期されます。 LangChain / RAG / Vector DBsコースには全4レッスンが含まれています。
このレッスンの一部はまだ翻訳されておらず、英語で表示されています。
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
AI チューターと学ぶ LangChain / RAG / Vector DBs — 無料
ブラウザでリアルコードを書いて実行し、24/7 の AI チューターから瞬時にサポートを受け、ウェブまたはアプリで続きから学習できます。
- コース
- 12
- レッスン
- 48
よくある質問
「Agentic RAG のメモリと状態」レッスンは無料ですか?
はい。「Agentic RAG のメモリと状態」の完全なテキストはこのウェブで無料で読めます。インタラクティブに演習し(組み込みコードエディタと24時間対応のAIチューター)、LangChain / RAG / Vector DBsコースの残りをアンロックするには、CoddyKit PROにアップグレードしてください。 LangChain / RAG / Vector DBsコースには全4レッスンが含まれています。
「Agentic RAG のメモリと状態」で何を学びますか?
RAG エージェントに短期・長期メモリを持たせ、会話を維持し、ターンをまたいで事実を想起できるようにします。 ブラウザで直接実行するハンズオンコードでLangChain / RAG / Vector DBsを演習し、24時間対応のAIチューターがレッスンを進める中での質問に答えます。
LangChain / RAG / Vector DBsを始めるのに経験は必要ですか?
事前経験は必要ありません。CoddyKitのLangChain / RAG / Vector DBsは初級者から上級者向けに構成されているため、ここから始めるか最初から始めて、自分のペースで進むことができます。 これはレッスン4/4です。
「Agentic RAG のメモリと状態」レッスンにはどのくらい時間がかかりますか?
ほとんどのCoddyKitレッスンは約5~10分かかります。各レッスンはコンパクトでインタラクティブなので、着実に進歩し、ウェブとアプリ全体で正確に前回の場所から再開できます。
このLangChain / RAG / Vector DBsレッスンでコードを書いて実行できますか?
はい。すべてのLangChain / RAG / Vector DBsレッスンに組み込みコードエディタが含まれているため、ブラウザでリアルコードを書いて実行し、即座のAIフィードバックを取得できます。ローカル設定は不要です。
このコースのすべてのレッスン
- LangChain Agentsとツールの概念
- マルチエージェントRAGワークフローの構築
- 外部APIのツールとしての統合
- Agentic RAG のメモリと状態