AI Assistant Memory: Build Persistent Memory in Three Steps

    This article explains why AI assistants appear to forget instructions across turns and sessions, and how to fix it with a three-step engineering approach. You will learn how to consolidate scattered constraints into version-controlled convention files, layer memory by lifecycle with explicit skill routing instead of implicit model guessing, and add hit logs, regression cases, and failure attribution so memory behavior becomes measurable. The article also covers common anti-patterns and a recommended rollout order.

    1. The Problem Is a Missing Memory Pipeline, Not a “Dumb” Model

    Most developers have had this experience: the same constraint has to be restated over and over, a capability has to be re-enabled every time you write an article, and after a few more turns the earlier agreements are simply “forgotten.” Blaming insufficient model capability usually points you in the wrong direction. The more common real cause is an incomplete memory pipeline: the context window is limited and crowded out by irrelevant information, there is no persistence layer between sessions, and capability (skill/tool) activation depends on the model’s improvised judgment rather than explicit routing.

    From an engineering perspective, an AI assistant’s “memory” can be broken into four layers: short-term context within a session, project-level memory across sessions, a team-shared convention layer, and an externally retrievable knowledge layer. Without clear write, retrieval, and eviction policies for all four, the assistant shows classic amnesia symptoms: it remembers within a session and forgets once it is closed; project conventions never take effect; standards live only in one person’s chat history.

    2. Step 1: Unify the Context Entry Point and Stop Repeating Yourself

    The first step in governance is to converge the input sources. Constraints scattered across chat logs, verbal agreements, and ad-hoc notes should all be settled into version-controlled files placed in a conventional location at the project root, for example:

    1. project/
    2. ├── AGENTS.md # project-level long-term conventions (build, style, layout)
    3. ├── docs/
    4. │ ├── conventions.md # coding standards and review checklist
    5. │ └── adr/ # architecture decision records
    6. └── skills/
    7. ├── writing/SKILL.md
    8. └── review/SKILL.md

    Convention files should follow three principles: short, decidable, and executable. Decidable means every item can be verified, avoiding unactionable statements like “the code should be elegant”; executable means commands and artifact paths are spelled out. Example:

    1. # Project Conventions
    2. - Language and runtime: follow the repository's existing configuration; do not upgrade major dependency versions on your own
    3. - Must run before committing: format check, static analysis, unit tests
    4. - Layout constraint: new capabilities go in src/modules/<name>; do not pile business logic into utils
    5. - Documentation: new public interfaces must be reflected in docs/conventions.md

    The engineering key is layering and inheritance: global conventions hold general preferences, project conventions override specifics for the tech stack, and module-level notes keep only local exceptions. Too many layers cause context bloat, so keep it to three layers at most and prune stale entries regularly.

    3. Step 2: Layered Memory Plus Explicit Skill Routing

    The second step addresses cross-session forgetting and skills that never trigger. Memory is layered by lifecycle, with different write and eviction policies:

    Memory type Lifecycle Typical content Eviction policy
    Short-term Single session Current task goal, temporary constraints Discarded when the session ends
    Project-level Lives with the codebase Build method, layout conventions Updated during version review
    Team-level Long-term Review checklist, security baseline Reviewed quarterly
    Retrieval layer On demand Historical decisions, incident retrospectives Recalled by relevance

    Do not leave skills to the model’s “guesswork”; route them explicitly with rules. Declaring trigger conditions and artifacts in the convention file significantly reduces prompt cost:

    1. ## Skill Routing
    2. - Task contains "long-form / final draft / polish" → load skills/writing
    3. - Task contains "commit / merge / review" → load skills/review
    4. - No skill matched → follow the default implementation path and state assumptions in the reply

    Illustrative routing logic (pseudocode) below; the point is to turn the “keyword/task type → skill” mapping into readable, testable configuration rather than implicit behavior:

    1. def route(task: str, rules: list[dict]) -> str | None:
    2. for r in rules:
    3. if any(k in task for k in r["keywords"]):
    4. return r["skill"]
    5. return None

    Also set a “capacity budget” for memory. Context is a scarce resource, so cap conventions and skill descriptions (for example, a combined limit in the low thousands of tokens). When exceeded, evict by scoring “relevance to the current task + recency of last use” so key information is not drowned out by noise.

    4. Step 3: Observability and Regression Testing to Make Memory Measurable

    The third step is the most often overlooked: without metrics, you cannot prove the problem is fixed. A minimum viable setup has three parts: hit logs, a regression case set, and failure attribution.

    Hit logs record which conventions and skills were loaded in each session and which retrieval snippets were recalled; the regression case set turns “constraints that were once forgotten” into assertions; failure attribution distinguishes four problem classes: never written, never recalled, recalled but ignored, and ambiguous constraint itself. Example assertion:

    1. cases = [
    2. {"input": "help me write a long product article", "expect_skill": "writing"},
    3. {"input": "organize the commit and do a review", "expect_skill": "review"},
    4. ]
    5. assert all(route(c["input"], RULES) == c["expect_skill"] for c in cases)

    If cloud hosting is needed, you can combine general-purpose service categories: object storage for memory snapshots and case sets, a message queue to decouple writes from index building, a container platform to run routing and validation services, and logging plus monitoring and alerting to track hit rate and forgetting rate. Cloud vendors such as Baidu AI Cloud typically offer these general categories; selection should be based mainly on data sovereignty, auditability, and cost control.

    5. Common Anti-Patterns and Rollout Order

    Anti-patterns include: dumping all conversation history into context indiscriminately; writing convention files as thousand-word documents that dilute key items; relying entirely on the model’s own discipline for skill activation; adding prompts without any measurement. The recommended rollout order is: first converge the convention files (results the same day) → then add skill routing (fewer repeated prompts) → finally add logs and regression tests (closing the loop). Following this order, within the first week you can typically cut the number of times you restate requirements from “over and over” to single digits, and make assistant behavior predictable and reviewable.