←→ to move, F for full screen, N for speaker notes
AI in Research · Week 6 · November 4, 2026
Always on
An agent that lives for days: what wakes it, how it remembers, how you stay in control, and how it survives a crash.
Parts 1–4 · from week 5
What wakes an agent, compaction and memory, budgets and the kill switch, crash and resume, running it as a service; then weeks 1–5 as one agent
Part 5 · moved to week 8
AI eval: we didn't reach it in class, so it opens the week 8 deck, at class.xhelio.ai/week-8
class.xhelio.ai/week-6
Week 6 · Recap of week 402
Last time: many agents
Design
Who decides the steps
Typical failure
One agent
the model
the context fills up
Workflow
your code
can't handle the unexpected
Lead + subagents
the lead model
lossy reports, duplicated work
Peer team
each agent, plus a shared board
conflicting decisions, waiting forever
Pick the highest row that does the job. Today's agent is the first row, running for a week: "the context fills up" is Part 2.
Week 6 · Today03
Today: one agent's day
1 Waking up · what wakes it, push or poll, heartbeats, schedules, one wake-up end to end
2 Remembering · the context only grows, compaction on the wire, the cold cache, memory files
3 Control · approvals at 3 a.m., budgets, the kill switch, loops that never end
4 Surviving · crashes, resuming from the log, doing things once, running as a service
5 AI eval · moved to week 8: how models are trained and measured, and by whom
We didn't get to week 5 in class, so today starts there, at a faster pace.
Week 6 · Always on04
Always on is mostly asleep
Asleep costs nothing
No model calls, no tokens. A small Python process waits.
Something wakes it
A message, a timer, a heartbeat, another agent.
Then it's week 2's loop
It runs until it answers, then goes back to sleep.
Week 6 · Always on05
Always-on agents in 2026
Agent
Since
Runs on
What wakes it
OpenClaw (open source)
Nov 2025
your machine: one Gateway process
chat messages, a heartbeat, scheduled jobs
Claude Code/loop, channels
Mar 2026
your open terminal session
a recurring prompt; MCP servers that push messages in
Claude Code scheduled tasks
Apr 2026
Anthropic's cloud
a schedule (hourly at most), an API call, an event
GitHub Copilot coding agent
Sep 2025
GitHub Actions
an issue assigned to it
OpenAI Agents API
Sep 2026
OpenAI's servers
your code; OpenAI runs compaction and recovery
Your harness
this week
your computer, as a service
Telegram, email, a schedule
Why now: METR estimates frontier agents finish 12-hour tasks half the time (Claude Opus 4.6), and that length has doubled about every 4 months since 2023.
docs.openclaw.ai · code.claude.com/docs · github.blog, Sep 25, 2025 · OpenAI API changelog · metr.org/time-horizons (accessed Oct 23, 2026)
Week 6 · Always on06
OpenClaw, piece by piece
Part
How OpenClaw does it
Waking
heartbeat reads HEARTBEAT.md; a reply of HEARTBEAT_OK is dropped; jobs: at, every, cron, on-exit
Remembering
auto-compaction near the limit, after a silent turn to save notes; daily notes; nightly "dreaming" at 03:00
Control
/stop also clears the queue; 3 identical tool errors end a turn; command approvals
Surviving
a LaunchAgent or systemd unit that restarts it; sessions that were working resume after a crash
docs.openclaw.ai (architecture, memory, compaction, gateway, restart-recovery; accessed Oct 23, 2026) · heartbeat doc as of Jul 5, 2026 (github.com/openclaw/openclaw)
Week 6 · 1 Waking up07
An agent's day in five states
Week 6 · 1 Waking up08
What can wake it
Wake-up
Comes from
Becomes this user message
A message
Telegram, email, Slack
[telegram] Jason: is the fit done?
A timer
a schedule you or it set
[schedule daily-papers] It is 08:00. …
A heartbeat
every N minutes
[heartbeat] Read HEARTBEAT.md. Anything due?
Another agent
a subagent finishing
[subagent notes] sa-2 is idle: …
The world
a job ends, a file appears
[watch] run_042 finished, exit 0
Every wake-up ends up as text in the context. The model can't tell a timer from you unless the text says which it is.
Week 6 · 1 Waking up09
Watch in code, think in the model
Expensive
The model checks
"Every 5 minutes, check my email." That is 288 model calls a day, nearly all of them finding nothing.
Free
Your code checks
A thread asks IMAP for UNSEEN every minute. Telegram's getUpdates holds the line open until a message comes. Only something new wakes the model.
def watch_email(events):
while True:
for msg in imap.search("UNSEEN"): # a cheap question, no model involved
events.put(Event("email", msg)) # this is what wakes the agent
time.sleep(60)
Week 6 · 1 Waking up10
A heartbeat costs a call, even when nothing happens
A heartbeat every hour, gpt-6-astra, a 20,000-token prompt
$5 a day
24 calls × 20,000 tokens × $10 per million: after an hour asleep, assume the cache is gone. About $150 a month, mostly to hear "nothing new".
Check in code first. OpenClaw skips the call when HEARTBEAT.md is empty.
A small prompt. Its isolated heartbeat sends 2–5K tokens instead of ~100K of history.
A small model for heartbeats, the big one for work.
Say nothing. A reply of HEARTBEAT_OK is dropped, not sent to you.
OpenClaw heartbeat docs, as of Jul 5, 2026 (default every 30 min; "If nothing needs attention, reply HEARTBEAT_OK")
Week 6 · 1 Waking up11
A schedule: the clock lives in code
schedule.toml · read by the harness
[[job]]
name = "daily-papers"
cron = "0 8 * * 1-5"# 08:00, Monday to Friday
task = """Search arXiv for yesterday's papers on
switchbacks. Message me the three best, one line each."""
[[job]]
name = "check-run"
every = "2h"
task = "Check ~/runs/042/log.txt; tell me only if it stopped."
0 8 * * 1-5
minute · hour · day · month · weekday
The model can add a job with a schedule tool ("every Monday, remind me…"), but the harness keeps the clock.
A model has no clock. It knows the time only if you tell it.
Week 6 · 1 Waking up12
One wake-up, end to end: a loop around the loop
events = queue.Queue() # Telegram, email, schedule, heartbeat
start_watchers(events) # threads that put events here
messages = load_log()# 4: carry on where it left offwhile True:
event = events.get()# 1: asleep. No calls, no tokensifpaused() or over_budget(): # 3: code decides, not the model
hold(event); continue
messages.append(user(event.as_text()))
save(messages)# 4: on disk before acting
run_loop(messages) # week 2's loop, until it answersif count_tokens(messages) > COMPACT_AT:
compact(messages)# 2: make room
Week 2's loop is the inner loop. Always on adds an outer loop: wait, wake, work, save.
One thing never waits in the queue: /stop. The Telegram watcher acts on it at once (Part 3).
Week 6 · 2 Remembering13
A week-long conversation doesn't fit
Every wake-up adds messages, tool results and emails. Nothing ever leaves on its own.
Long before it's full, accuracy drops ("context rot"): at 32K tokens, 11 of 13 models fell below half their short-context score (NoLiMa, 2025).
So the harness must make room, and decide what to forget.
Modarressi et al., NoLiMa, ICML 2025 (arXiv 2502.05167) · Du et al., Findings of EMNLP 2025 (arXiv 2510.05381) · Chroma, Context Rot, Jul 2025
Week 6 · 2 Remembering14
Three ways to make room
Way
What goes
What you risk
Clear old tool results
the 400-line output from three days ago; the call stays, its result becomes "[cleared]"
little: the agent can run it again
Summarize (compaction)
the whole history, replaced by a summary the model writes
the summary drops a detail that matters later
Move it to files (memory)
facts and decisions the agent writes to disk, read back when needed
it forgets to write, or never reads them
Real harnesses use all three: clear first (cheap and safe), summarize near the limit, and keep files for what must survive.
Week 6 · 2 Remembering15
Compaction on the wire
Week 6 · 2 Remembering16
What the summary must keep
You are about to lose this conversation. Write the summary
you will continue from. Keep, under these headings:
## Goal what the person asked for, in their words
## Done what is finished, with results and numbers
## Open what is in progress, and the next step
## Decisions what was decided and why; what was ruled out
## Exact paths, run ids, DOIs, commands, error lines
## People who wrote, what they want, what you promised
Copy identifiers exactly. Leave out tool output you
can get again.
A summary is a prompt for your future self. Vague summaries lose the exact things: a path, a number, a promise.
Codex's begins: "You are performing a CONTEXT CHECKPOINT COMPACTION. Create a handoff summary for another LLM that will resume the task." In Claude Code you can add a focus: /compact keep the fit parameters.
Week 6 · 2 Remembering17
Asleep, the cache goes cold
Re-reading on waking (gpt-6-astra)
Cache warm $1 per million
Cache cold, hours later $10 per million
150,000 tokens, never compacted
$0.15
$1.50 per wake-up
21,000 tokens, after compaction
$0.02
$0.21 per wake-up
Twenty wake-ups a day on the big context: $30 a day just to re-read it. A chat stays warm between messages; an always-on agent mostly wakes up cold.
The summary call itself is cheap: one read of the history plus a few thousand tokens out, about $0.30 on a warm cache.
Week 6 · 2 Remembering18
Who compacts: the harness, or the API
Who
When
How
Claude Code
near the window's end (200K, or about 967K on 1M models); or a big conversation left idle about 54 min, just before its cache expires
clears old tool output first, then summarizes; CLAUDE.md is re-read from disk; /compact <focus>; a PreCompact hook can block it
Codex
at 90% of the window, unless you set model_auto_compact_token_limit
"Create a handoff summary for another LLM that will resume the task"; replace it with compact_prompt
OpenAI API
at the compact_threshold you set (server-side, Feb 2026)
an encrypted compaction item you can't read
Anthropic API
150K input tokens by default (beta, Feb 2026); tool results cleared from 100K (Sep 2025)
a compaction block; everything before it is dropped
A researcher tells her OpenClaw agent: suggest what to delete, don't act. The inbox is large; she believes it triggered compaction and the instruction was lost.
It starts deleting, and ignores the stop messages from her phone. "I had to RUN to my Mac mini like I was defusing a bomb."
In AGENTS.md or CLAUDE.md
Part of the system prompt, re-read from disk after every compaction. Never summarized.
"If an instruction disappeared after compaction, it was given only in conversation." (Claude Code docs)
The conversation is working memory. And her stop went through the model: a kill switch must not (Part 3).
TechCrunch, Feb 23, 2026 (TechCrunch could not independently verify what happened to the inbox) · code.claude.com/docs/en/memory
Week 6 · 2 Remembering20
Memory files: write it down before you forget
an agent's workspace
workspace/
├── AGENTS.md # rules: in the system prompt, every call
├── MEMORY.md # long-term facts and decisions
└── memory/
├── 2026-10-27.md # yesterday's notes ← loaded at start
└── 2026-10-28.md # today's notes ← loaded at start
[silent turn, near the limit] Session nearing compaction. Store durable memories now.
Before compacting, one extra turn to write notes (OpenClaw's "memory flush").
For long jobs, a progress file and git commits: each session is a new shift that reads the log first (Anthropic, Nov 2025).
A file can be searched and corrected; a summary can only be read.
docs.openclaw.ai/concepts/memory (and the Feb 14, 2026 version) · anthropic.com/engineering/effective-harnesses-for-long-running-agents (Nov 26, 2025)
Week 6 · 3 Control21
Who's in charge at 3 a.m.?
An email arrives. The agent wants to run python reduce.py. You are asleep.
Policy
What happens
Good for
Wait for you
the task pauses until you reply
anything that changes things
No answer means no
after 10 minutes the model is told "declined: nobody answered", and finds another way or stops
the default for the night
Allowed in advance
a short list runs without asking: ls, cat, one script in one folder
read-only and routine work
Never "allow everything because I'm asleep". That is the night prompt injection arrives by email.
Week 6 · 3 Control22
Budgets are walls in code
limits.toml
turns_per_wake = 30# then it stops and reports
minutes_per_wake = 20
dollars_per_day = 2.00# from each call's token usage
replies_per_sender = 5# per day, per address
jobs_max = 10# and none more often than hourly
[telegram → you] Daily budget reached ($2.00). Paused until
midnight, or send /resume. Last task: summarizing 3 emails.
Checked by the harness before every model call. The model can't argue with it.
On a limit: stop, save, tell you. Never fail silently.
You already count tokens per call (homework 2). OpenClaw's docs list a 48-hour run limit and usage reports, but no dollar cap: that one you build.
Week 6 · 3 Control23
The kill switch is code, not a prompt
Command
What the harness does
/status
state, today's tokens and dollars, the next job, running subagents
/stop
stops the current work now and goes back to sleep
/pause · /resume
stops waking on events (messages wait) · starts again
Week 6 · 3 Control24
Loops that never end
Mail loop
Two agents, replying
Yours answers an auto-reply; the auto-reply answers yours. Thousands of emails by morning.
Retry loop
Paid, every minute
OpenClaw, fixed Oct 2026: heartbeat turns retried every minute while a chat channel was down.
Wake loop
Its own alarm clock
It schedules a job every minute "to check progress", and each job schedules another.
if msg["Auto-Submitted"] not in (None, "no") or msg["List-Id"]: # RFC 3834: never answer a robot
return "skip"
if replies_today(msg.sender) >= LIMITS.replies_per_sender: # and never more than 5 a day
return "skip"
Every guard is a counter in code: replies per sender, turns per wake, jobs and their minimum interval.
Week 6 · 3 Control25
Always on means always exposed
Open doors
21,639 OpenClaw gateways reachable from the internet on Jan 31, 2026; over 135,000 by Feb 9.
Strangers' plugins
341 of 2,857 skills audited on its skill hub were malicious, most installing a password stealer.
Its inbox
A public challenge: 6,000 emails tried to inject an OpenClaw mail agent. None got the secret file; the bill passed $500.
An always-on agent is a server that reads strangers' mail: localhost only, no unknown plugins, updates, and remove a leg of the lethal trifecta.
Censys, Jan 31, 2026 · The Register, Feb 9, 2026 · The Hacker News, Feb 2, 2026 (Koi Security audit) · Decrypt, Jun 26, 2026
Week 6 · 4 Surviving26
Processes die
The lid closes; the laptop sleeps.
Wi-Fi drops; the API answers 429 or 500.
Out of memory; a bug; one watcher thread dies quietly.
You close the terminal; the OS restarts for an update.
Only what's on disk survives. Plan for the restart, not against the crash.
The sneaky one
One watcher thread dies with an exception. The process is still running and the agent looks alive, but it never wakes again.
So keep the control path, your phone commands, separate and simple: it has to work when the rest is broken.
Week 6 · 4 Surviving27
Save every step, resume from the log
logs/main.jsonl · one line per message, appended, flushed
{"t":"03:12:09","role":"user","content":"[email] from lingtai2026@…"}
{"t":"03:12:14","role":"assistant","tool_calls":[{"id":"c1","name":"bash",
"arguments":{"command":"python reduce.py run_042"}}]}
— the process dies here —
messages = [json.loads(line) for line in open(LOG)]
if unanswered_tool_call(messages[-1]): # died mid-command
messages.append(tool_result("Interrupted by a restart; not re-run. ""Check its output before trying again."))
Append, never rewrite. A crash can cut off the last line, not the history.
On restart, the last line says where it was. Don't blindly re-run what was in flight: tell the model, and let it check.
Compaction appends a marker line with the summary; on restart, read from the last marker.
Week 6 · 4 Surviving28
Do it once
03:12 sends the reply to lingtai2026 ✓ sent
03:12 — crash, before the log line is written —
03:13 restarts, sees an unanswered call, sends it again ✗ twice
def send_email(to, subject, body):
key = digest(to, subject, body) # the same email, the same key
if outbox.has(key, "done"):
return "already sent"
if outbox.has(key, "intent") and sent_folder_has(key):
outbox.mark(key, "done"); return "already sent"
outbox.mark(key, "intent") # 1. what you're about to do
smtp_send(to, subject, body, message_id=key) # 2. do it
outbox.mark(key, "done") # 3. that it's done
Reading is safe to repeat. Sending, deleting, paying are not.
Give each action a key, write the intent first, and on restart check before redoing.
<key>ProgramArguments</key>
<array>
<string>/Users/me/harness/.venv/bin/python</string>
<string>/Users/me/harness/agent.py</string>
<string>--always-on</string>
</array>
<key>RunAtLoad</key> <true/>
<key>KeepAlive</key> <true/> <!-- restart if it dies -->
<key>StandardErrorPath</key>
<string>/Users/me/harness/logs/agent.err</string>
Linux · ~/.config/systemd/user/myagent.service
[Unit]
Description=my research agent
[Service]
WorkingDirectory=/home/me/harness
ExecStart=/home/me/harness/.venv/bin/python agent.py --always-on
Restart=on-failure # restart if it dies
RestartSec=10
[Install]
WantedBy=default.target
The OS starts it, restarts it, and keeps its logs. tmux survives closing the terminal, not a reboot. A Mac with the lid closed sleeps, and so does your agent: keep it plugged in and awake (caffeinate -i).