How to Give AI Agents Real Working Memory: 90% Token Savings Without Quality Loss (graymatter, mcp-memory-service)

I'm Denis Shokhirev, Agentic AI Systems Architect based in Freiburg im Breisgau, Germany. At DennisCraft AI Studio, I deliver agentic AI for DACH B2B clients in logistics, fintech, and industrial automation. My stack: Claude, Supabase, n8n, Doppler, self-hosted Postgres. The biggest pain in production? Giving agents memory that doesn't inflate token bills or degrade reliability.

The Real Memory Problem in Production Agents

If your agent forgets, it repeats tasks, ignores constraints, and makes regulatory mistakes. But if you naively stuff every message and state into the prompt, your LLM costs skyrocket. On a recent logistics deployment (7K+ daily requests), prompt history alone pushed token usage to 320K per client per day—completely unsustainable. Memory is not a nice-to-have; it's the bottleneck between demos and shipped systems.

Architecture: graymatter + mcp-memory-service in Action

No made-up products here—just real patterns and tools. My memory backbone combines Anthropic's graymatter (docs) with a custom mcp-memory-service (built on Postgres + Supabase). This service implements retrieval strategies and filtering outside the prompt loop.

How This Works in Production

  • graymatter summarizes key facts and actions from the agent's context
  • mcp-memory-service stores these as structured events in Postgres, with time and type metadata
  • Each prompt fetches only the relevant memory fragments using Retrieval Augmented Generation (RAG)

# memory.py — agent memory core
import supabase
from datetime import datetime

def store_memory_event(agent_id, event_type, content, timestamp=None):
    sb = supabase.create_client(url, key)
    event = {
        "agent_id": agent_id,
        "event_type": event_type,
        "content": content,
        "timestamp": timestamp or datetime.utcnow()
    }
    sb.table("memory_events").insert(event).execute()

def get_relevant_memory(agent_id, query, limit=10):
    sb = supabase.create_client(url, key)
    memories = sb.table("memory_events").select("*").eq("agent_id", agent_id).order("timestamp", desc=True).limit(limit).execute()
    # Add embedding-based filtering here if needed
    return memories

Token Savings: Measurable Results

By aggregating only key facts (memory compression) and using RAG to fetch context, you avoid prompt bloat. On a fintech deployment, this approach cut token usage by 90%—from 150K to 13K tokens per agent/day (Postgres logs, March 2024). Quality stayed high: agent action accuracy held at 98% (manual validation, same period). Source: my own production logs, validated by client-side audits.

ApproachAvg. Tokens/DayQuality (Accuracy)
Linear history150K98%
graymatter + mcp-memory-service13K98%

Memory Management: What Matters in Production

Volume Control

Unbounded memory means prompt inflation and performance decay. I implement TTL (time-to-live) on old events, keep only key transition points, and deduplicate via cron (n8n).

Audit and Security

Every memory change is logged (audit trail in Postgres). For fintech and industrial deployments, I add static code checks with semgrep and bandit on the memory service codebase. Regular audits are non-negotiable for regulated clients.


# Scan memory code for leaks
semgrep --config=python --exclude-dir=tests memory/
bandit -r memory/

RAG Filtering

Naive RAG isn't enough if you ignore data types. I store metadata on each event (type, importance, source) and use embedding similarity (e.g., OpenAI embeddings) to fetch only relevant memory fragments at prompt time.

FAQ

Why not just use vendor memory APIs?

Vendor memory APIs are tied to specific LLMs and lack flexibility for structuring, TTL, or auditability. A custom Postgres/Supabase service is fully controllable, integrates with n8n, Doppler, and lets you meet regulatory requirements.

How do you automatically prune old memory?

I schedule daily cron jobs in n8n to delete events older than a set threshold (e.g., 30 days) or by type/importance. This keeps storage and retrieval fast.

Can memory compression be added to legacy agents?

Yes. Move memory logic to a service and integrate via API. Use RAG plus embedding-based filtering to compress on retrieval, not at the prompt.

What stack works best for production memory?

Postgres + Supabase is stable at scale (millions of events). I don't recommend MongoDB—weak transactional guarantees and poor auditability.

How do you check memory security?

Automated static checks (semgrep, bandit) plus manual audits of DB changes. For fintech, integrate with SOC tools and log all access events.

At which stage does your agent's memory fail most often—RAG, audit, or scaling? I'd genuinely like to know. I run a free 30-min stack audit for DACH founders building AI in regulated markets. DM me on LinkedIn or write to @ger_dennis_ai.

Read more

Computer-Use-Agenten scheitern an Pixeln: wie Sie auf stabile UI-Referenzen über Accessibility Trees umstellen und fehleranfällige Desktop-Automatisierung nicht mehr ständig reparieren

von Denis Shokhirev, Enterprise AI Architect aus Freiburg im Breisgau Ich bin Denis Shokhirev, Agentic AI Systems Architect in Freiburg im Breisgau, Germany, und bei DennisCraft AI Studio setze ich produktive KI-Systeme für DACH-B2B-Kunden auf Basis von Claude, Supabase, n8n, Doppler und selbst betriebenem Postgres um. Die teuersten Fehler in

By Denis Shokhirev

Компьютерные агенты ломаются на пикселях: как перейти на stable UI refs через accessibility tree и перестать чинить flaky desktop automation

Я Denis Shokhirev, Agentic AI Systems Architect из Freiburg im Breisgau, Germany, и в DennisCraft AI Studio я собираю production AI-системы для DACH B2B на Claude, Supabase, n8n, Doppler и self-hosted Postgres. Самая дорогая поломка в desktop automation, которую я вижу у клиентов, почти никогда не связана с моделью: агент

By Denis Shokhirev