Contextual memory for an AI chatbot: session, Redis, vector search

Does the dialogue with the bot fall apart as soon as the client returns to something said earlier? We design and implement contextual memory for an AI assistant turnkey: the bot keeps the thread of the conversation, remembers details, and does not mix up facts. The client gets predictable answer quality and fewer manual edits.
Frequently Asked Questions

Why an AI bot loses dialogue context and confuses facts

Loss of dialogue context almost always manifests in the same way: the bot re-asks what the client has already stated, swaps numbers from previous replies, and gives contradictory answers within a single session.

The cause is not a "weak model" but the fact that state is not preserved between requests — each call goes out as the first one, and the model fills in the gaps with a plausible guess.

The second failure is the mixing of facts from different sources. If relevant fragments of the knowledge base are mixed in without being tied to the current session, the bot pulls the terms of one plan into a question about another and confidently answers incorrectly.

The client sees not a refusal but a plausible error — and loses trust faster than with an honest "I don't know."

The third typical case is the growth of history. When the entire dialogue goes into every subsequent request, responses slow down, and important details from the beginning of the conversation drown in the noise of later replies.

The bot begins to contradict itself: it confirmed deadlines at the start, and ten messages later named different ones.

AI contextual memory closes this at three levels: a short window of recent replies for coherence, a session state store for facts named by the client, and semantic search for pulling in the needed fragments of the knowledge base.

Let's break down how this works and where the line runs between "remembers" and "hallucinates."

What managed contextual memory gives an AI assistant

Managed memory splits context into tiers: short-term session history in Redis with a configurable time-to-live, and long-term facts about the client in a vector store with semantic similarity search.

For each request, a working set is assembled: the latest turns of the dialogue, relevant fragments of past interactions, and the client profile. The model receives not the entire log, but only what relates to the current question.

The practical effect is resilience over long dialogues. When history is not blindly truncated by message count, the share of correct answers at the twentieth or thirtieth turn stays at the level of the first ones instead of dropping by half.

The answer arrives faster: fewer tokens go into the prompt, and response time stays steady even under a dense flow of requests.

The second effect is fewer repetitions and escalations. The client does not retell what they have already said, and the assistant does not ask the same clarifying question again.

The share of dialogues that reach a human operator drops because the bot carries typical scenarios through to completion on its own.

We build such solutions on the Redis and vector search pairing, proven in projects on LangChain — this delivers predictable memory behavior under load.

Manageability matters more here than volume: retention policies, the priority of recent turns, and rules for evicting outdated context are tuned to the scenario.

Memory can be purged on demand, capped per client, and checked for reproducibility — without this, answer quality remains a lottery.

Session memory, cache, and vector search: comparing approaches

Dialogue context can be stored in four ways, and the choice determines what the bot remembers between messages.

Session memory in the process lives until the service restarts, an external cache survives a restart but is limited by the retention window, vector search retrieves utterances from the entire history by meaning, and a hybrid scheme combines them. Below is a comparison of how each option behaves under load and during node failure.

Option When it fits Limitations
Session in process memory Single instance, short dialogues, prototype Lost on restart and deploy; does not scale to multiple instances — the request goes to another node and the context is empty
External dialogue cache Multiple instances, shared context needed, window within the session Stores only the latest utterances; when the key expires, the history breaks off; volume control is needed, otherwise the cache grows
Vector search over history Long dialogues, questions about past messages, knowledge base More expensive in latency per request; requires embeddings and an index; without a freshness filter it pulls in outdated utterances
Hybrid scheme Production with long history and multiple instances Harder to maintain: cache and index consistency, write and eviction strategy are needed

In practice, the working configuration is a hybrid: we keep fresh utterances in a shared cache with a window of several steps, and everything older goes into the vector index and is pulled in by the meaning of the request.

This way the bot responds taking into account both the latest message and the agreements from the beginning of the dialogue, and memory does not grow uncontrollably. Let's break down how this is handled for a specific load.

How we implement contextual memory: stages of work

Implementing bot memory starts not with code, but with analyzing scenarios: which dialogue branches live for minutes, and which should remember the client between visits.

