---
title: "Long jobs"
description: "Agents that run for hundreds of steps continue as new and compact their conversation, so they stay within Temporal's limits."
canonical_url: "https://docs.oz0.ai/agents/long-jobs"
---
# Long jobs

> Agents that run for hundreds of steps continue as new and compact their conversation, so they stay within Temporal's limits.

For: people running and reviewing agents.

A long job grows two records: the conversation sent with every model call and the steps in Temporal history. The model's context window can fill up. Temporal recommends ending a run's history at a few thousand events and refuses a history at 50 MB. Use continue-as-new and compaction to keep those records bounded.

```yaml [agents/acme-researcher.yaml]
name: acme-researcher
runtime: pydantic-ai
model:
  primary: anthropic/claude-sonnet-5-5
tools: [acme_web.search, acme_web.fetch]
limits:
  max_steps: 400
history:
  continue_after: 50     # model requests per run; unset: when Temporal suggests it
  compact: summarize     # or trim; unset keeps the whole conversation
  max_messages: 40       # compact once the conversation is longer than this
  keep_messages: 12      # the newest messages stay as they are
```

## Continue-as-new

A job's workflow can end its run and start a fresh one under the same job ID, with what it needs to go on: the conversation, the tokens and requests used so far, the jobs it delegated to, its hooks and approvals policy, its home node, and the live stream's position. This is Temporal's continue-as-new, and every agent job does it:

- when Temporal suggests it, because the run's history grew large;
- after `history.continue_after` model requests in one run, if you set it;
- at the latest after 10,000 events in one run.

It happens between two steps, never in the middle of a tool call or while a question or an approval waits. Nothing changes for anyone who follows the job:

- The job ID stays. `job get`, `job watch`, `run`, the console and the runtime API keep working; `job list` shows the job once, with the time it first started.
- The live stream goes on where it was. A watcher that fell far behind skips what the job left behind, at most the oldest events beyond the last 500.
- Limits count the whole job: `limits.max_steps` and `limits.max_tokens` include every run, and `limits.timeout` counts from the job's start.
- The trace shows every run as one job. Steps of later runs have IDs such as `r2/activity-7`, and a step named `continued as new` marks each new run.
- Cost stays on the job ID, so the job's cost and budget cover all its runs.

## Compaction

Continue-as-new keeps Temporal's history small; compaction keeps the conversation small. Once the conversation is longer than `max_messages`, before the next model call, the older turns are taken out:

<table>
<thead>
  <tr>
    <th>
      <code>
        compact
      </code>
    </th>
    
    <th>
      What takes their place
    </th>
  </tr>
</thead>

<tbody>
  <tr>
    <td>
      <code>
        summarize
      </code>
    </td>
    
    <td>
      A summary, written by the agent's own model: the task, facts, decisions, names and IDs, results that matter, and what remains. The call goes through the gateway like any other, so it is metered on the job and shows in its trace as a step named <code>
        compact
      </code>
      
      , with its tokens and cost.
    </td>
  </tr>
  
  <tr>
    <td>
      <code>
        trim
      </code>
    </td>
    
    <td>
      A note that says how many messages were left out. No model call.
    </td>
  </tr>
</tbody>
</table>

The first message (the task) and the newest `keep_messages` messages always stay. A cut never separates a tool call from its result. The next compaction takes the earlier summary into the new one.

<tip>

Summaries cost a model call each and can lose detail; trimming is free and loses more. Set `max_messages` well above the turns an agent needs to see at once, and give the agent's instructions a hint to write down what it must not forget.

</tip>

## What it does not cover

Harness agents keep their own conversation inside the harness session, which is one activity; `history` applies to Pydantic AI agents. An `openai-agents` agent continues as new (`history.continue_after`) but does not compact: `plugin lint` warns about `history.compact` there. Flows run steps, not conversations, and do not need it.


## Sitemap

See the full [sitemap](https://docs.oz0.ai/sitemap.md) for all pages.
