The Truth About Always-On AI Agents: Learn to Sleep
Always-on AI agents do not run a model around the clock. This article explains how an event-driven sleep/wake scheduler, built from an inbox, an outbox, and follow-up timers, keeps long-running autonomous agents reliable and cost-efficient, and includes a minimal scheduling skeleton plus a reliability and cost-control checklist.
1. The Misunderstood Idea of “Always-On”
When developers hear “long-running autonomous agent,” many picture a cloud VM with a coding tool or automation script running in the background, watching the screen 24 hours a day like a night-shift security guard. It sounds diligent, but it is an illusion.
The industry is converging on a different view: the underlying model still runs the same short perceive–reason–act loop. What actually allows a system to survive tasks that last days or even weeks is the sleep/wake scheduling layer wrapped around the model. This closely mirrors how a phone works—the phone appears to be always on, but for most of the time the screen is off and processes are asleep. It wakes briefly only when a notification or alarm arrives, then immediately returns to a low-power state.
Always-on is a property of the external messaging system, not of the internal “brain.” Conflating the two is the most common cognitive mistake in long-running agent engineering, and the root cause of a great deal of wasted compute.
2. From “Single-Threaded Blocking” to “Event-Driven”
The most common early approach to scheduled tasks was a process running a while loop that called sleep for a number of seconds and then checked state once. That model is fine for short tasks. Once the task cycle stretches to hours or days, the problems surface all at once:
- Idle retries: an external dependency has already failed, yet the process keeps retrying on a fixed interval, filling the logs with useless entries;
- Lost state: after a restart, context and progress are not reliably persisted, so the task has to start over;
- No attribution: it is hard to tell whether a failure came from the model, a tool, or the scheduler.
This is the same evolution the server side went through when moving from single-threaded blocking waits to asynchronous event-driven design. For long-running tasks to stay stable, you do not rely on the model holding one long breath from Monday to Friday. Instead, you cut each “wake up and work” cycle short and lightweight, and keep state in reliable external storage.
A minimal scheduling skeleton looks like this (generic pseudocode only, not tied to any specific product API):
while True:event = inbox.fetch_one() # Atomically fetch one pending eventif event is None:sleep(idle_interval) # Nothing to do; go to sleepcontinuewith session(event.trace_id) as s:result = run_once(s, event) # Single short loop, strictly time-boundedoutbox.append(result) # Write the result to the outbox; do not push directlyif result.need_followup:scheduler.set_timer( # Set an alarm before going to sleepdelay=result.followup_delay,target=event.trace_id)
The key point is that fetching an event, writing a result, and setting a timer are all short transactions. Model inference is confined to a single run, so no single crash can corrupt global state.
3. Three Core Building Blocks
3.1 Inbox: A Single Entry Point for All Inputs
Whether the input is an external webhook, a user message, or a scheduled trigger, none of them may call the model directly. They must first be written to an inbox table and queued. This brings three benefits: it smooths out bursts so a traffic spike cannot overwhelm the inference layer; it makes events replayable, so failed events can be safely retried; and it makes every wake-up auditable with a clear origin.
The table usually needs only a few fields—event ID, source type, payload, status, attempt count, and trace ID—together with a state machine (pending / processing / completed / abandoned).
3.2 Outbox: Decoupling Output from Side Effects
Model replies are first written to an outbox, and a separate delivery service handles the actual push. This way the inference process does not have to wait on external channel timeouts, retry and rate-limiting policies are governed centrally on the delivery side, and content moderation and deduplication are easier to apply.
3.3 Follow-Up Timer: Giving the Agent a Sense of Time
This is the most important design decision for long-running tasks. Traditional scheduled jobs can only mechanically “send the daily report at 8 a.m. every day.” An agent with follow-up capability can, before the current run ends, schedule a future moment based on the semantics of the task: if the other party still has not replied in three days, it wakes up automatically to follow up; if the state has already changed, it cancels that timer. In essence, this turns “waiting” from blocking polling into a schedulable event.
4. Memory and “Dreaming”: Cleanup During Idle Time
Idle does not mean completely blank. During low-load windows, the system can tidy up memory: compress fragmented interaction records into structured summaries, expire stale information, and rebuild indexes. This kind of background work should be strictly rate-limited, interruptible, and re-entrant so it does not take resources away from the main path. The test is simple—if a piece of background work cannot be terminated at any time without affecting correctness, it does not belong in the idle window.
5. Reliability and Cost-Control Checklist
- Idempotency: deduplicate on event ID plus the business key so repeated delivery produces no duplicate side effects;
- Time limits: set a wall-clock limit and a step limit for each run; on timeout, suspend rather than retry indefinitely;
- Backoff: use exponential backoff with jitter for retries, plus a maximum attempt count and a dead-letter queue;
- Observability: use the trace ID to link the inbox, inference, and outbox stages, with metrics covering wake-up count, idle duration, and cost per task;
- Hot/cold tiering: keep state and memory in persistent storage and object storage respectively, and archive historical data to reduce cost;
- Least privilege: grant tool access on demand, and keep a manual confirmation switch for write operations and outbound messages.
6. Implementation Advice
To build similar capabilities in your own system, you can start by composing general-purpose cloud services: a message queue for event buffering, a container platform for stateless scheduling services, object storage for long-cycle memory, logging and monitoring/alerting services for observability, and a scheduled-task service for the follow-up timer. Baidu AI Cloud offers corresponding capabilities in these general categories, which can be combined as needed without building the entire infrastructure stack yourself.
The real conclusion is one sentence: keep the external system online 24 hours a day, and let the internal model sleep on schedule. Make each wake-up short, store state reliably, and turn waiting into a schedulable event—only then can long-running tasks be both cheap and reliable.
