Anthropic published a guide to building always-on agents on 13 November. Its core idea is that an always-on agent spends most of its time asleep and wakes for short runs: each run receives an event, works inside a session, and saves state before sleeping again. The guide names five primitives for this cycle: an event inbox, timers, memory, permissions and an outbox.
This post builds the three that are plain plumbing (inbox, timers, outbox) in about 150 lines of Python and SQLite, plus a single-worker lease. The model call is a stub you replace. The point is to see where each piece breaks and to have tests that prove it recovers. Everything below runs with the standard library only; the full file, assembled in order, is the sum of the blocks shown.
The shape: the inbox is the source of truth
The guide's inbox has three parts: a durable list of events (usually a database table), a handler that writes events (usually an HTTP endpoint), and a background worker that reads them. Two rules matter most. Events are deduplicated using the unique ID from the source, and the inbox, not the worker, is the source of truth, so a replacement worker can re-read pending events and retry a crashed run.

Here is the schema. The three tables map to the inbox, timers and outbox; the lease table is how we get one active worker.
import sqlite3
import time
db = sqlite3.connect(":memory:", isolation_level=None)
db.executescript("""
CREATE TABLE inbox (
id INTEGER PRIMARY KEY,
source_id TEXT UNIQUE NOT NULL,
session TEXT NOT NULL,
body TEXT NOT NULL,
status TEXT NOT NULL DEFAULT 'pending',
created REAL NOT NULL
);
CREATE TABLE timers (
id INTEGER PRIMARY KEY,
session TEXT NOT NULL,
due REAL NOT NULL,
every REAL,
payload TEXT NOT NULL,
done INTEGER NOT NULL DEFAULT 0
);
CREATE TABLE outbox (
id INTEGER PRIMARY KEY,
session TEXT NOT NULL,
body TEXT NOT NULL,
sent INTEGER NOT NULL DEFAULT 0
);
CREATE TABLE lease (name TEXT PRIMARY KEY, owner TEXT, until REAL);
""")The UNIQUE constraint on source_id is the deduplication. The webhook handler only stores and acknowledges; the guide says to acknowledge events quickly and defer deeper checks, such as prompt-injection screening, to the run.
def ingest(source_id, session, body):
"""Webhook handler: store and acknowledge. True if new, False if a duplicate."""
cur = db.execute(
"INSERT OR IGNORE INTO inbox(source_id, session, body, created) VALUES (?,?,?,?)",
(source_id, session, body, time.time()),
)
return cur.rowcount == 1Use the provider's own delivery or message ID as source_id. If a provider retries a slow webhook, the retry lands on the constraint and nothing is duplicated.
Timers live outside the inbox
The guide distinguishes two kinds of timer. Follow-ups are deadlines the agent sets for itself. Schedules are recurring tasks that also track their recurrence. It keeps timers in their own durable store, separate from the inbox, which holds only ready work. A scheduler polls at a fixed interval, finds due timers, marks follow-ups done, advances schedules to their next occurrence and adds an event to the inbox.

The detail worth copying is the deterministic source ID. If the scheduler crashes between inserting the event and updating the timer, the next tick inserts the same timer:<id>:<due> ID, which the inbox ignores.
def add_timer(session, due, payload, every=None):
db.execute(
"INSERT INTO timers(session, due, every, payload) VALUES (?,?,?,?)",
(session, due, every, payload),
)
def tick_timers(now=None):
"""Move due timers into the inbox. Safe to run twice: the source id is deterministic."""
now = time.time() if now is None else now
due_rows = db.execute(
"SELECT id, session, due, every, payload FROM timers WHERE done=0 AND due<=?",
(now,),
).fetchall()
for tid, session, due, every, payload in due_rows:
db.execute("BEGIN")
db.execute(
"INSERT OR IGNORE INTO inbox(source_id, session, body, created) VALUES (?,?,?,?)",
(f"timer:{tid}:{due}", session, payload, now),
)
if every: # schedule: jump to the next future occurrence
nxt = due + every * (int((now - due) // every) + 1)
db.execute("UPDATE timers SET due=? WHERE id=?", (nxt, tid))
else: # follow-up: fire once
db.execute("UPDATE timers SET done=1 WHERE id=?", (tid,))
db.execute("COMMIT")Note the nxt arithmetic: if the machine was off for three days, a daily schedule fires once and jumps to the next future slot instead of firing three times to catch up. Whether you want catch-up is a product decision; make it explicitly.
One active worker, one run at a time
The guide recommends one worker per inbox to prevent duplicated or overwritten work, and batching: several pending events, such as consecutive user messages, merge into one run. For mid-run events it lists three options: queue for the next run, fold into the current run, or interrupt and restart. This sketch takes the first and simplest: new events wait for the next run.

The lease is a row that a worker may take if it is free, expired or already its own. The upsert only updates when that condition holds, so rowcount tells you whether you won.
def acquire_lease(owner, ttl=60.0):
"""One active worker: take the lease if it is free, expired, or already ours."""
now = time.time()
cur = db.execute(
"""INSERT INTO lease(name, owner, until) VALUES ('worker', ?, ?)
ON CONFLICT(name) DO UPDATE SET owner=excluded.owner, until=excluded.until
WHERE lease.until < ? OR lease.owner = excluded.owner""",
(owner, now + ttl, now),
)
return cur.rowcount == 1
def release_lease(owner):
db.execute("UPDATE lease SET until=0 WHERE name='worker' AND owner=?", (owner,))
def run_once(handle, owner="worker-1"):
"""One run: batch the pending events of one session, call the agent, commit."""
if not acquire_lease(owner):
return False
try:
first = db.execute(
"SELECT session FROM inbox WHERE status='pending' ORDER BY id LIMIT 1"
).fetchone()
if not first:
return False
session = first[0]
events = db.execute(
"SELECT id, body FROM inbox WHERE status='pending' AND session=? ORDER BY id",
(session,),
).fetchall()
reply = handle(session, [body for _, body in events]) # may raise: nothing is lost
db.execute("BEGIN")
db.executemany("UPDATE inbox SET status='done' WHERE id=?", [(i,) for i, _ in events])
if reply:
db.execute("INSERT INTO outbox(session, body) VALUES (?,?)", (session, reply))
db.execute("COMMIT")
return True
finally:
release_lease(owner)The key ordering: events are marked done and the reply is written to the outbox in one transaction, after the agent returns. If handle raises, nothing changed, the lease is released, and the same events are still pending for the next attempt. The TTL covers the harder case where the whole process dies and never reaches finally: after 60 seconds another worker can take over. Choose a TTL longer than your longest run, or renew the lease during the run.