This determines the context storage scheme — what we keep in the active session, what we move to long-term storage, and what shouldn't be saved at all.

  1. Analyzing scenarios and memory boundaries. We go through real dialogues and mark where the bot loses the thread: returning to something said several messages ago, asking again, switching topics. We determine what memory horizon each scenario needs — from a single session to a permanent client profile.
  2. Designing the storage scheme. We break context down into layers: short session, long-term profile, history of interactions. We define keys, record lifetimes, and cleanup rules so the storage doesn't grow uncontrollably.
  3. Setting up sessions and cache. We bind the session to the user and channel, set the lifetime and behavior on connection loss. The cache keeps the active dialogue at hand, so the response comes without the delay of reading from the main storage.
  4. Connecting history search. Dialogues are indexed, and the bot pulls relevant fragments of past conversations rather than the entire log. This keeps the response on point and doesn't bloat the request to the model.
  5. Testing dialogues. We run long scenarios: dozens of messages, returns to an old topic, parallel sessions. We look at where memory is lost or, conversely, pulls in too much.
  6. Setting up cleanup and privacy. We define retention periods, masking of sensitive data, and deletion on request — memory shouldn't turn into a dump.
  7. Handover and documentation. We hand over the storage scheme, session rules, and instructions for edits so the team can develop the bot without us.

Each stage ends with a verifiable result, not a report. Describe your scenarios — we'll come back with an estimate and a work plan.

Storage schema, access policies, and cleanup regulations

Contextual AI memory is not just code, but also a set of artifacts that remain with the client and outlive any development team.

The layer schema, rules for accessing dialogue history, retention periods, and cleanup regulations are documented in writing: when a contractor changes, during an audit, or during an inspection, new engineers deploy the bot's memory based on documents, not on the memory of departed people.

The handover package includes:

  • Memory layer schema — what is stored in the runtime session, what is stored in Redis with TTL, what goes to the vector store with embeddings, and how the layers are linked by keys.
  • Access policies — roles (support operator, analyst, administrator), which history fields are visible to them, how rights are granted and revoked, and where the log of access to dialogues is kept.
  • Retention periods by data type — separately for session context, separately for long-term memory, separately for logs and metrics, with justification for each period.
  • Cleanup regulations — rules for automatic deletion by TTL, cascading deletion of related records, manual cleanup upon user request, and confirmation of the deletion.
  • Depersonalization procedure — replacing personal data with pseudonyms before writing to the vector index, so that search works while identifiers do not leak.
  • Backup schema — what is backed up, at what frequency, and how memory is restored after a node failure.
  • Migration instruction — transferring history when changing the model or storage without losing accumulated context.

Each item is a working document, not a verbal description: with diagrams, configuration examples, and a verification checklist.

The client gains the ability to change retention rules themselves without waiting for the developer, and to prove compliance with personal data requirements.

Case study: long dialogues without losing context

The task was a typical one for support: the bot responded based on the most recent messages, and everything said earlier was lost.

On dialogues exceeding twenty turns, the share of correct answers dropped to 61%, clients re-asked the same thing 2-3 times, and every fourth dialogue ended with a handoff to an operator — not because the question was complex, but because the bot didn't remember the context.

What we changed

We split memory into two levels. The short session — the most recent turns of the dialogue — is kept in Redis with a time-to-live limit and eviction by recency: it provides instant response and doesn't grow indefinitely.

Long-term memory — facts, agreements, order numbers — is moved to vector search: before each response, the system retrieves relevant fragments of past interactions and mixes them into the request to the model.

The key is the write rule. It's not the entire dialogue that goes into long-term memory, but only extracted entities and confirmed facts, so the storage doesn't get clogged with junk, and search remains accurate even on a long history. We work on Redis in conjunction with a vector index — the combination has been stress-tested.

What the result was

Metric Before After
Correct answers on dialogues with 20+ turns 61% 89%
Repeat client questions 2-3 times 0-1 time
Response time 4.2 s 1.1 s
Handoff to operator 24% 9%

Let's note the response time separately: the speedup came specifically from Redis — hot data is read from memory, and vector search is only engaged when a fact from history is needed.

The share of handoffs to an operator dropped threefold, and this isn't about a "smarter model," but about the bot no longer losing the thread of the conversation.

How does session memory differ from vector search over history?

Session memory and vector search solve different problems, and confusing them is the source of most issues with bot behavior.

A session stores the latest turns of a dialogue in operational storage such as Redis: the key is tied to the user, the record has a time to live, and the window is limited — usually the last 10-20 messages.

This provides coherence within a single conversation: the bot remembers what you discussed three turns ago and does not ask again.

As soon as the window overflows, older turns are evicted — otherwise the context size grows, and along with it the response time and the cost of calling the model.

Vector search works differently: it does not store the dialogue but retrieves relevant fragments from a knowledge base or message archive.

Each turn is turned into a vector and placed into an index; on a new query, the system looks for the nearest chunks by meaning and mixes them into the context. This pulls out a fact from a conversation from a month ago that would no longer fit into the session.

