Build an AI Agent with Memory from Scratch: Full-Stack with Python, Flask, and DeepSeek

Build a memory-enabled AI Agent with Python and Flask — the model decides what to remember and recalls it across sessions.
This article walks through a beginner-friendly AI Agent project that gives a large language model long-term memory using a Python/Flask backend and a vanilla frontend. The key design lets the model autonomously decide what conversation content is worth saving, persists it to the file system, and automatically retrieves it when relevant topics arise in future sessions — keeping conversations coherent across restarts. The tech stack is intentionally minimal to keep every step of the memory pipeline visible. The article also outlines how to extend this foundation: swapping file storage for a vector database enables semantic retrieval, and more refined scoring logic improves memory quality.
Why AI Agents Need a Memory System
Large language models are stateless by nature — once a conversation ends, the model retains nothing of what was said. That's why so many chatbots completely "forget" previously discussed project context, technical preferences, or key decisions the moment you start a new session. For an AI to truly function like an assistant that understands its users, the application layer must provide it with a memory mechanism.
The core idea behind this project is to give an agent long-term memory through code — and it does so in a clever way: the model itself decides what's worth remembering. When a conversation touches on project background, technical details, or key decisions, the model autonomously determines whether to store that information. The next time a user opens a new session or revisits the page and brings up a related topic, the system automatically retrieves the previously saved memories and surfaces them.
This "selective storage + on-demand retrieval" design is far more efficient than naively stuffing the entire conversation history into every context window. It also more closely mirrors how human memory works — only the important things are retained.
From a technical standpoint, AI Agent memory systems are typically divided into four categories: In-Context Memory (information within the current conversation window); External Memory (information persisted to external storage); Episodic Memory (records of specific events); and Semantic Memory (abstracted knowledge and preferences). This project implements the most practical form — long-term memory — by writing key information outside the model's context window and into the file system. This overcomes two fundamental limitations of large language models: finite context length and session-end amnesia. It's also the core problem that mainstream Agent frameworks like LangChain's Memory module and MemGPT are built to solve.
Project Tech Stack Breakdown
The project uses a front-end/back-end separated full-stack architecture with a deliberately minimal tech stack, making it an ideal hands-on project for anyone getting started with AI Agent development.
The backend is built on Python, using the lightweight web framework Flask to handle requests and routing. For model capabilities, the project supports both OpenAI and DeepSeek APIs, allowing developers to switch between them based on cost and requirements. Memory persistence is handled through simple file I/O — no database required — keeping the barrier to entry low.

The frontend uses the vanilla trio of HTML + CSS + JavaScript, communicating with the backend via the native Fetch API. No heavy frameworks like React or Vue are introduced, which makes the entire data flow fully transparent and easy to follow — from sending requests and handling responses to rendering the page.
For learners who want to understand the underlying principles of Agents, this "framework-free" approach is actually an advantage. You can clearly see how every piece of the memory mechanism fits together.
Demo: Seeing Memory in Action
The browser interface is divided cleanly into two panels. The left side shows the standard session history — the current conversation flow. The right side is where this project shines: long-term memories generated by the model based on its own judgment.

The demo flow is intuitive. Start by sending the model a greeting like "Hello" and wait for its reply. Then ask "What historical messages are there?" — this triggers a memory retrieval function. Under the hood, this step calls an internal method to fetch previously stored memories.

Once retrieved, the system bundles the memory content together with the user's current message and sends them to the model. With this complete context in hand, the model can respond in a way that reflects the historical information. This loop of "retrieve memory → assemble context → call model" is the core logic of the entire memory system.
Critically, even after refreshing the page and starting a brand-new session, the system can still automatically recall relevant memories whenever the user brings up a previously recorded topic — keeping the conversation coherent across sessions.

The Design Value of a Memory System
From an engineering perspective, this project is simple in implementation yet demonstrates several key design principles of AI Agent memory systems.
Selective storage is the first key point. Letting the model decide what's worth remembering — rather than mechanically saving every message — reduces both storage and context costs while improving the signal-to-noise ratio of stored memories. On-demand retrieval is the second — memories are only fetched when the user raises a relevant topic, avoiding the overhead of injecting a lengthy history into every request and keeping token usage under control.
Cross-session persistence addresses the fundamental pain point of model statelessness. By storing memories in the file system, nothing is lost when a session ends, and users get a continuous, coherent experience every time they return.
This approach is also extensible: replacing file storage with a vector database enables semantic search; swapping simple keyword matching for more sophisticated rules or model-based scoring improves memory quality. Mastering this minimal viable version lays the groundwork for building more sophisticated agent memory systems.
Vector databases are a key component for taking memory systems to the next level and deserve a brief explanation. Unlike file storage with keyword matching, a vector database converts text into high-dimensional numerical vectors (embeddings) and retrieves semantically similar content by computing cosine similarity between vectors. This means that even if the user's query is phrased differently from the stored memory, the system can still surface relevant information — for example, a user asking "what framework did we discuss?" could retrieve a memory stored as "prefers using FastAPI." Popular vector databases include Chroma, Pinecone, and Weaviate. In production-grade Agents, memory retrieval typically combines both embedding-based search and keyword filtering to balance semantic coverage with precision.
Who Is This For?
This tutorial is aimed at learners with a basic understanding of Python who want to get started with AI Agent development. It doesn't chase a flashy tech stack — instead, it takes the most straightforward path to making the abstract concept of memory concrete and runnable. By following along step by step, you'll end up with a chatbot that actually remembers things, and more importantly, you'll gain a genuine understanding of how agent memory works under the hood.
Related articles

Prompt → MCP → Agent → Skill: The AI Terminology Evolution Chain Explained in 5 Minutes
A clear guide to five core AI concepts — Prompt, MCP, Agent, Skill, and Cowork — and how they connect in a layered evolution chain from simple instructions to multi-agent teamwork.

OpenAI Discloses Model Anomalies, DeepMind Launches AGI Forum, NVIDIA Partners on Grid Power Management
Sept 17 AI roundup: OpenAI publishes model anomaly disclosure framework with 6 reports, Google DeepMind launches AGI public forum, NVIDIA leads AI energy management alliance with 18 partners.

Build a Local AI Agent with Python in 10 Minutes: Ollama + PydanticAI in Action
A hands-on guide to building a fully local AI agent with Python, Ollama, and PydanticAI in 10 minutes — covering model selection, tool functions, and conversation loops.