A sleep study in agents

Do Agents Dream
of Electric Sleep?

A nighttime maintenance cycle for agents that wake up smarter, cleaner, and ready to work.

1
Chapter One

Your agents never sleep

Every day, Claude Code accumulates a little more entropy. Stale memory entries. Unused hooks. Bloated CLAUDE.md files. Repeated frustrations that never get addressed.

Today, this configuration rot is your problem. You're the one who notices the agent keeps making the same mistake. You're the one who cleans up.

"Token anxiety" — the nagging feeling that your agents are doing nothing while you're sleeping.

But in biology, every organism has a solution to entropy. It's called sleep.

2
Chapter Two

Sleep isn't rest. It's healing.

During sleep, your brain runs the most sophisticated maintenance cycle in nature:

  • Memory consolidation — hippocampal replay strengthens important patterns
  • Synaptic pruning — unused connections are weakened and removed
  • Waste clearance — the glymphatic system activates 10x to clear metabolic debris
  • Creative dreaming — REM sleep recombines ideas into novel solutions

Sleep is the price the brain pays for plasticity. Without it, the system degrades. With it, the system compounds.

3
Chapter Three

Four stages of agent sleep

N1 · Light Sleep
Measure
Scan Braintrust metrics. Detect frustration clusters, token anomalies, repeated commands.
N2 · Medium Sleep
Prune
Trail evaporation. Decay unused artifacts. Remove dead weight. Synaptic downscaling.
N3 · Deep Sleep
Repair
Health-check hooks, skills, memory. Fix broken references. Glymphatic waste clearance.
REM · Dream
Create
Two-signal activation. Synthesize new hooks, skills, memory from confirmed patterns.
4
Chapter Four

The immune system's wisdom

Your immune system never attacks on a single signal. It requires co-stimulation — two independent signals confirming the threat before deploying a response.

Our sleep agent follows the same principle:

  • First sighting — a pattern appears in one sleep cycle. Noted. Deferred.
  • Second sighting — the same pattern appears again. Confirmed. Act on it.

This prevents hallucinated improvements from single-session noise.

Two signals = 0.65 confidence. Three+ signals = 0.85. Cross-session + cross-pattern = 0.95.

5
Chapter Five

Trails that fade

Ant colonies coordinate through pheromone trails. Popular paths get reinforced. Abandoned paths evaporate. No central coordinator needed.

Every artifact in our system has a relevance score that decays each cycle:

relevance = 0.9 × previous + reinforcement

Used artifacts get reinforced (+1.0). Unused artifacts fade. Below 0.2? Pruned.

This mirrors Tononi's synaptic homeostasis hypothesis: sleep exists to downscale synaptic strength, keeping only what matters.

6
Chapter Six

How it works

Daytime — Braintrust CC plugin captures every session automatically. Online scorers analyze each trace in real-time.

Nighttime — The sleep cycle runs:

N1 Haiku
N2 Sonnet
N3 Sonnet
REM Opus

Each stage gets escalating capability. Haiku does cheap measurement. Sonnet does focused repair. Opus does creative synthesis.

Structured JSON output at every stage. Least-privilege tool access. Full traceability in Braintrust.

7
Chapter Seven

The proof

Same task. Before sleep and after. Measured in Braintrust.

150k
tokens before
60k
tokens after
60%
reduction

The sleep agent created targeted memory entries, pruned stale context, and added rules that eliminated redundant tool calls. The agent woke up knowing things it had to rediscover before.

8
Chapter Eight

What we built

Built in one hackathon day, using Claude Code itself.

  • sleep.sh — orchestrator chaining claude -p with JSON schemas
  • 4 stage prompts — escalating model tiers and tool permissions
  • query_braintrust.py — BTQL bridge between plugin and sleep agent
  • upload_to_braintrust.py — 7 sessions, 23MB, 1,024 spans uploaded
  • 3 online scorers — FrustrationDetector, TokenEfficiency, ToolUsageSummary
  • 15,000+ lines of research across 300+ sources
9
Chapter Nine

Grounded in science

300+ sources across neuroscience, immunology, ecology, and distributed systems:

  • Tononi's synaptic homeostasis → trail evaporation
  • Glymphatic waste clearance → orphaned file cleanup
  • Hippocampal replay → cross-session aggregation
  • REM dreaming → creative artifact synthesis
  • Immune co-stimulation → two-signal activation
  • Ant colony stigmergy → environmental traces as coordination
  • Alan Kay's cathedral → simple components, powerful arrangement

The bridge isn't in any of the stones. It's in the leaning.

10
Chapter Ten

What's next

  • Scheduled execution — launchd triggers when idle for 30+ minutes. True healing while you sleep.
  • Multi-project learning — same correction across 3 repos? Strong signal.
  • Continuous cycles — repeat N1→REM like real sleep (4-5 cycles per night) with diminishing returns detection.
  • Community patterns — 1,000 developers frustrated by the same thing? Anthropic should know.
  • Voice briefing — "Good morning. While you slept, I pruned 3 stale memories and created a new skill. Token usage should drop 40%."
11

Goodnight

Your agents are healing. Your agents are growing.

12
swipe to turn pages