The limitation is the opposite: search does not know the order of turns and does not distinguish who said what — it returns something similar, not the latest.

The approaches also break in different ways. A session loses the thread when storage restarts without persistence or when the time to live expires — the user returns, and the context is empty.

Vector search produces false positives: for a general question, fragments with high similarity but off-topic are pulled in, and the bot answers confidently but beside the point. Plus the index needs to be updated, otherwise fresh turns do not get into search.

In practice, they are combined: the session holds the current dialogue and the order of turns, while vector search is connected when a fact beyond the window is needed.

A similarity threshold cuts off weak matches, and the found fragments are supplied with a source label so that the model does not mix them with the current turn. This separation of roles eliminates both context breaks and false pull-ins.

Technical concerns: overflow, leaks, storage cost

Context window overflow is not a crash but a normal mode for which memory must be prepared.

When the dialogue history hits the model's limit, we don't cut it "to the quick": a layered scheme works — fresh replies go into the window in full, the middle layer is compressed into a summary, and old facts remain in the vector store and are pulled in by semantic proximity to the current query.

The dialogue continues without losing the thread, and the bot doesn't start responding as if it were seeing the client for the first time.

History protection is about access control and retention rules, not just backups. The session layer lives in Redis with a configurable time-to-live, so abandoned dialogues don't pile up forever and personal data doesn't stay in memory longer than necessary.

Access to history is role-restricted, and actions on it are logged — you can see who read or changed data and when.

Memory growth is limited by three things: session time-to-live, a policy for evicting old records, and deduplication of repeated facts before writing to the vector store. Without them, the database grows linearly with the number of dialogues, and search over it slows down. With these settings, the volume stabilizes at a working level instead of growing indefinitely.

What happens if the context window overflows right during a dialogue?

Nothing critical: summarization and retrieval of relevant facts happen on the fly, and the response is formed from the compressed history and semantic matches. The client doesn't notice the break, and the logs record the moment of compression — this is visible during review.

Can changes in memory be rolled back if the bot "remembered" something it shouldn't have?

Yes. Records are versioned, and an unsuccessful fact is deleted pointwise — without rebuilding the entire database. For sensitive scenarios, we set up manual confirmation before writing to long-term memory.

How can memory growth be limited without losing response quality?

Through a combination of session time-to-live, eviction of outdated records, and deduplication. What matters stays, duplicates collapse, the volume stays at a stable level — search doesn't slow down as dialogues accumulate.

Let's discuss the task and come back with an estimate and a plan

To come back with an estimate and an implementation plan, rather than generalities, we need a minimum of source data about your scenario.

Contextual memory of an AI bot is not a single setting, but a combination: where the dialogue history is stored, how it is compressed during long conversations, what goes into vector search, and by what rules outdated information is forgotten.

The more accurately you describe the current state, the more realistic the plan will be and the fewer revisions will surface during implementation.

Send us what you already have — that is enough for us to calculate the scope of work and propose an architecture:

  • Bot scenarios: what types of requests it handles, where end-to-end context between sessions is needed, and where memory within a single dialogue is sufficient.
  • Current memory scheme: whether the history is stored in a database, in a cache, or only in the prompt; how context is currently passed between messages.
  • Dialogue length constraints: what happens when the window overflows — truncation, summarization, or loss of earlier replies.
  • Data for search: which documents, knowledge base, or catalog should go into the vector index and how often they are updated.
  • Load: approximate number of simultaneous dialogues, average session length, peak periods.
  • Integrations: messengers, CRM, internal systems that the bot accesses for data.
  • Privacy requirements: which data cannot leave the perimeter, whether memory isolation by clients or departments is needed.
  • What has already been tried: current problems with answer quality, complaints about "forgetting" context or, conversely, about leaking excess information.

Describe the task in free form — there is no need to prepare a technical specification.

Our engineers will analyze the scenarios, propose a context storage scheme, and come back with a work estimate and a phased implementation plan: from a memory prototype to rollout into production with answer quality monitoring.

AI Development Areas

Latest works

  • Development of a web application for FEEDME
    Development of a web application for FEEDME
    1393
  • Development of an online store for the company FURNORO
    Development of an online store for the company FURNORO
    1368
  • B2B Advance company logo design
    B2B Advance company logo design
    815
  • Development of a web application for Enviok
    Development of a web application for Enviok
    1139
  • AIDER company logo development
    AIDER company logo development
    1057
  • CRM development for Chasseurs
    CRM development for Chasseurs
    1152