◉CLAUDE LABJP
●CC 2.1.332 — No new Claude Code release over the weekend. 2.1.332 (11/25) is still current●ANALYTICS — A fix to search counts in the Enterprise Analytics API may raise numbers from 11/17 onward●SONNET 4.5 — Today, 11/30, is Sonnet 4.5's last day on the Claude API. Move to claude-sonnet-5-5●USAGE — Some users report weekly usage jumping right after upgrading to a higher plan●NEW — New: Does what a Project remembers carry over into your everyday chats?●BILLING — More readers are looking for what to check when a payment won't go through●CC 2.1.332 — No new Claude Code release over the weekend. 2.1.332 (11/25) is still current●ANALYTICS — A fix to search counts in the Enterprise Analytics API may raise numbers from 11/17 onward●SONNET 4.5 — Today, 11/30, is Sonnet 4.5's last day on the Claude API. Move to claude-sonnet-5-5●USAGE — Some users report weekly usage jumping right after upgrading to a higher plan●NEW — New: Does what a Project remembers carry over into your everyday chats?●BILLING — More readers are looking for what to check when a payment won't go through
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 API150pgvectorPostgreSQLRedismemory9agent14vector 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-11-30
The unit price drops 20%, yet the bill may rise: estimating the Opus 4.5 to 5.5 switch before January 22
claude-opus-4-5-20251101 retires on January 22, 2027, and the official replacement is claude-opus-5-5. Prices fall by a fifth, but the tokenizer changes and thinking can no longer be turned off. You will be able to count the cost per 1,000 of your own requests and pick a replacement and an effort level.
⬡ API & SDK2026-11-25
Five Days Before Sonnet 4.5 Retires: Compare the Three Things That Change Without an Error While the Old Model Still Answers
claude-sonnet-4-5 retires on November 30. Switch the ID to claude-sonnet-5-5 and nothing returns a 400, yet thinking now runs by default, max_tokens covers more than the answer, and stop_reason shifts. Here is a small script to run the same requests on both models before the old one stops answering, and how to read the differences.
⬡ API & SDK2026-11-18
Before Your Customers Pay Through Linked Billing: Reading a 402 or 403 to Find Whose Wallet Is Empty
Linked billing, released in beta on November 17, lets you choose who pays on every Claude API request through billing_policy. When a request billed to a customer comes back as a 402 or 403, here is how I work out which wallet is the cause, when to pay for it myself, and a small Python wrapper that keeps that decision in one place.