Module 4 · Query engines
Chat engines and conversational memory
4 min read
A support agent doesn't ask an isolated question: they follow up. "What's the warranty on the Axis chair?" then "And for the gas lift?". The second question makes no sense alone: sent as is to retrieval, it finds nothing. A chat engine adds conversation memory to a query engine, and knows how to rephrase a follow-up question into a standalone question. This lesson presents LlamaIndex's chat modes and its memory system.
The follow-up question problem
Agent : Quelle est la garantie de la chaise Axis ?
Assistant : 5 ans sur la structure et le mécanisme, 2 ans sur la maille et les accoudoirs.
[products/chair-7.md]
Agent : Et pour le vérin ?
Searching "Et pour le vérin ?" brings back anything: the question contains neither the product nor the topic (the warranty). You must first condense it with the history into "What is the warranty on the Axis chair's gas lift?", then search.
Chat modes
| Mode | How it works | Use |
|---|---|---|
simple | Direct conversation with the LLM, no retrieval | No interest for a documentation assistant |
context | Retrieves with the last message as is, then answers with the history | Standalone questions |
condense_question | Rephrases the question with the history, then queries a query engine | Follow-up questions, purely documentary answer |
condense_plus_context | Rephrases, retrieves, then answers with the history and the excerpts | The best compromise for an assistant |
Lumen uses condense_plus_context:
def chat_engine(index: VectorStoreIndex, session_id: str) -> CondensePlusContextChatEngine:
memory = Memory.from_defaults(session_id=session_id, token_limit=8000)
return CondensePlusContextChatEngine.from_defaults(
retriever=index.as_retriever(similarity_top_k=config.TOP_K),
llm=models.main_llm(),
memory=memory,
system_prompt="Tu es l'assistant interne du support Nimbus. Réponds à partir des "
"extraits fournis, et dis-le quand l'information n'y figure pas.",
node_postprocessors=[SimilarityPostprocessor(similarity_cutoff=config.SIMILARITY_CUTOFF)],
)
For each message: the LLM condenses the question with the history, the retriever searches with the condensed question, and the LLM answers seeing the history, the system prompt and the excerpts. The test_chat_engine_remembers_context test checks the gas-lift scenario: the second answer does mention the 10-year warranty.
Condensing is one extra LLM call per message. It can be handed to the fast model by passing a dedicated LLM for this step; and it can be skipped (skip_condense=True) when the interface guarantees standalone questions.
Memory
LlamaIndex's Memory object stores a session's history (session_id) and controls its size:
memory = Memory.from_defaults(session_id="agent-42", token_limit=8000)
When the history exceeds the budget, the oldest messages are flushed from short-term memory. They aren't necessarily lost: memory can be complemented by long-term memory blocks that receive what's flushed:
| Block | Role |
|---|---|
StaticMemoryBlock | Fixed information, always present (agent profile, team instructions) |
FactExtractionMemoryBlock | The LLM extracts and keeps durable facts from flushed messages |
VectorMemoryBlock | Old messages are embedded and retrieved when they become relevant again |
Blocks are injected into the model's context, with their own token budget. They let a long session keep the essentials without sending the whole history on every call.
Persisting conversations
By default, memory lives in an in-memory SQLite database: a restart erases conversations. For a service, persist it in a real database, by passing an async database URI (SQLite on disk, PostgreSQL):
memory = Memory.from_defaults(
session_id=session_id,
token_limit=8000,
async_database_uri="postgresql+asyncpg://user:password@host/lumen",
)
Lumen's server keeps one chat engine per session in memory, which is enough for a single-instance demo. In production, with several instances, memory must be shared — otherwise a conversation whose messages land on two different servers loses its context.
Conversation and privacy
A conversation often contains personal data: customer name, order number, address. It's stored in memory, perhaps persisted, sent to the model on every turn, and goes through traces. Set a retention period, isolate sessions (an agent must never load another's session by guessing its identifier) and plan session deletion.
Key takeaways
- A follow-up question must be condensed with the history before retrieval.
condense_plus_context: rephrase, retrieve, answer with history and excerpts — the best compromise.Memorybounds the history in tokens; long-term memory blocks (facts, vectors, static) keep the essentials.- Persist memory in a shared database as soon as there are several instances.
- A conversation contains personal data: limited retention, isolated sessions, deletion possible.
Practice
- Replay the gas-lift scenario with the
contextmode instead ofcondense_plus_context: what happens? - Add a
StaticMemoryBlockstating "The agent works for the after-sales department" and observe its effect on answers. - Persist memory in a SQLite file (
sqlite+aiosqlite:///sessions.db), restart the program and check that the conversation continues.