Long-running agents at Rox
Gopal Goel
Joshua Zheng


The sequencer before and after. Left: a screen that waits for the rep. Right: an agent that acts on each edit and reply as it arrives.
Most software today is a state machine behind a UI that a person clicks through. Increasingly, it is becoming a long-running agent that owns a task from start to finish. At Rox we are moving our product in that direction, and the sequencer is the first feature we have moved.
The natural worry is that something gets lost along the way, because SaaS products enforce rules. A three-step sequence sends each prospect at most three emails, and a prospect marked do-not-contact gets none.
We found that the agent can do all of the work without the product giving up any of those rules. The state machine is still there, but its role is different: instead of driving the work, it checks the work the agent does. Two parts of the design make that possible.

The same state machine in both designs. In the old design it drives the work. In the new one the agent drives, and the state machine checks every write the agent attempts.
The first is the tools. The agent can be wrong, whether by trying to send a fourth email or by writing against a campaign it hasn’t re-read. So it can only change the product through tools, and each tool checks the product’s rules before it writes anything.
The second is the runtime. A sequence’s agent lives as long as the sequence does. Its state is kept in MongoDB, and it runs on whichever machines are free, any of which can fail. The runtime makes sure every event is handled, no tool call runs twice, and only one machine acts for an agent at a time. Without those guarantees the checks in the tools wouldn’t mean much, since a single write could still land twice.
Sequences
The sequencer is one of Rox’s core products. A rep writes a campaign as a series of steps, such as a first email, a follow-up and a closing note, and adds prospects to it. Each prospect gets their own sequence, written for them and sent over several days.
Before AI, a sequencer was templates, contact enrichment and a scheduler. Our previous version replaced the templates with two handcrafted pipelines, one that researched the prospect and one that drafted the message, and ran them once per step. It worked well, but nothing carried over from one run to the next. Each step was drafted in isolation, and any feedback from the rep sent both pipelines back to the start.
The new sequencer gives each sequence its own long-running agent. Campaign edits and prospect replies reach it as events, and it keeps everything it has seen, so the research it did for the first email is still there when it writes the third. When a rep asks for a new draft, the agent can see the earlier drafts and what the rep said about them, and the emails are better for having that context.
It is also cheaper to run. The old pipelines repeated the research every time a step was regenerated and sent a fresh prompt for every step, while the agent researches once and keeps the results. The cost of each AI-written step fell from about $0.065 to about $0.0125, roughly a fifth of what it was. Part of that comes from moving to a newer model, and the rest from work the agent no longer has to repeat.
Tools as guardrails
The agent can change a sequence only through tools, and each tool checks the product’s rules before it writes anything. When a check fails, the tool writes nothing and tells the agent why.
Take the three-step sequence. The agent writes an email by calling commit_ai_step with a step and its content, and if it names a step the sequence doesn’t have, the commit is refused. That is all it takes to enforce the three-email limit, because there is no tool call that can write a fourth email.

A commit for a fourth step is refused, and the tool returns the message shown.
Campaign edits work the same way. When the read tool shows the agent a campaign, it records which build of the campaign it showed. If a rep edits the campaign while the agent is writing, the agent’s next commit is refused and it is told to read the campaign again. Because the agent never passes the build version itself, it has no way to claim it read something it didn’t.
Porting the task runner
The step limit and the build check are the obvious rules. Most of the sequencer’s logic, though, lived in the old task runner, which did everything in a fixed order: check the contact, repair missing tasks, work out which steps to regenerate, run the pipelines for each, check policy, write the tasks and finalize the sequence. Errors were caught in a list of except blocks, and if a worker crashed, the queue redelivered the task.
An agent chooses its own order, so none of this could stay a fixed sequence of statements. Each piece had to become either a check inside a tool or a property of the runtime.

