The Five Building Blocks of an Always-On Agent

    Always-on agents fail in production when they are treated as a framework choice instead of a set of durable parts. This article describes five components—inbox, timers, memory, permissions, and outbox—plus run/session semantics, failure modes, and reliability practices so you can evaluate implementations by recoverability, not API polish.

    The question is not which framework, but which parts you assemble

    In AI engineering, a recurring question is which agent framework is “best.” It is hard to answer because “best” is rarely measurable. What actually moves an agent from a demo into production is not a pretty API. It is always-on behavior: the agent can wake itself, remember, and ask a human for permission.

    The skeleton is simple. It has five parts: an event inbox, timers, memory, permissions, and an outbox. This article does not compare products. It defines each part’s responsibility, data shape, and failure modes. Once those are clear, framework choice becomes an engineering question: which option lets you implement the five parts with less code, and with recoverable failures.

    Two units: run and session

    A run is one unit of work. An event arrives, the work is completed, what happened is written to storage, and the run ends. A run has a clear start and end.

    A session is the full record of a context: messages, tool calls and returns, and intermediate conclusions. Runs execute inside a session. When a run ends, the agent sleeps.

    When the next event arrives, the scheduler has two choices: continue the existing session, or start a new one. “Always on” is not a process in a tight loop. It is this short cycle, repeated:

    1. event arrives → write inbox → worker claims it → load session
    2. → execute run (reasoning + tools)
    3. → persist session / write outbox / set timers
    4. → release lease → sleep

    The engineering payoff is that memory and “being alive” are decoupled. A process can die; state lives in storage. A new process can start and pick the same state up. Availability depends on storage and scheduling, which you can back with ordinary infrastructure: relational databases, message queues, containers, and object storage.

    Part 1: the event inbox

    A typical agent has one trigger: a user message. An always-on agent has many: webhooks, cron-like jobs, background-job completion, callbacks from other agents.

    Calling the agent directly from each source fails in two ways. Concurrent events can start two runs that race on the same session. A crash can drop the event with no compensation trail.

    So every source writes only to an inbox, and workers only read from the inbox. In practice that is often a durable table, an HTTP write endpoint, and a worker that polls or subscribes.

    1. CREATE TABLE inbox (
    2. id BIGSERIAL PRIMARY KEY,
    3. source TEXT, -- event source category
    4. dedup_key TEXT, -- idempotency key, unique index
    5. payload JSONB,
    6. session_id TEXT, -- owning session
    7. status TEXT, -- pending / running / done / failed
    8. attempts INT DEFAULT 0,
    9. created_at TIMESTAMPTZ,
    10. lease_until TIMESTAMPTZ
    11. );

    Four practices, each matching a real failure:

    1. The write path must return quickly. Many producers treat slow responses as failure and retry, which multiplies load. Slow checks—safety, prompt-injection defense, attachment parsing, permission decisions—belong inside the run.
    2. The inbox is the source of truth. After wake-up, the worker loads the latest events from the table, not from an in-memory queue. If a worker dies mid-run, a new worker can retry.
    3. One active worker per inbox item at a time. Use a lease, not a permanent lock: status=running and lease_until > now() means claimed; an expired lease is reclaimable. This prevents duplicate work and session overwrite.
    4. Workers should merge pending events. Three user messages in a row should become one run and one reply. A typical merge: take the earliest pending event for the session as the primary event, attach the rest as context, and mark them merged in the same transaction.

    You also need a dead-letter path and an attempts cap. Events over the threshold go to a failed queue with an alert so poison messages cannot burn compute forever.

    Part 2: timers

    The inbox covers “the world needs you.” Timers cover “you need to remember to act.”

    There are two kinds. Follow-up is a deadline the agent sets for itself (for example, check progress in three days). It is one-shot and ends when marked done. Schedule is a recurring cadence (for example, a daily report at 08:00). After each fire, the next fire_at must be advanced.

    1. CREATE TABLE timer (
    2. id BIGSERIAL PRIMARY KEY,
    3. kind TEXT, -- follow-up / schedule
    4. session_id TEXT,
    5. fire_at TIMESTAMPTZ, -- next due time
    6. period TEXT, -- schedule expression
    7. payload JSONB,
    8. state TEXT -- armed / fired / done
    9. );

    Constraint: timers do not start runs. They live in their own table. A scanner converts due rows into inbox events. After that, the path is identical to external events. You get a single execution path: permissions, idempotency, merge, retry, and audit are implemented once.

    For schedules, define catch-up explicitly. If the scanner was down for two hours, do you replay missed fires or jump to the next period? Frameworks should not decide this. Catch-up often duplicates notifications; skipping with a log is usually safer.

    Part 3: memory

    Memory is where always-on agents diverge most from chat-style agents. Organize it in four layers:

    • Working memory: the current run’s context window; discarded when the run ends.
    • Episodic memory: the session’s full event stream; replayable for debugging and audit.
    • Semantic memory: stable facts extracted from episodes (preferences, constraints, project background); written explicitly and merged with deduplication.
    • Procedural memory: reusable playbooks and tool-use notes—instructions the agent writes for itself.

    Three engineering rules:

    Writes must be explicit. Dumping every turn into semantic memory turns it into noise. A dedicated “record” tool, with the model deciding what is worth keeping and with a structured write schema, is more reliable.

    Retrieval must be scoped. Semantic search needs session, tenant, and project filters. Cross-context bleed is the usual form of memory pollution.

    Context must be compressed. Long sessions overflow the window. Combine a rolling summary with selected original spans: the summary keeps continuity, the originals keep precise quotes. Persist the summary so you do not rebuild it every time.

    Part 4: permissions

    An always-on agent acts on its own, so permissions are not optional. The principles are least privilege, human-in-the-loop, and full audit.

    Grade tools by risk: read-only queries, internal writes, external side effects (messages, orders, deletes), and irreversible actions. The first two can auto-run; the last two need approval. Approval is itself an event: the agent writes pending_approval to the inbox, ends the run, and sleeps. Human approval produces a new event that wakes it. That fits the event model; you do not need a special wait primitive.

    Three extras: keep credentials out of the session and the prompt; fetch them from a secrets manager inside the run. Record each tool call: who, as which identity, on which resource, what happened, and the result. Cap rate and quota on high-risk actions so a runaway loop cannot do real damage.

    Part 5: the outbox

    A run produces outbound work: replies, notifications, writes to other systems. Calling those APIs synchronously inside the run has two costs: external timeouts stall the run, and external failures leave inconsistent state.

    Mirror the inbox with an outbox. The run writes intended sends to an outbox table and returns. A separate dispatcher sends asynchronously, with backoff, idempotency keys, and dead letters. Inbox and outbox become two observable, replayable queues at the boundary with the outside world.

    Reliability and observability checklist

    • Idempotency: every event has a dedup_key with a unique inbox index; outbound calls carry idempotency keys.
    • Crash recovery: all state is in storage; workers are stateless; expired leases are reclaimed.
    • Backpressure: worker concurrency is configurable; past a queue-depth threshold, degrade or reject new events instead of growing without bound.
    • Observability: cover run duration and failure rate, inbox/outbox depth, timer drift, tool error rate, and tokens/cost per run. Send logs and metrics to your logging and alerting stack, correlated by session_id.
    • Replay: persist episodic memory so a failed run can be replayed offline.

    Common anti-patterns

    Wiring triggers straight into the agent; putting events on an in-memory queue; using permanent locks instead of leases; letting timers call the agent and skip the inbox; writing everything into long-term memory; blocking a run on human approval; calling external APIs without idempotency. These can work in a demo. Unattended, they show up as duplicate execution, dropped events, or corrupted state.

    Closing

    There is no universally best agent framework—only whether these five parts are implemented. The bar is: processes may die, work must not be lost, state must not scramble, and actions must be traceable. Implement inbox, timers, memory, permissions, and outbox with the boundaries above, then use commodity cloud pieces (relational databases, queues, object storage, containers, logging, alerting, secrets) for reliability and operations. You then have an agent that can wake itself over the long term. Framework shopping reduces to who requires less glue.