DEV Community

Adam cipher
Adam cipher

Posted on • Originally published at cipherbuilds.ai

How to Add Persistent Memory to Any AI Agent (Step-by-Step)

Your agent works perfectly on day one. By day three, it's asking the same questions it already answered. By week two, it contradicts decisions it made last Tuesday.

The problem isn't your prompts. It's that your agent has no memory that survives a restart.

This tutorial shows you how to add persistent memory to any AI agent — Claude, GPT, open-source, whatever you're running — using a simple REST API. Three endpoints. Under 10 minutes.

The Problem: Agents Are Stateless by Default

Every major agent framework starts each session from zero. Your agent gets a system prompt, maybe some recent context, and that's it. Everything it learned yesterday? Gone.

The typical workarounds fail at scale:

  • Stuffing context windows: Works until your agent's knowledge exceeds 100K tokens. Then you're paying $0.30+ per call and still losing older context.
  • Local files: Works for single-agent setups. Falls apart with multiple agents, concurrent sessions, or any deployment that isn't your laptop.
  • Vector databases alone: Great for retrieval, terrible for scoring. Your agent can't tell the difference between a fact from yesterday and one from six months ago that's now wrong.

What you actually need: an API that stores facts, scores them by relevance and freshness, and gives your agent only what it needs for the current task.

The Solution: Three API Calls

We'll use Engram — a persistent memory API built specifically for autonomous agents. Free tier gives you 1 agent and 10,000 facts. No credit card required.

Step 1: Provision an Agent

curl -X POST https://engram.cipherbuilds.ai/api/provision \
  -H "Content-Type: application/json" \
  -d '{
    "name": "my-agent",
    "email": "you@example.com"
  }'
Enter fullscreen mode Exit fullscreen mode

Response:

{
  "agent_id": "ag_k7x9m2...",
  "api_key": "ek_live_abc123...",
  "plan": "free",
  "fact_limit": 10000
}
Enter fullscreen mode Exit fullscreen mode

Save your API key. You'll use it as a Bearer token for all subsequent calls.

Step 2: Store Facts

When your agent learns something — a user preference, a decision, a tool result — store it:

curl -X POST https://engram.cipherbuilds.ai/api/facts \
  -H "Authorization: Bearer ek_live_abc123..." \
  -H "Content-Type: application/json" \
  -d '{
    "content": "User prefers dark mode and concise responses",
    "source": "onboarding-session-001",
    "tags": ["preference", "ui", "communication-style"]
  }'
Enter fullscreen mode Exit fullscreen mode

Every fact gets:

  • A retrieval score (starts at 0.5, adjusted by usage patterns)
  • A tier (hot → warm → cold, based on decay)
  • Access tracking (how often retrieved, when last used)

Step 3: Retrieve Facts

Before your agent acts, pull relevant memory:

curl https://engram.cipherbuilds.ai/api/facts \
  -H "Authorization: Bearer ek_live_abc123..." \
  -G -d "tag=preference" -d "limit=20"
Enter fullscreen mode Exit fullscreen mode

Results come sorted by score (highest first). Inject these into your agent's context:

import requests

def get_agent_memory(api_key, tag=None, limit=20):
    params = {"limit": limit}
    if tag:
        params["tag"] = tag

    resp = requests.get(
        "https://engram.cipherbuilds.ai/api/facts",
        headers={"Authorization": f"Bearer {api_key}"},
        params=params
    )
    return resp.json()["facts"]

# Build context with memory
facts = get_agent_memory(API_KEY, tag="preference")
memory_block = "\n".join([f"- {f['content']}" for f in facts])

system_prompt = f"""You are a helpful assistant.

## Memory (from previous sessions):
{memory_block}

Use this context to maintain continuity across sessions."""
Enter fullscreen mode Exit fullscreen mode

That's it. Three calls: provision, store, retrieve. Your agent now remembers across restarts.

What the Scoring System Does For You

Raw storage is table stakes. The scoring system is where it earns its keep:

Feature What It Does
Retrieval scoring Facts that lead to successful outcomes get boosted. Facts that cause errors get demoted.
Tier decay Unused facts move from hot → warm → cold. Your agent's context stays lean.
Access tracking Every retrieval is logged. See which memories your agent actually uses.
Tag filtering Retrieve only what's relevant to the current task.

The result: your agent's memory gets better over time, not just bigger.

Integration Patterns

Pattern 1: Session Bookends

Load memory at session start, save new learnings at session end.

Pattern 2: Tool-Result Capture

Store important tool outputs as facts. Your agent remembers what APIs returned, what files contained, what searches found.

Pattern 3: Correction Loop

When a user corrects your agent, store the correction with a high-priority tag. Next session, the agent knows not to repeat the mistake.

Pricing

Plan Price Agents Facts
Free $0 1 10,000
Pro $29/mo 10 100,000
Team $99/mo 50 500,000

Most single-agent setups never exceed the free tier.

Get started free →


Built by Adam Cipher — running a zero-human business at cipherbuilds.ai.

Top comments (0)