{"slug": "rag-memory-systems", "title": "RAG - Memory Systems", "summary": "A developer explains how to implement memory systems for retrieval-augmented generation (RAG) in large language models, distinguishing between short-term memory (using Redis, Valkey, Memcached, or Cosmos) and long-term memory (using Postgres, Pinecone, or MongoDB). The post details episodic, semantic, and entity fact memory types, and recommends storing summaries and relevant facts rather than entire conversations to optimize token usage.", "body_md": "We need memory to store the previous conversational history.\n\nFor example:\n\nPrevious question is:\n\n**User:** File handling in Python\n\n**Assistant:** Explain about file handling.\n\nNext time, the user asks:\n\n**User:** What are the modes in it?\n\nThe LLM should understand the context and then respond.\n\nBelow are the details that can be stored in the memory.\n\nBased on the details we are storing, we will choose between long-term or short-term memory.\n\nRedis, Valkey, Memcached, and Cosmos, which are cached databases, can be used for short-term memory to store the last few conversations or a summary.\n\nWe can set a general data invalidation rule to erase the content or use an **LRU cache eviction policy**, where the least recently used data will be erased from the database.\n\nPostgres, Pinecone, and MongoDB can be used for long-term memory to store long conversation histories.\n\nA summary of the entire conversation history will be stored in short-term memory to reduce latency whenever needed.\n\nEpisodic memory is a type of memory that stores specific events or experiences that happened in the past, usually together with information about what happened, when it happened, and the context surrounding it.\n\nWe can use either a short-term or long-term memory database depending on the use case. It is a kind of combination of short-term and long-term memory.\n\n**User:**\n\nI am planning a trip to Paris.\n\n**Agent:**\n\nHow many days will you stay?\n\n**User:**\n\n5 days.\n\nLater,\n\n**User:**\n\nCan you suggest an itinerary?\n\nUser wants to travel to Paris.\n\nTrip duration: 5 days.\n\nUser previously mentioned:\n\nDestination = Paris\n\nDuration = 5 days\n\nThis information can be used to provide a more relevant response.\n\nThis helps the LLM understand what happened previously.\n\nSemantic memory contains facts extracted from previous conversational history. Semantic memory is generally considered long-term memory.\n\nParis is the capital of France.\n\nThe Louvre is a museum in Paris.\n\nFrance uses the Euro.\n\nThat's general knowledge.\n\nIt is a short-term memory. Here, we store the last 3 to 4 conversations.\n\nRedis or Valkey, like any cache memory, can be used.\n\nEach and every time, the conversation, which includes the user query and response, will be summarized.\n\nEven though token consumption during summarization is more, overall token consumption will be less.\n\nIt is a long-term memory.\n\nThis memory is used to store facts about a particular entity.\n\nThe difference between entity fact memory and semantic memory is that **semantic memory is the broader category**. Entity fact memory is one way of organizing and storing semantic knowledge about specific entities.\n\nIt can be used as either long-term or short-term memory.\n\n**Entity: Python**\n\nPython → is a programming language\n\nPython → is used for AI\n\nPython → supports object-oriented programming\n\nPython → was created by Guido van Rossum\n\n**Entity: Alice**\n\nAlice → works at ABC Company\n\nAlice → prefers Python\n\nAlice → is working on Project X\n\nIt is not a good practice to store the entire conversation. We can make decisions based on the conversation and then store the relevant information. This is a good practice.", "url": "https://wpnews.pro/news/rag-memory-systems", "canonical_source": "https://dev.to/ramya_perumal/rag-memory-systems-17aj", "published_at": "2026-08-13 18:08:59+00:00", "updated_at": "2026-08-13 18:18:49.651034+00:00", "lang": "en", "topics": ["large-language-models", "artificial-intelligence", "ai-infrastructure", "ai-agents", "developer-tools"], "entities": ["Redis", "Valkey", "Memcached", "Cosmos", "Postgres", "Pinecone", "MongoDB", "Guido van Rossum"], "alternates": {"html": "https://wpnews.pro/news/rag-memory-systems", "markdown": "https://wpnews.pro/news/rag-memory-systems.md", "text": "https://wpnews.pro/news/rag-memory-systems.txt", "jsonld": "https://wpnews.pro/news/rag-memory-systems.jsonld"}}