Practical technical guides / Agent-era architecture: durable execution and long tool calls

A Long Agent Tool Call Needs a Heartbeat: Find a Lost Worker in Seconds and Resume From Recorded Progress

Give a long tool call a heartbeat timeout apart from its overall limit, with progress in each heartbeat. Simulated: a lost worker found in 90 s, not 26 min, with no rework.

By Sandeep Belgavi · Published 2026-11-06

Give a long tool call a heartbeat timeout apart from its overall limit, with progress in each heartbeat. Simulated: a lost worker found in 90 s, not 26 min, with no rework.

A tool call that takes eleven minutes

Most agent tool calls are short: look up an order, check a stock level, send an email. Some are not. When the help-desk agent at Belgavi's Book Store decides that a catalogue correction affects a whole publisher, it calls reindex_catalogue(publisher), which rewrites 1,200 search records in chunks of 100. Each chunk takes about 30 seconds, so the call runs for six minutes on a good day and longer when the search cluster is busy.

The workflow engine wraps this call as an activity with a generous timeout of 30 minutes, because nobody wanted it killed halfway through. Then a worker node was drained during a deploy, after chunk 7. Nothing noticed for 26 and a half minutes. When the timeout fired, the retry started at chunk 1 and rewrote the seven chunks that were already done.

Both problems have the same fix, and it is one of the few durable-execution features that most agent workflows built in the last year never switch on: heartbeats with recorded progress.

Two different timeouts

Temporal, which this guide uses for its terminology, separates the limits clearly. The Start-To-Close timeout is the maximum time one attempt of an activity may take. The Heartbeat Timeout is, in the documentation's words, "the maximum time between Activity Heartbeats". If an attempt stops heartbeating for longer than that, it fails and is retried under the retry policy, long before Start-To-Close would have expired.

The documentation adds two rules that matter for configuration. A Heartbeat Timeout of 0, which is also what an unset value becomes, means the heartbeat timer is disabled, not that it fires immediately. And the Heartbeat Timeout is capped at the Start-To-Close timeout: set 15 minutes against a 10-minute limit and you get 10 minutes, which is no faster than not heartbeating at all.

A heartbeat can also carry details. In the Python SDK the activity calls activity.heartbeat(*details), and on the next attempt activity.info().heartbeat_details returns the details from the last heartbeat the server received. That is how a retry knows where to resume.

The tool body, written to resume

from temporalio import activity

@activity.defn
async def reindex_catalogue(publisher: str) -> int:
    details = activity.info().heartbeat_details
    start = details[0] if details else 0          # chunks fully written by an earlier attempt
    chunks = plan_chunks(publisher, size=100)
    for i in range(start, len(chunks)):
        await write_chunk_idempotently(chunks[i])  # upsert by record id, never blind insert
        activity.heartbeat(i + 1)                  # details: number of chunks durably written
    return len(chunks)

Three details in that loop are deliberate. The heartbeat is sent after the chunk is durably written, so the recorded number never claims work that might be lost. The details are the smallest thing that lets a retry resume, here a chunk count, not the records themselves. And the chunk write is an upsert keyed by record id, for a reason that comes from how heartbeats are delivered.

Heartbeats are throttled, so details can lag

Workers do not send every heartbeat to the server. The Temporal documentation says heartbeats "may be throttled by the Worker": the interval is the smaller of 0.8 times the Heartbeat Timeout (a 30-second default when no timeout is set) and a maximum that defaults to 60 seconds. The SDK keeps the latest details and sends them when the interval allows. So when a worker dies, the last details the server has may be one or more chunks behind the last chunk the worker finished.

That is harmless if a repeated chunk is harmless. The companion example replays exactly this case: attempt 1 writes seven chunks, the server's last recorded detail is six, and attempt 2 resumes at chunk 7.

Chunk writesDistinct recordsRows stored
Upsert by record id1,2001,200
Blind insert1,2001,300

One chunk of duplicates is the cost of treating heartbeat details as exact. Treat them as a lower bound on progress, and make every unit of work safe to repeat.

Measured on the simulation

The companion example is a discrete-time simulation of the documented rules, not a Temporal run: 12 chunks of 30 seconds, a crash after chunk 7, a 30-minute Start-To-Close limit.

ConfigurationCrash noticed afterRetry starts at chunkChunks repeatedTotal time
No heartbeat1,590 s172,160 s
Heartbeat every chunk, 90 s timeout, no details90 s17660 s
Heartbeat with details, 90 s timeout90 s80450 s
Heartbeat timeout set to 15 min under a 10 min limit390 s80750 s

The heartbeat alone turns a 26-minute silence into 90 seconds. The details are what stop the retry from redoing the work. The last row is the configuration mistake: a heartbeat timeout above Start-To-Close is silently capped, so the crash is noticed only when the 10-minute attempt limit expires. The details still arrive, because the throttle never exceeds 60 seconds, so the retry resumes correctly; what the mistake costs is detection time, five minutes more than the honest 90-second timeout.

Choosing the numbers

A practical rule is to set the Heartbeat Timeout to a few times the longest gap you expect between two progress points. With 30-second chunks, 90 seconds tolerates one slow chunk without false failures. Then set Start-To-Close from the largest legitimate run, not from fear: here, 1,200 records at the slowest observed rate, plus margin. If the heartbeat timeout is short and honest, Start-To-Close can be generous without costing detection time.

Heartbeats also carry cancellation. The documentation notes that cancellation is delivered to activities when they heartbeat, and that an activity that does not heartbeat cannot receive one. For an agent workflow this matters when a person cancels a case mid-run: without heartbeats, the re-index keeps running to completion after the case is closed.

What stays deterministic, and what the agent decides

The agent decides that a re-index is needed and for which publisher. Everything else is ordinary workflow code: the chunk plan, the resume point, the idempotent write, the timeouts and the retry policy. Do not let the model choose timeouts or resume points from its own reading of a log; give the tool a fixed contract and have it report progress through heartbeats, which the workflow can observe.

The same split applies outside Temporal. AWS Step Functions has HeartbeatSeconds on task states with SendTaskHeartbeat, and a queue-based worker can extend a visibility timeout or lease while recording a checkpoint. The names change; the two-timeout structure and the idempotent unit of work do not.

When not to heartbeat

The Temporal documentation lists short API calls and small file reads as unsuitable for heartbeating, and that is right. A tool that finishes in two seconds should have a tight Start-To-Close timeout and no heartbeat. Heartbeats cost a little worker and server work and add code paths to test. They earn their place in calls that run for minutes, process items in batches, or wait on something external such as a long export.

Migration steps for an existing long tool

  1. Find tool calls whose p99 duration is above a minute and whose timeout was set high "to be safe". These are the candidates.
  2. Split the work into units you can write idempotently, and choose a progress marker per unit.
  3. Add heartbeats after each durable unit with the marker as details, and read the details at the start of the activity.
  4. Set the Heartbeat Timeout from the slowest unit, and keep it below Start-To-Close.
  5. Test by killing a worker mid-run in a staging environment and checking where attempt 2 starts and whether any record was written twice.

Verification

The six offline tests in the companion example check the detection times and resume points in the table above, the cap at Start-To-Close, and the duplicate-row count with lagging details under upsert and insert. The example does not start a Temporal server; the SDK names in the code block are from temporalio's Python SDK 1.35.0 source, and the timeout rules are quoted from the Temporal documentation linked below.

Primary references and authorship

Original explanation and synthetic examples by Sandeep Belgavi. Companion code: Sandeep-BA/agent-decision-lab/examples/python/tool_call_heartbeat_resume. Views are my own and do not represent any employer, past or present.