Durable execution for AI agents

Your agent will crash.Its work won’t.

Tenacity runs long AI-agent workflows so that a crash costs nothing. Every step is written to an append-only log before it counts. When a worker, the server or the database dies, the run picks up exactly where it stopped: no finished model call is paid for twice, and no card is charged twice.

research-agent · run r-0042 · “How does Raft handle leader failure?”
starting…

Workflow0.0 s

    Event log0 events in history

    history event (numbered, durable) crash recovery

    Processeskill one

    Worker w1
    running your Go code
    Server
    4 shards · leases · timers
    Database
    Postgres 17 · boot 1
    Same crashesTenacityPlain script
    Finished model calls run again00
    Finished steps redone00
    Times the card was charged00

    A plain script restarts from the top after a worker crash. Tenacity replays its log instead and retries only the step that was in flight.

    ✓ Run completed Watching a scripted run. Press Kill on any process to take over.
    How it works

    Four rules, enforced by the engine

    You write the agent as ordinary Go. The engine keeps four promises about it, and the crash tests check each one.

    EVENT LOG

    Nothing happens until it is written down

    Every decision your code makes (call this model, start this timer, finish) is appended to the run’s event log and committed before anything acts on it.

    #3 ActivityScheduled write model call #4 ActivityStarted write attempt 1 · lease b1·3 #5 ActivityCompleted write 1,284 tokens
    REPLAY

    A crash replays, it doesn’t restart

    A new worker runs your code again against the log. Steps already in the log return their recorded results instantly, so the run is back where it was in under a millisecond. Code that no longer matches its history is stopped with a diff, never silently corrupted.

    non-determinism in run b8bf98… at seq=1: activity name differs history : ActivityScheduled name="StepV1" replayed: ActivityScheduled name="StepV2"
    IDEMPOTENCY KEYS

    Side effects happen once

    Each activity gets a key derived from history that stays the same on every retry. Pass it to the payment API or the email service, and a retry after a crash returns the first receipt instead of charging again.

    charge attempt 1 key r-0042/5 applied rc_81f2 worker killed before the result was recorded charge attempt 2 key r-0042/5 seen → rc_81f2, not charged
    LEASES + FENCING

    A slow worker can’t overwrite a newer one

    Work is handed out on leases with tokens that only go up, even across database crashes. A worker that wakes up late with an old token is turned away, so two workers never both finish the same step.

    complete charge lease b1·5 → 409 stale lease (current b2·7) complete charge lease b2·7 → 200 recorded
    Developer experience

    It’s just Go, or TypeScript

    No YAML, no state machine to draw. Loops, branches and concurrency in plain functions, made crash-proof by the engine. Go and TypeScript workers replay each other’s histories, so both can serve the same queue.

    • Workflows decide what happens next. They are replayed, so they use the engine’s futures, timers and clock (workflow.Go, ctx.sleep, ctx.now()) instead of goroutines, setTimeout and the system clock.
    • Activities do the I/O: model calls, tools, payments. They run under leases with heartbeats and retries.
    • Waiting is free. A run can wait three days for a human. The wait is a durable timer and survives every restart.
    • Budgets survive crashes. Token and dollar limits reserve before each model call and settle after it, so a crash never lets an agent overspend.
    • Agents compose. Sub-agents run as child workflows with their own history; long-lived agents continue as new to keep it short; schedules start runs on a cron.
    workflow.go
    
            
    Architecture

    Small pieces, each one killable

    Workers hold no state that matters. The server holds the log in shards. The store is SQLite files or Postgres. Any of them can die mid-step.

    Workersyour Go or TypeScript code · poll for tasks · Go keeps a sticky cache of running workflows
    HTTP/JSON
    Tenacity servershards · event log · leases with fencing tokens · fair dispatch across tenants · durable timers and signals · child workflows and schedules · group commit · metrics and traces · web console
    one writer per shard
    SQLiteone file per shard, pure Go
    Postgresone schema per shard

    Cluster mode runs several servers over shared storage. Shard ownership is decided by a Raft implementation written for this project, and every write is fenced by an ownership epoch.

    Proof

    Tested by breaking it, thousands of times

    Every number below is recorded with its date, commit, hardware, command and raw output. The tag names the evidence entry.

    0
    duplicate or missing side effects out of 18,000, on Postgres, under crash tests
    E29
    29
    times Postgres itself was SIGKILLed mid-run, plus 396 server kills. Every run passed.
    E29
    34,308
    model calls in the 1,000-agent acceptance gate, each billed exactly once while workers were killed every second
    E23
    10,000
    simulated seeds per Raft profile with 0 safety violations; 9 of 9 planted bugs caught
    E12–E20

    Recovery of a model call when its worker is killed E24 → E27

    before M9
    ~60 s
    heartbeats
    8–9 s

    History sent per workflow task, 200-step run E28

    full replay
    298 events
    sticky cache
    3 events

    Workflow starts per second, 64 clients, full durability E25

    one fsync each
    389/s
    group commit
    2,299/s

    Measured on one Windows laptop (8 logical CPUs), medians of interleaved runs where noted. These are a single machine’s numbers, not a capacity plan.

    Built in eighteen milestones

    From a single node to a crash-tested engine

    1. M1Single-node engineEvent log, deterministic replay, leases, retries.
    2. M2Crash safetyCrash harness, idempotency keys, effect ledger.
    3. M3Sharding and timersShards, fair dispatch, durable timers and signals.
    4. M4Raft and cluster modeOwn Raft, simulator, ownership with fencing.
    5. M5Agent layerModel calls, budgets, approval, replay debugger, traces.
    6. M6Benchmarks and gate1,000-agent acceptance gate, real-model drills.
    7. M7Group commitOne fsync for many transactions: 5.9× more starts.
    8. M8Fewer fsyncsLeases commit without fsync, tokens never repeat.
    9. M9Streaming and heartbeatsKilled model calls recover in 8–9 s, not 60.
    10. M10Sticky caches3 events per task instead of up to 300.
    11. M11Postgres backendCrash-tested with the database killed 29 times.
    12. M12Ship itAPI keys, TLS, one-command dev, a 30 MB image.
    13. M13Web consoleRuns, timelines and approvals in the browser.
    14. M14Workflow versioningChange code while old runs are still open.
    15. M15Providers and toolsClaude, OpenAI, a durable tool loop, MCP servers.
    16. M16OperationsCancel, terminate, retention, metrics, live traces.
    17. M17CompositionChild workflows, continue-as-new, schedules.
    18. M18 · LATESTTypeScript SDKHistories replay identically under Go and TypeScript.
    Get started

    Two terminals, one minute

    Go 1.27 and nothing else. SQLite is built in and pure Go. Or docker compose up, Postgres included.

    1 · Start Tenacity

    A local server and a worker with the demo workflows, in one process.

    go build -o bin/tenacity ./cmd/tenacity ./bin/tenacity dev

    2 · Start a workflow

    It fails twice and is retried. Then read its history, event by event, or open the console at 127.0.0.1:7233.

    ./bin/tenacity start -type Flaky -id f1 \ -input '{"fail_times":2}' -wait ./bin/tenacity history f1

    Or a TypeScript worker

    Node 23.6 or newer; serves the same demo workflows.

    ./bin/tenacity dev -no-worker node sdk-ts/examples/worker.ts

    Or break it on purpose

    500 agent workflows, a worker killed every second, the server killed every five, then every side effect checked against an external ledger.

    go build -o bin/chaos ./cmd/chaos ./bin/chaos -workflows 500 -workers 4 -kill-every 1s -server-kill-every 5s -seed 1