◉CLAUDE LABJP
●2.1.301 — Adds the vim % motion, LaTeX math rendering in the VS Code panel, and a retry-watchdog limit for usage-limit waits (10/15)●2.1.299 — Conversations over 200K tokens left idle about 54 minutes get compacted; MCP allow rules now match only the server they name●SONNET 4.5 — Retires on the Claude API on 11/30, 45 days left. Migrate to Sonnet 5.5●WINDOWS — Reports say hooks break silently only on Japanese-locale Windows; check the character encoding first●FAST MODE — Opus 5.5 fast mode is out of research preview and no longer needs the beta header (10/15)●NEW — I Put a Ten-Line Promise File at the To…●2.1.301 — Adds the vim % motion, LaTeX math rendering in the VS Code panel, and a retry-watchdog limit for usage-limit waits (10/15)●2.1.299 — Conversations over 200K tokens left idle about 54 minutes get compacted; MCP allow rules now match only the server they name●SONNET 4.5 — Retires on the Claude API on 11/30, 45 days left. Migrate to Sonnet 5.5●WINDOWS — Reports say hooks break silently only on Japanese-locale Windows; check the character encoding first●FAST MODE — Opus 5.5 fast mode is out of research preview and no longer needs the beta header (10/15)●NEW — I Put a Ten-Line Promise File at the To…
Articles/API & SDK
⬡ API & SDK/2026-04-06Advanced

Building a Persistent Memory Agent with Claude API, pgvector, and Redis: A Complete

A complete guide to building production-ready persistent memory for Claude API agents using PostgreSQL + pgvector + Redis. Learn vector search, layered memory architecture, session management, and GDPR-compliant data handling.

Claude API128pgvectorPostgreSQLRedismemory9agent14vector searchRAG4

✦ Premium Article

Why Your AI Agent Needs Memory

If you've built chatbots or agents with the Claude API, you've likely run into this frustration: users have to re-explain themselves every session. Preferences, past decisions, ongoing projects—gone the moment a conversation ends.

The Claude API is stateless by design. Each request stands alone, and any prior context must be explicitly included in the messages array. This makes the API clean and composable, but it means you are responsible for building a memory system if you want your agent to feel intelligent over time.

This guide walks you through building a production-grade persistent memory system using:

  • PostgreSQL + pgvector — long-term memory with semantic vector search
  • Redis — short-term session memory with fast in-memory access
  • Claude API — reasoning engine that autonomously invokes memory tools

By the end, you'll have an agent that remembers user preferences, references past conversations, and continuously improves its responses the more it interacts with each person.


Architecture Overview: A Three-Layer Memory System

Human memory isn't monolithic. We have working memory that lasts seconds, episodic memory that captures experiences, and long-term semantic memory that persists for years. Your AI agent's memory can follow the same layered model.

Layer 1 — Short-Term Memory (Redis)

Stores the current session's message history. Expires via TTL (time to live) once the session is inactive, keeping Redis lean and cost-efficient. Redis is the right tool here because access speed is the priority—agents frequently look back at the last few messages.

Layer 2 — Long-Term Memory (PostgreSQL + pgvector)

Stores important information extracted from conversations: user preferences, key facts, learned skills, notable events. Each memory is embedded as a vector, so retrieval uses cosine similarity rather than exact keyword matching. This means queries like "does this user like concise answers?" return the right memories even if those exact words were never used.

Layer 3 — Episodic Memory (PostgreSQL)

Stores session summaries with timestamps. This enables recalling "what we discussed last month" at a coarser granularity, without storing every individual message forever.

Together, these three layers balance speed, precision, and storage cost for a realistic production system.


✦

Thank you for reading this far.

Continue Reading

What follows includes implementation code, benchmarks, and practical content we hope you'll find useful. This site runs without ads — server and development costs are supported entirely by members like you. If it's been helpful, we'd be truly grateful for your support.

WHAT YOU'LL LEARN
✦The measured recall vs. p95 latency trade-off when tuning HNSW ef_search from 40 to 100
✦Cutting embedding API calls by roughly 60% with a Redis cache keyed on the query hash
✦Importance-score decay and 0.95-similarity dedup patterns that keep the memory table from bloating in production
Secure payment via Stripe · Cancel anytime
✦

Unlock This Article

Get full access to the rest of this article. Buy once, read anytime. This site is ad-free — your support goes directly toward keeping it running.

or
Unlock all articles with Membership →
Share

Thank You for Reading

Claude Lab is ad-free, supported entirely by members like you. We publish practical guides daily with implementation code, benchmarks, and production-ready patterns. If you've found it useful, we'd love to have you on board.

  • ✦Copy-paste ready implementation code
  • ✦New advanced guides published daily
  • ✦$5/mo or $10 for lifetime access
View Membership →

Related Articles

⬡ API & SDK2026-10-16
Sending 40 Rows From Make to the Claude API Stops at Row 14 — Avoiding the 429 With One Subtraction
Why a no-code automation that sends spreadsheet rows to the Claude API one by one hits a 429 partway through, and how to fix it. Seven spacing strategies tested on a mock bucket, a formula for the Sleep delay, and settings that stop failed rows from vanishing.
⬡ API & SDK2026-10-13
Haiku 5.5 is cheaper per token, but your bill jumps fivefold past about 77k Haiku 4.5 tokens
On the price sheet, Haiku 5.5 costs roughly a tenth of Haiku 4.5. But the same text becomes about 30% more tokens, and a prompt over 100k tokens is billed at five times the rate. Here is the boundary in Haiku 4.5 terms, a script I actually ran, and the changes that return a 400.
⬡ API & SDK2026-10-08
Before Sonnet 4.5 retires, one script to find the arguments that will return a 400
claude-sonnet-4-5 retires on November 30. Swapping the model ID is not enough: budget_tokens, prefill, and tool_choice will fail. Here is a scanner built from the official migration guide, its real output, and the order I would fix things in.