The old runner’s run(), condensed, with each line linked to its replacement. Ordering and writing went to the agent, and everything else became a tool check or a runtime property.
Most of the migration was this translation. The hard part was the cases the runner handled that nobody had ever written down. Each of those became a message that a tool returns for the agent to act on. These are the actual strings:
Case | What the tool tells the agent |
|---|---|
A rep edits the campaign mid-write | “The sequence build changed since you read it. Call read_sequence_state again and work from what it says now.” |
The contact is still being enriched | “The contact is still being enrolled or enriched. That job will wake this sequence again when it finishes, so there is nothing to do now.” |
A LinkedIn connection note is too long | “…LinkedIn caps at 300 characters; this draft is 412. Shorten it and commit again.” |
The rep asks for a new draft and gets the same one | “Step 2 already carries exactly this content; the rep asked for it to be regenerated, so write a new draft.” |
A template step has no subject line | “…The sequence has been marked generation failed; there is nothing to do.” |
A few of these are an improvement on the runner. An email body containing HTML used to fail the whole sequence. Now the tool asks the agent to rewrite it without HTML, and it does.
To find the cases we had missed, we ran the old sequencer and the agent side by side in preview environments, gave both the same campaigns, edits and replies, and compared the sequences they produced. Most differences turned out to be a case the runner handled that the tools didn’t cover yet, and each one became a new check.
Running agents reliably
The tools make sure each write follows the product’s rules, but the runtime has to make the agent itself dependable. An agent lives as long as its sequence and runs on whatever machines are free, and any of those machines can fail partway through a turn. We built the runtime around three guarantees:
Every event is handled. An edit or reply that reaches a sequence is never dropped or left waiting.
No tool call runs twice. A write that may already have happened is never retried blindly.
Only one worker acts for an agent. A worker that has lost the agent can’t change anything.
The design starts from one decision, which is to treat an agent as data rather than as a process. Each agent is a mailbox of events, a transcript, a record of its tool calls, and a row holding its status and an epoch, all stored in MongoDB. A scheduler hands any agent with waiting events to a free worker, which runs one turn (either a single model inference or the tool calls that inference asked for) and then lets the agent go. Because nothing is kept on the worker, any worker can run any agent’s next turn.
Two properties of the workload decided where that state should live. First, it is write-heavy. A single turn involves a handful of small writes, from incoming events and transcript appends to tool-call records and lease renewals, and that is multiplied across every active agent. Second, it is transactional. Each step of an agent’s lifecycle touches several documents at once and has to happen completely or not at all, and MongoDB’s multi-document transactions let each step commit as a single unit.
This is also why the old sequencer’s recovery model no longer works. When one of its workers died, SQS redelivered the task and it started over from the beginning. An agent can’t start over, because its transcript is its state. That state survives in MongoDB even when the worker doesn’t, which lets the scheduler hand the agent to another worker, but the handoff puts two of the guarantees at risk. If a worker dies after a tool has written but before the result is recorded, the agent can’t tell whether the write happened. And if a worker stalls and later wakes up, it can write while another worker is running the same agent.
Agent state lives in MongoDB, and product data lives in Postgres. Pick a scenario and step through it. Brown is a write that lands, and red is a refused write or a worker that has lost the agent.
We considered making every tool idempotent, which would cover both cases, but only if every tool author got it right, and we can’t guarantee that. We also considered killing a worker before handing its agent to another, but that needs container-level control and still can’t undo a write that has already landed. Instead, the runtime enforces the guarantees itself, in two ways.
Every tool call is recorded before it runs. The worker writes the call to MongoDB first, under the epoch check, so the record survives even if the worker dies a moment later. If a worker finds a recorded call with no result, it doesn’t run the call again. It returns INTERRUPTED instead, and the agent reads the current state and decides what to do next. The agent may not have the full story at that point, but nothing it has been told is wrong.
Every write is fenced by an epoch. Each time a worker claims an agent, the agent’s epoch goes up by one. The runtime’s own steps in MongoDB check the epoch in the same transaction as the write they make, and so does every write a tool makes to the product’s data in Postgres. A worker that has lost the agent fails that check and exits.
We specified the agent’s lifecycle in TLA+ and model-checked it against these guarantees, which let us simplify it from five states to three. Each step in the lifecycle is a single MongoDB transaction: an event landing and the agent going active, events moving from the mailbox into the transcript, a tool call being recorded, and the agent going idle and rechecking its mailbox. A step runs only when its guards hold, such as “the epoch is still mine”. To make sure none of the guards were redundant, we removed each one in turn and re-ran the checker, and every removal broke one of the guarantees. Here is what broke for four of them:
Guard removed | What breaks |
|---|---|
The epoch goes up when an agent goes idle | an old worker becomes current again on the next event |
A tool call with an existing record is not run again | two workers execute the same call |
A tool’s write checks the epoch | a worker that lost the agent commits to the database |
An agent rechecks its mailbox before going idle | events are stranded on an idle agent |
Together with the tools, this is what makes it safe to trust an agent with the product. The tools stop it from writing anything the rules forbid, and the runtime makes sure that whatever it does write lands at most once, from the one worker entitled to write it. The agent can make mistakes and machines can fail without the product ending up in a wrong state.
What’s next
The sequencer was the first product we moved, and more will follow. We are building Rox so that long-running agents do the work end to end across the whole product.
Each product follows the same pattern. The state machine and its rules stay in the product, the agent does the work through tools that check those rules on every write, and the runtime keeps each agent reliable across machines, even when one of them crashes or stalls.
None of this asks users to give anything up. The rules they set still hold, and they keep control over whatever they want to control. What they gain is a product that keeps working when they aren’t looking at it. A sequence used to wait for the rep’s next click. Now it reacts to each reply as it arrives, remembers what it has learned about the prospect, and redrafts as soon as the rep asks, all within the rules the rep set. That is the kind of software we are building across Rox, one that does the work instead of waiting to be operated.
This post was written in partnership with MongoDB.
Similar Articles
We build with the best to make sure we exceed the highest standards and deliver real value.

