Python / SQL
Electric Sheep: long-term memory for AI agents
Agents forget everything when a session ends. Electric Sheep reviews past sessions offline, finds what keeps proving true, and saves it as a lasting memory the next session can use.
What it does
Electric Sheep in plain terms
Electric Sheep gives coding agents memory that lasts across sessions. Between sessions it runs a 'dream cycle': it groups similar events from past sessions and promotes a pattern to a lasting belief only after it has held up across several of them. Beliefs live in Cloud Spanner and agents read them through an MCP server.
How it works
Three things to know
- Three levels of memory: the current session, the workspace, and global beliefs that apply everywhere.
- A pattern has to show up in several sessions before it becomes a belief.
- Agents reach it through MCP tools, so any MCP-capable agent can use it.
Learn more
Essays and source
Read the essay: WIBTM: Give an agent access to memories created with AI dreams →
Related: 'Dark data' is your new most valuable data for AI →
GitHub: Electric Sheep ↗
View a code sample (dream_consolidation.py)
from dataclasses import dataclass
@dataclass(frozen=True)
class EpisodicObservation:
session_id: str
rule_candidate: str
success_rate: float
@dataclass(frozen=True)
class CrystallizedBelief:
belief_id: str
statement: str
confidence: float
supporting_sessions: int
scope: str = "global"
def consolidate_dream_cycle(
observations: list[EpisodicObservation],
min_sessions: int = 3,
min_confidence: float = 0.90,
) -> list[CrystallizedBelief]:
"""Promote recurring episodic observations into crystallized Spanner beliefs."""
grouped: dict[str, list[EpisodicObservation]] = {}
for obs in observations:
grouped.setdefault(obs.rule_candidate.strip(), []).append(obs)
promoted: list[CrystallizedBelief] = []
for idx, (statement, items) in enumerate(sorted(grouped.items()), start=1):
unique_sessions = {i.session_id for i in items}
if len(unique_sessions) < min_sessions:
continue
avg_score = sum(i.success_rate for i in items) / len(items)
confidence = round(min(0.99, avg_score * (1.0 + 0.02 * len(unique_sessions))), 2)
if confidence >= min_confidence:
promoted.append(
CrystallizedBelief(
belief_id=f"BELIEF-{idx:03d}",
statement=statement,
confidence=confidence,
supporting_sessions=len(unique_sessions),
)
)
return promoted
DREAM CONSOLIDATION COMPLETE · Scanned 18 session trajectories PROMOTED BELIEF #042: 'Always run uv run pytest -v and maintain <3s unit test execution' (confidence=0.97) SPANNER COMMIT: projects/davenport-boutique/instances/electric-sheep/databases/memory