I'm building Agent Brain Hub, an open-source memory that several AI agents share. It's modeled on the human brain, and like a brain it needs sleep: that's when conversations get consolidated into long-term memory, expired facts get forgotten, and the brain reflects on what it learned.
Until this week, sleep only happened when someone clicked Run sleep cycle.
The bug that pushed me
While adding automatic sleep, I wrote a test for a long conversation and found a real problem. Working memory keeps only the last 40 turns. After 30 back-and-forth messages with nobody pressing the button, the first 10 questions were simply gone. They were never consolidated, so no agent could ever recall them.
Three ways the brain falls asleep now
| Trigger | When | Default |
|---|---|---|
| Idle | The customer has gone quiet, so the session is over | 30 min |
| Pressure | Too many turns are waiting to be consolidated | 24 turns |
| Nightly | Once a day, for customers active since last night | 03:00 |
The core check is small:
const trigger =
turns >= cfg.maxPendingTurns ? 'pressure'
: idleMinutes >= cfg.idleMinutes ? 'idle'
: null;
The 30-minute idle default isn't arbitrary: it's the same gap the brain already uses to decide that a new session has started. The pressure threshold stays below the 40-turn cap, so nothing is dropped.
A few details I cared about:
- Manual and automatic sleeps share one queue, so two runs never touch memory at the same time.
- No surprise bills. On the first start, the server doesn't sleep every customer at once. With a paid LLM, that would mean a burst of summarization calls.
- You can watch it happen. Automatic runs show up live in the brain view, with a note saying why the brain fell asleep.
-
Docker runs in UTC, so set
TZ=Asia/Ho_Chi_Minh(or your zone), or "03:00" will be someone else's night.
Settings live in the UI (Settings → Sleep cycle) or in BRAIN_SLEEP_* environment variables. There are 9 new tests, including one that reproduces the lost-turns bug.
Try it with docker compose up. Feedback is very welcome, especially if you've solved memory consolidation differently in your own agents.
👉 https://github.com/leluong141996-dev/Agent-Brain-Hub

Top comments (0)