All Agent Engineering

Resume an interrupted Codex batch task in the same session

Use exec resume with a saved session ID, and record which steps were continuations and which were new tasks.

AINavWritten / Updated
On this page

At a glance

When a batch run is interrupted, the decision is not simply to run it again but to work out which remaining tasks depend on the original session's context and which can start a fresh codex exec. This article shows how to write the operation record: the input is the saved session ID and the list of remaining tasks, and the deliverable is a record showing whether each step was a continuation or a new task. The only basis is the official non-interactive documentation, and none of the commands or observations below was executed or verified.

Runtime and applicability

Article type
Engineering practice
Verified
Applies to
  • Codex Resume an interrupted Codex batch task in the same session

Execution locations and responsibilities

  • Codex host and calling client Use the workflow below to distinguish responsibilities of the caller, runtime, and external actions.
Engineering workflow order
Engineering workflow order1. Save the session identifier before work starts → 2. Take stock of the remaining items after the interruption → 3. Separate continuations from new tasks → 4. Run the continuation → 5. Validate the result and update the ledger → 6. Close out, or record the next interruption. Arrows show the order of checks in the guide. Follow each step’s prerequisites, branches and stop conditions; unexecuted steps are not completed work.1. Save the session identifier before work starts2. Take stock of the remaining items after theinterruption3. Separate continuations from new tasks4. Run the continuation5. Validate the result and update the ledger6. Close out, or record the next interruption

Arrows show the order of checks in the guide. Follow each step’s prerequisites, branches and stop conditions; unexecuted steps are not completed work.

When to use

  • With --json enabled, the thread.started event in the stream carries a thread_id. The documentation does not say it equals the SESSION_ID used in codex exec resume <SESSION_ID>, so confirm that by trial rather than assuming it.

When not to use

  • If the run used --ephemeral, the documentation says only that rollout files are not persisted and does not say whether resume is possible afterwards; do not count on resuming such a batch.
  • Whether thread_id equals SESSION_ID, whether resume keeps the sandbox settings and which options resume accepts are all unstated; confirm them in a controlled environment before relying on them.
  • This record does not handle approval request interaction or thread creation through the SDK; those belong to other tasks.

Prerequisites and input

  • The event stream was saved with --json when the batch started, or the session identifier was saved some other way; without it this workflow can only treat remaining tasks as new tasks.
  • Each batch item has its own completion status and output file, so you can tell which items finished.
  • The batch was not run with --ephemeral, and the storage holding the session files is still available at recovery time.

Engineering checks

Where the session identifier comes from — With --json enabled, the thread.started event in the stream carries a thread_id. The documentation does not say it equals the SESSION_ID used in codex exec resume <SESSION_ID>, so confirm that by trial rather than assuming it.

Continuation versus a new task — codex exec resume --last continues the most recent session, codex exec resume <SESSION_ID> targets a specific one, and a plain codex exec starts a new task. The official example is a two-stage pipeline: review first, then resume so it fixes the problems it found.

Whether session files survive — --ephemeral means session rollout files are not persisted to disk, and the documentation separately mentions that workers with network-mounted home directories need to keep job state local and retain the rollout. Both decide whether there is anything to resume.

1. Save the session identifier before work starts

Where: the batch ledger, your own task record. Input: the thread.started event in the --json event stream. Action: read the thread_id and write it to the ledger together with the batch number and item list. Completion check: the ledger has a non-empty identifier for the batch, written before the first item was processed. Stop condition: if no identifier is available, do not guess with --last; downgrade recovery to new tasks and note that in the ledger.

2. Take stock of the remaining items after the interruption

Where: the ledger and each item's output file. Input: the ledger, existing output files and the previous validation results. Action: mark each item as completed, not started or unclear; completion is judged by validated output, not by progress seen in a log. Completion check: every item has exactly one status. Stop condition: confirm unclear items by hand, and keep them out of the recovery scope until then.

3. Separate continuations from new tasks

Where: the 'action type' column of the ledger. Input: the remaining items and how much each depends on the original session's context. Action: items that depend on earlier findings or decisions are marked as continuations; independent items that do not need the old context are marked as new tasks and use a plain codex exec. On a runner with several parallel jobs, prefer naming the session with codex exec resume <SESSION_ID>, because the documentation does not define which session --last picks in that case. Completion check: every remaining item has an action type and a reason. Stop condition: if the dependency cannot be judged, treat the item as a new task and say so in the ledger.

4. Run the continuation

Where: the runtime host recorded in the ledger, normally the original runner. Input: the saved session ID and a one-sentence prompt describing the remaining items. Action: run codex exec resume <SESSION_ID> with the prompt. The documentation does not say whether resume keeps the original sandbox settings, and shows no example combining it with --json or -o, so state those options explicitly and trial them on a small scale first. Completion check: the run produced a result and the ledger records the identifier and options actually used. Stop condition: if the session is reported as not found, record the error and stop; do not silently turn it into a new task, and let a person decide.

5. Validate the result and update the ledger

Where: the ledger and the output files. Input: the output from the continuation or the new task. Action: reuse the original batch's output contract and validation; if --json was also enabled this time, write the new thread_id from thread.started next to the saved identifier without presuming they match. Completion check: each ledger item carries its action type, the identifier used and its validation result. Stop condition: an item that fails validation stays incomplete, and already validated output is not overwritten.

6. Close out, or record the next interruption

Where: the batch summary in the ledger. Input: the final status of every item. Action: close the batch if all items pass validation; otherwise write the stop reason (session lost, validation failed, dependency unclear) and keep the latest identifier for next time. Completion check: the summary shows how many items were continuations and how many were new tasks. Stop condition: if a status cannot be explained, keep the batch open rather than force it to complete.

Task example

This is an original manual walkthrough and no command was run. Imagine a batch of four review items where the runner is interrupted while processing the second, and the ledger already holds a saved thread_id. After the inventory, items 1 and 2 have validated output; item 3 must refer to item 1's findings and is marked as a continuation; item 4 is unrelated and is marked as a new task. The expected operation record is: item 3 uses codex exec resume <SESSION_ID>, item 4 uses a plain codex exec, and the two action types differ in the ledger. If the resume for item 3 reports that the session cannot be found, the expected handling is to stop and record it so a person can decide to make it a new task, not to start over quietly. All of this is hypothetical expectation, not observed behavior.

Completion checks

  • The session identifier was saved when the batch began, and the ledger shows where and in what time order it was stored.
  • Every remaining item states whether it is a continuation or a new task, with a reason.
  • Continuations name an explicit session ID instead of relying on --last on a runner with parallel jobs.
  • Output that already passed validation was not overwritten by a later run.

Verification scope

Only the official non-interactive documentation was read. Installation, login, API execution and output were not tested, and every command and observation here is a hypothetical expectation grounded in the documentation, not a run result. The mapping between identifiers, the scope of --last and resume option compatibility are not given in the documentation and remain to be confirmed.

Sources

All links are official pages; verification dates are listed per source, and links point to the version checked.

  1. Codex · Codex documentation: codex-batch

Found an error? Tell us →