liveBook · Manning

Chapter 6: Working with memory and knowledge RAG for agents

What you will learn in chapter 6 of AI Agents in Action, Second Edition

The introduction explains why agents need retrieval for external information and memory for their own past experiences.

After this chapter you will be able to

What it builds on

Section by section

Introduction

The introduction explains why agents need retrieval for external information and memory for their own past experiences.

Key terms: training data, retrieval, context, external knowledge sources, memory, past experiences, user preferences

6.1 Understanding retrieval in AI applications

The section explains how agents retrieve external knowledge and memory and add relevant pieces to an LLM prompt despite a limited context window.

Key terms: knowledge, memory, structured, unstructured, context window, external storage, retrieval, augmentation, retrieval-augmented generation, RAG

6.1.1 The basics of RAG

This section explains how RAG retrieves relevant information for an LLM’s answer and how the same retrieval pattern can give agents access to past conversations.

Key terms: RAG, ingestion, retrieval, embedding model, context chunks, augmentation, training cutoff, retrieval-augmented generation, external sources, current context

6.1.2 Delving into semantic search and document indexing

Explains how document indexing supports meaning-based retrieval and why semantic search results can be incomplete or unreliable.

Key terms: document indexing, index, semantic search, dense vector embeddings, RAG, memory retrieval, semantic similarity, semantic correctness, top-K, re-ranking

6.1.3 Applying vector similarity search

The section uses TF-IDF to turn documents into sparse vectors, then shows how cosine similarity compares those vectors and where this approach falls short.

Key terms: TF-IDF, semantic embeddings, sparse vector, dense vector, Term Frequency, Inverse Document Frequency, corpus, corpus vocabulary, cosine similarity, cosine distance

6.2 Vector databases and similarity search

Introduces an in-memory vector database example and explains why matching similar words and phrases is not enough for semantic search.

Key terms: vector database, similarity searches, similarity score, context, semantic meaning

6.2.1 Demystifying document embeddings

This section contrasts exact-term retrieval with semantic embeddings, explains how embeddings represent meaning, and shows how to visualize and choose them.

Key terms: TF-IDF, semantic embeddings, hybrid retrieval, BM25, embedding networks, dense vectors, semantic structure, TF-IDF dimensions, principal component analysis (PCA), dimensionality reduction

6.2.2 Querying document embeddings from Chroma DB

This section uses Chroma DB to illustrate semantic document retrieval and explains how to interpret results scored by cosine distance.

Key terms: Chroma DB, local vector store, semantic meaning, cosine distance, cosine similarity

6.3 Building practical RAG knowledge agents

This section frames vector search as a useful foundation for RAG while noting that practical RAG agents often combine search approaches.

Key terms: similarity or vector search, RAG systems, RAG agents, combination of search approaches

6.3.1 Everything begins with search and relevance

This section explains why practical RAG agents combine search methods and assess relevance rather than relying on vector similarity alone.

Key terms: vector/similarity search, relevant information, RAG context, supplementary technique, vector/hybrid search, relational database search, RAG agent

6.3.2 Building a vector search RAG agent

This section builds a movie-script RAG agent using vector search and explains how grounding shapes its answers and exposes a retrieval limitation.

Key terms: semantic chunks, OpenAI embedding model, Chroma DB, search_script, vector/similarity search, grounding, retrieved context, hallucination, weak grounding, strong grounding

6.3.3 Building a hybrid search RAG agent

This section shows how hybrid RAG combines keyword and vector search, either through rank fusion inside a search tool or through an agent that selects results and cites its sources.

Key terms: hybrid search, keyword search, vector search, Reciprocal Rank Fusion (RRF), ranked list, hybrid search tool, vector search function, keyword search function, relevance, grounding

6.4 Adding memory to agents with MCP

This section introduces MCP as a source of tools for agent knowledge and memory systems built with RAG.

Key terms: RAG, MCP, knowledge, memory, out-of-the-box tools

6.4.1 Understanding memory form and agent function

The section explains agent memory through the storage, working state, and retrieval mechanisms that make information available to a prompt, using cognitive memory terms only as shorthand.

Key terms: context window, external storage, state management, retrieval mechanisms, short-term memory, long-term memory, sensory memory, multimodal retrieval, embed-and-search, modality-specific index

6.4.2 Attaching a graph database for memory using MCP

This section shows how an agent uses an MCP-connected graph database as long-term memory, and why graph memory alone may need a complementary search method.

Key terms: graph database, entities, nodes, edges, observations, relationship traversal, memory MCP server, add_observation, add_relationship, query_facts

6.4.3 Creating hybrid memory systems with MCP

This section explains how an agent combines semantic and graph memory through MCP, then distinguishes the learning example from production-ready memory.

Key terms: MCP servers, graph memory, semantic memory, agent instructions, hybrid memory agent, semantic store, entities, observations, ephemeral, MCP server

6.4.4 Semantic augmented memory and applications to semantic, episodic, and procedural memory

The section distinguishes three forms of memory and explains how semantic augmentation makes event and process memories easier to retrieve.

Key terms: episodic memory, procedural memory, semantic memory, relational database search/retrieval pattern, semantic/vector storage, semantic memory augmentation

6.4.5 Uncluttering memory with compression and forgetting

This section explains how clustering and summarizing can reduce memory and knowledge clutter, when compression or forgetting is useful, and why production forgetting needs care.

Key terms: memory compression, semantic augmentation, clustered, compression function, forgetting, repetitive memories, knowledge compression, periodic compression, compression levels, production memory

6.5 Exercises

The section gives five hands-on exercises for comparing retrieval methods, persisting vectors, and testing vector-only and hybrid RAG agents.

6.6 Summary

The section summarizes how agents retrieve external knowledge, combine search methods, and manage memory to produce grounded, scalable answers.

Where this is used later

Concepts explained in this chapter

← Chapter 5: Agent reasoning and planning Chapter 7: Building robust agents with evaluation and feedback →

These guides are generated from the text of Manning books by Manning's knowledge graph and link to the sections they draw on. The full books are available on liveBook and at manning.com.