Memory Systems for Long-Running Agents: Episodic, Semantic, and Working Context
Human software engineers do not reset their cognitive state to blank zero when they sit down to code in the morning. They draw upon a multi-tiered memory architecture: working memory (the function currently being debugged), semantic memory (language syntax and library patterns), and episodic memory (the specific design choices and bug resolutions completed over past weeks).
For autonomous AI agents to sustain multi-day, long-horizon software projects, they must move beyond stateless prompt loops to Three-Tier Memory Architectures.
flowchart TD
subgraph Agent Runtime
LLM["Agent Reasoning Core"]
Working["Tier 1: Working Memory (Active Context Window / Scratchpad)"]
end
subgraph Persistent Storage
Episodic["Tier 2: Episodic Memory (Changelogs, Past Decisions, Git History)"]
Semantic["Tier 3: Semantic Memory (Domain AST Graph, Shared Coding Standards)"]
end
LLM <--> Working
Working <-->|Retrieve Relevant Past Slices| Episodic
Working <-->|Query Symbols & Rules| Semantic
Working -->|Consolidate & Persist at Turn End| Episodic
1. Deconstructing the Memory Tiers
| Memory Tier | Storage Medium | Retention Horizon | Primary Function |
|---|---|---|---|
| Tier 1: Working Context | LLM Context Window ($32\text{k} - 128\text{k}$) | Current Turn / Task | Active file diffs, immediate compile outputs, step instructions |
| Tier 2: Episodic Memory | Append-Only Vector/Relational DB | Weeks to Months | Previous bug fixes, user design corrections, architectural decisions |
| Tier 3: Semantic Memory | AST Code Graph & Vector Index | Permanent | Public APIs, project architecture rules, style guidelines |
2. Memory Consolidation and Sleep Cycles
If an agent simply dumps every conversation turn into episodic memory, the index quickly becomes contaminated with outdated assumptions and dead-end experiments.
To maintain memory hygiene, production agent harnesses implement Consolidation Cycles (inspired by biological sleep):
- Task Conclusion Trigger: When a major milestone is verified, a lightweight summarization worker extracts key insights:
* What was the core problem?
* Which attempted approaches failed and why?
* What was the final accepted fix?
- De-duplication and Pruning: Redundant episodic snippets are merged using vector clustering, discarding transient compiler noise.
- Rule Crystallization: If a user corrects an agent multiple times on the same behavioral constraint (e.g. "Never use default exports" or "Always format timestamps in UTC"), the rule is promoted from episodic memory into permanent semantic guidelines.
3. Implementation Blueprint for Agent Engineers
- Treat git commits as natural episodic anchors: Use
git log --statand pull request descriptions as structured episodic memory snapshots. - Store decisions with rationales, not just diffs: A patch without an explanation of why alternatives were rejected is difficult for future agents to interpret.
- Keep retrieval selective: Query episodic memory using hybrid vector + keyword matching only when initiating a new subtask, injecting at most 2–3 relevant past case studies into working memory.