# Temporal for AI agents: what the ADR didn’t tell me

> Self-hosted Temporal ran every agent request of a fintech AI layer. Signals that arrive late, patches for in-flight runs, a history with no personal data – and a green suite that proved nothing.

I chose self-hosted Temporal for the [AI layer of a fintech platform](/work/ai-layer/) in an ADR, after comparing it with a scheduler of my own, Redis with RQ, LangGraph checkpoints, DBOS, Hatchet and Temporal Cloud. I covered that choice, and when building your own is enough, in [multi-agent pipeline architecture](/blog/designing-a-multi-agent-pipeline/). This post is about what came after: how agent runs map onto Temporal, and what running it taught me that the comparison didn’t.

## The shape: few workflows, many activities

There are only three workflows. `RunWorkflow` is one per agent run, and its ID is the ID of the request, so starting it twice can’t create a second execution. `PeriodicJobWorkflow` runs scheduled jobs. `ReconcileWorkflow` finds work that should exist and doesn’t.

Everything else is an activity, and they fall into four groups: execution (plan the next steps from the graph, run a step, mark a step failed), pauses (wait for the user and resume after the answer, wait for an approval and resume after it), endings (complete, fail, cancel, expire) and the rest (restart, an extra iteration on an event, reconciliation, periodic and operator jobs).

The workflow decides order. It never talks to a model, a database or the network – that is what makes it replayable. All I/O lives in activities, and an agent step is one activity, so a worker that dies in the middle of a model call loses exactly one attempt.

## Human-in-the-loop is a timer and four signals

Agents here ask clarifying questions and wait for approvals, sometimes for days. In Temporal that is a durable timer plus signals, and there are four: the user’s answer, the decision on an approval, restart and cancel. A pause waits up to seven days.

The interesting part is what arrives late. An answer or a decision is checked against the policy version and the deadline. One that arrives after the deadline is stored but changes nothing – you don’t want a run that expired last week to wake up and act on a click. A late answer to a clarifying question closes the expired request and opens a new one that links to the old, so the user’s answer isn’t lost and the history isn’t rewritten.

The same idea covers repeated events. If a system event arrives for a run that is still in progress, it goes in as a signal and buys one extra iteration, instead of starting a second run that races the first.

## Three ways work starts without a user

System runs have no conversation behind them, and each source has its own trade-off:

- **A change to the user’s profile** goes through an outbox read from the change log. It costs up to about two minutes of delay and buys never losing a change to a crash between the write and the start.
- **A new version of a rule** starts runs only for users whose results that rule took part in. Recomputing everyone on every rule edit would be simpler and much more expensive.
- **A schedule** ticks every 15 minutes, and each user’s daily event fires at 00:05 in their own time zone.

## Versioning in-flight runs

A run that waits seven days will be alive across several deploys. Changing workflow code under it is the part of Temporal that bites people, because a replay of an old history through new code must make the same decisions in the same order.

Every change to workflow logic went behind a named `workflow.patched` – delegation, restart, approval each have one – and the pipeline version is fixed in the request when it starts. CI replays the new code against stored histories of old runs, and a separate suite runs on a real server: a worker killed in the middle of a model call, the process stopped between the database commit and the workflow start, a terminal activity executed twice, the server restarted.

I added the replay test after the first deploy, not before it. I would reverse that, and I would put the patch into the template for any change to a workflow, so nobody has to remember it.

## A history with nothing in it

Workflow history is a second database. It has its own retention, its own access path and its own backups, and anything in it is subject to the same deletion rules as your main store. So arguments and results of activities carry identifiers and compact results only – no question text, no documents, no profile – and a test reads the history and fails if personal data appears in it.

That made two decisions easy. There is no payload codec, because there is nothing to encrypt. And the namespace retention went from three days to fourteen – long enough to debug a run after the fact – without making account deletion harder, because deleting an account never has to reach into Temporal. The application’s PostgreSQL stays the source of truth; Temporal runs the work, it doesn’t own the data.

## Surprises

**Starting after a commit.** In FastAPI the database session commits after the handler returns, so a workflow started from the handler can run before its row exists. The start now goes into a background task after the response, a scheduled reconciliation finds requests without a workflow, and the deterministic ID makes the retry harmless.

**One owner for retries.** Retries inside LiteLLM are off. The harness retries, and each attempt is its own immutable record; otherwise the audit shows only the last attempt and the cost of the others disappears.

**Validating old runs against new schemas.** A step’s error message was validated against the current schema instead of the one pinned to the run. After a deploy that changed the schema, old runs broke on their own error messages. An audit caught it before users did. Anything a running workflow reads back has to be read with the version it was written with.

**A green suite that proved nothing.** Locally the Temporal server in Docker sometimes fell into a restart loop. Tests that need a real server are marked, and when the server isn’t reachable they are skipped rather than failed – so the run went green having tested none of the durability it was there to test. A skip under that marker deserves the same attention as a failure. In CI those tests run in a shard of their own, against a server the job starts itself.

## Was it worth it

For this system, yes. Pauses of days, approvals, late answers and system runs from three sources are exactly the problems Temporal is built for, and I didn’t write a scheduler. But the engine took care of durability and nothing else. Idempotent side effects, what goes into history, how old runs read new data and whether the tests touched a real server were still my job, and each one was found by running it, not by the ADR.
