Count history events and bytes per agent turn, see why an inline transcript fills a workflow history in 112 turns, and continue-as-new without repeating effects.
A durable workflow remembers every turn of the loop
Putting an agent loop inside a durable workflow engine solves real problems. Model calls and tool calls become activities with retries, a crash resumes the loop where it stopped, and a customer reply that arrives three days later wakes the workflow through a signal instead of a polling job. The engine pays for that by recording an event history: every scheduled activity, every result, every signal and every workflow task is appended, and replay rebuilds the workflow state from it.
A deterministic business workflow has a bounded shape, so its history stays small. An agent loop does not. A long-lived assistant workflow that handles every message from one customer of Belgavi's Book Store keeps adding turns for as long as the customer keeps writing, and each turn adds a model call, a few tool calls and their results. Sooner or later the history meets the engine's limits. This guide counts that growth, shows which limit binds first for typical agent payloads, and explains how to continue-as-new at a turn boundary without repeating any effect.
The numbers use Temporal's documented defaults, because they are published and concrete: an event history is limited to 51,200 events or 50 MB, with warnings at 10,240 events or 10 MB, and a single payload is limited to 2 MB with a warning at 512 KB. The self-hosted defaults page notes that exceeding the total history size limit terminates the workflow execution. Other engines have different numbers but the same arithmetic.
Count the events in one turn
The companion example models one turn of the assistant as a customer message delivered by a signal, one model activity and two tool activities that run in parallel. Each activity produces scheduled, started and completed events, and each time the workflow wakes to react, a workflow task adds three more.
# Author: Sandeep Belgavi. Approximate history events added by one agent turn.
def events_per_turn(tools: int) -> int:
signal = 1 + 3 # WorkflowExecutionSignaled + one workflow task
model = 3 + 3 # model activity + the workflow task that handles its result
tool_calls = 3 * tools + (3 if tools else 0) # parallel tools complete into one workflow task
return signal + model + tool_calls
assert events_per_turn(2) == 19Nineteen events per turn means the 51,200-event limit arrives after about 2,695 turns, with the first warning at turn 539. Those are approximations: SDKs batch several completions into one workflow task when they arrive together, and heartbeats or timers change the count. Treat 19 as an order of magnitude to plan with, and read the real number from your own histories.
The transcript argument makes growth quadratic
Event count is rarely what stops an agent workflow. Size usually stops it first, and the cause is a habit that looks harmless: passing the conversation transcript to the model activity as an argument. Activity arguments are stored in the history with the scheduled event. If turn 50 sends the whole transcript of turns 1 to 49, and turn 51 sends turns 1 to 50, the history stores every earlier turn again on every new turn. Total size grows with the square of the number of turns.
With the example payloads, a 400-byte customer message, a 1,500-byte model reply and two tool calls of 200 bytes of arguments and 3,000 bytes of results, the transcript grows by 8,300 bytes per turn. Passed inline, the history crosses the 10 MB warning at turn 50 and the 50 MB limit at turn 112, after only 2,128 events. The same loop with the transcript stored outside the workflow and passed by reference, so only the new customer message travels in the activity input, reaches the event limit first at turn 2,695 with about 23.6 MB of history.
design first warning first limit
transcript inline turn 50 (10 MB) turn 112 (50 MB, 2,128 events)
transcript by reference turn 539 (10,240 ev) turn 2,695 (51,200 events, about 23.6 MB)That is a factor of 24 from one argument. Tool results behave the same way: a tool that returns a 40 KB search result each turn fills the history quickly even without an inline transcript. Large results belong in external storage, with the activity returning a reference and a small summary the workflow needs for its next decision.
Continue-as-new at a turn boundary
Continue-as-new closes the current execution and atomically starts a new one with the same workflow ID, a new run ID and a fresh history, passing whatever state you hand it as input. Temporal's Python SDK exposes workflow.info().is_continue_as_new_suggested() so the workflow can ask the server whether it is time, and the documentation recommends not calling continue-as-new from inside signal or update handlers; wait for handlers to finish in the main workflow method first.
For an agent loop, the right moment is between turns: the model reply for the last customer message has been produced, every tool call it requested has completed, and the reply has been sent. Cutting there means the carried state is small and complete.
# Author: Sandeep Belgavi. State that crosses the continue-as-new boundary.
@dataclass
class CarriedState:
case_id: str # stable across runs, unlike the run id
next_turn: int
transcript_ref: str # pointer into the store, never the transcript itself
summary: str # what the model needs to keep the thread
completed_effect_keys: tuple # recent effects, so a replay never repeats one
pending_approval: str | None # an approval still waiting carries over explicitlyIn the companion example a 500-turn conversation continues-as-new every 200 turns. Each of the three runs stays below 3,900 events, far under the first warning, and the case keeps one workflow ID throughout.
Keep effects exactly-once across runs
The subtle failure is in idempotency keys. Inside one execution, it is natural to build an effect key from the workflow run ID and the step, because the run ID is unique. Across continue-as-new it is not stable: the new execution has a new run ID. If a boundary ever lands in the middle of a turn, for example because a bug calls continue-as-new after a refund activity completed but before the turn was marked done, the new run repeats that turn and sends a new key.
The example reproduces that case. With keys built from the run ID, the tool's idempotency store sees two different keys for turn 200 and sends two refunds for CASE-7731. With keys built from the case ID, the turn number and the tool name, the store recognises the second attempt and sends one. Build effect keys from identifiers that survive the boundary, and carry the most recent completed keys in the state so the new run can check them before calling a tool at all.
What stays deterministic
Everything about the boundary is deterministic and should be testable without a model: when to continue (a suggestion from the server, or a fixed turn or size budget), what the carried state contains, and how effect keys are formed. The model never decides whether to continue-as-new, and it never writes into the carried state directly. The summary field is the one place model output crosses the boundary, so treat it like any other model output: it informs the next prompt, but nothing downstream relies on it for correctness.
Migrating an existing agent workflow
First, measure. Read history length and size for your longest-running executions and compute events and bytes per turn. Temporal exposes current history length and size to workflow code as well, which makes the check cheap to log.
Second, remove inline transcripts and large results from activity arguments. Store them outside the workflow, pass references and keep only decision-relevant fields in the history. This alone usually moves the limit by an order of magnitude.
Third, add the continue-as-new check at the turn boundary, with carried state and effect keys that do not depend on the run ID. Version this change like any other workflow code change, because executions already in flight replay the old code.
Fourth, test the boundary explicitly. The Temporal documentation shows a test hook that lowers the history threshold so a test reaches continue-as-new quickly; use the same idea to run a conversation across several boundaries and assert that every effect happened exactly once.
When you do not need this
If each case closes within a bounded number of turns, as most help-desk cases do, a workflow per case never approaches the limits and continue-as-new is unnecessary. Start a new workflow per case and keep the long-lived customer history in a database. The long-lived assistant pattern is the one that needs a boundary, and it is worth asking whether it is needed at all before building one.
Verification checklist
Assert the events and bytes per turn from real histories against your budget. Run a test conversation long enough to cross at least two boundaries and compare the final case state with a run that never continued. Inject a boundary in the middle of a turn and assert that no effect is repeated. Alert when any execution passes the warning thresholds. For the broader rules of replay and model calls as activities, see the durable execution guide at /blogs/agents/durable_execution_agent_workflows_replay.html.