Updated 2026-11-19 · DeepSeek V4 Hub Editorial Team
How to Build an Always-On AI Agent That Survives Restarts
Suppose a maintainer asks an agent to watch for a dependency release. Several days later, the agent reports the new stable version, shows what changed, and asks whether to forward a note to the team. The model may have run for only a few short stretches. The service stayed available because it knew when to wake the model and what work to resume.
What does an always-on agent actually run?
A run is one stretch of work. It begins with an event and ends after the worker records the result or saves enough state to resume. A session is the conversation and tool-call history. One session can span several runs, including the run that asks for approval and a later run that handles the answer.
This pattern applies whether a worker calls a Claude model or a compatible DeepSeek endpoint. Check the chosen model and agent framework for tool calling and session handling. The application still owns the queue, state, permissions, and delivery.
| Stage | What happens |
|---|---|
| Receive | A release event reaches the inbox. |
| Check | The agent reads the public release notes and saves a draft. |
| Ask | The user receives an approval request. The worker is free. |
| Resume | The reply reaches the inbox and resumes the saved session. |
How does the inbox handle repeat events?
Record each incoming event before acknowledging it. Give the record a source, a source event ID, an inbox owner, and a processing status. A worker claims pending records and starts the appropriate session. Anthropic suggests starting with one active worker per inbox and splitting inboxes when the application needs more parallelism. When several related messages arrive together, the worker can read them in a single run.
Suppose a release webhook times out after your endpoint has already saved it. The sender can deliver it again. A uniqueness constraint on the combination of inbox, source, and source event ID lets the handler acknowledge the repeat while keeping one record. If the worker crashes after claiming the event, an expiring claim lets another worker take over. Renew the lease during long runs. Before committing results, atomically verify that the original claim is still current and unexpired.
Inbox deduplication covers repeated input. Give each outgoing approval request or notification its own delivery key. Keep those IDs distinct so you can tell which release arrived and which message was sent.
If you're building a watcher with DeepSeek, compare available plans and usage limits on the DeepSeek Coding Plan page.
What wakes the agent up on a schedule?
A scheduler writes due events to the same inbox as user messages and webhooks. A one-off follow-up might say, "Check whether the release notes are available in two hours." A recurring schedule might ask for a Friday digest. Keep the next due time in durable storage. For a recurring schedule, retain the time zone and recurrence rule as well.
Use a stable identity for each scheduled occurrence. If the scheduler restarts after writing Friday's event but before advancing the timer, the duplicate occurrence can be recognized on the next pass. When the timer and inbox share a database, a transaction can commit the event and advance the timer together. Decide ahead of time whether a digest missed during downtime should be sent late, folded into the next digest, or skipped.
What should survive a restart?
The worker needs a small record of the task, the user's instructions, what it has already reported, and any pending decision. The release monitor might keep the watched repository, the latest release reported to this user, and a link to the draft awaiting review. It can fetch the release notes again from their original location. Anthropic recommends keeping references to information already stored elsewhere and loading detailed context only when a run needs it.
Load the watcher settings and the current release record at startup. Fetch older summaries only when comparing releases. Keep user settings in an application record that the worker can read and update through controlled operations. A model-written summary can help with continuity, while recorded settings and action IDs give the next run something precise to work from. If the user's preferences change, update that saved record so later runs consult the current settings.
How can the agent wait for approval?
Give each tool a scope the application can enforce. The release monitor can read public release notes automatically. Forwarding the draft to a team channel can require approval for that destination and message. Keep service credentials in a connector or proxy outside the model's runtime, and check the operation at the tool boundary. Anthropic describes automated review as a way to screen routine calls. Use a user decision for actions your product treats as consequential.
When the agent asks for approval, save the proposed action and end the run. Record the person authorized to decide, the exact draft and destination, and an expiry. The reply returns as a new inbox event. Before sending, verify that the decision still applies to the saved draft and that the user still has permission to approve. If the draft has changed, ask for a fresh decision.
When should the agent send a message?
Send on a change the user has asked to hear about. For the release monitor, that could mean a new stable release with a link and a short summary. A check that finds the same release can finish quietly. If a source has been unavailable for several checks, the product can show a separate degraded status so the user can distinguish quiet operation from a stalled monitor.
A direct send tool can be enough for a simple workflow. A durable outbox helps when delivery needs retries or the product needs to control notification frequency. Store the message alongside the state change when both use the same database, and let a dispatcher handle delivery. If the channel accepts idempotency keys, reuse the same key on retry. If a send times out and the destination provides neither deduplication nor a way to look up the prior send, a blind retry can still send a duplicate.
What should you test before going live?
Use one release watcher and a test destination. In a staging run, deliver the same event twice, restart the worker after it claims a record, and replay an approval after it expires. Then watch whether the system recovers and what the user receives.
Track queue age, duplicate notifications, and the time between a release event and a useful message. Add model usage per completed check if cost matters. These measures tell you whether the monitor is dependable when the worker restarts, the scheduler misses a tick, or a user answers hours later.
| Test condition | Expected result |
|---|---|
| Webhook arrives twice | The inbox retains one event and the user receives one approval request. |
| Worker stops before recording its result | The claim expires and another worker recovers the pending event. |
| Approval expires or the draft changes | The send remains pending until the user approves the current draft. |
| Destination times out after a send | The dispatcher resolves delivery using the original message key or flags the outcome for review. |
FAQ
Does an always-on agent use model tokens while idle?
In an event-driven design, model calls happen during runs. The scheduler, database, and event receiver can still consume infrastructure resources between runs.
Do I need a separate workflow engine?
A database, a scheduler, and a worker can support a simple watcher. A workflow engine becomes worth evaluating when you have many delayed steps, retries, or approvals to recover.
Is one active worker per inbox enough to prevent duplicate notifications?
Use a separate delivery key as well. An event can be processed again after a crash, and the first run may already have sent its message.