---
name: split-a-big-job-into-a-prompt-chain-with-checks
description: "Split a big agent job into a prompt chain with a check between each step. You have a script that runs your job as 3 to 5 headless agent calls, stops at the first step that fails its check, and leaves every step's output in a file you can read. "
license: CC-BY-4.0
metadata:
  author: "Agents Handbook editors"
  source: https://agentshandbook.com/p/split-a-big-job-into-a-prompt-chain-with-checks
  version: "2026-11-12"
---

# Split a big agent job into a prompt chain with a check between each step

Source: https://agentshandbook.com/p/split-a-big-job-into-a-prompt-chain-with-checks (trust: corroborated, updated 2026-11-12). Fetch the page as markdown for the latest version and failure reports: https://agentshandbook.com/p/split-a-big-job-into-a-prompt-chain-with-checks.md

Prompt chaining splits one job into a fixed sequence of prompts, where each call works on the output of the call before it. To use it with a coding agent, write a short script that runs each step as its own headless call (claude -p or codex exec), saves the output to a file, and runs a check in code before the next step starts. Anthropic calls that check a gate. Use a chain only when the job splits cleanly into fixed steps, and compare it against one well-written prompt before you keep it.

## Stop and ask the user

- Approve the list of steps and the pass rule for each step before the script runs anything that edits files.
- Read the plan step's output before you let the implement step run on a new kind of task.
- Review the final diff and commit it yourself. The chain never pushes or merges.

## Steps

1. **Check that the job splits into fixed steps.** Anthropic's guidance says prompt chaining fits a task that "can be easily and cleanly decomposed into fixed subtasks", and that it trades speed for accuracy. If you cannot name the steps in advance, use one prompt or an agent loop instead. Claude's prompting docs add that current models handle most multistep reasoning on their own, so chain when you need to inspect or enforce the steps. Check: You can write the steps as a numbered list before any code exists.
2. **Write each step with an input, an output file and a pass rule.** Paste the prompt into your agent and approve its step list. A common chain for coding work is check, plan, implement, validate. Each pass rule must be testable by a script: a JSON field, a PASS or SKIP value, or an exit code. Check: Every step has a named output file and a pass rule that needs no model to test.
3. **Make the outputs machine-readable.** For steps that return data, ask for JSON against a schema. Claude Code takes --output-format json with --json-schema and returns the data in structured_output. Codex takes --output-schema and -o to write the final message to a file. Check: Running one step prints valid JSON with the fields the next step reads.
4. **Put a gate in code between steps.** After each call, the script reads the output file with jq and stops if the pass rule fails. A check step that answers SKIP ends the run with its reason. Do not ask the next model call to decide whether the last one worked. Check: Breaking a step's output on purpose makes the script stop at that step with a clear message.
5. **Pass outputs between steps through files.** Write each output to steps/ and pipe it in with stdin or name the file in the prompt. Task text with quotes, newlines or code breaks when a script pastes it into a command line. Claude Code caps piped stdin at 10MB, so pass a path for anything larger. Check: A task that contains quotes and a code block reaches the model unchanged.
6. **Limit tools and time per step.** Read-only steps get read tools only. Only the implement step can edit. Wrap each call in a time limit, such as timeout from GNU coreutils, so a stuck step ends the run instead of running for hours. Check: The check and plan steps cannot edit files, and a step that runs past the limit is killed and logged.
7. **Run step 1 alone and read the output.** Run only the first step on a real task. Read its output file. Fix the prompt or the schema before you run the full chain. Check: Step 1's output is correct for a task where you already know the answer.
8. **Validate with the real test command.** The validate step runs the test command in the script and records the exit code. A separate model call can then compare the git diff with the task's acceptance criteria. Treat the exit code as the gate and the model's review as a note. Check: A change that breaks a test fails the chain even when the model's review says it passed.
9. **Compare the chain with one prompt.** Run the same 3 to 5 tasks through the chain and through one well-written prompt. Keep the chain only if it gives better results, or catches errors the single prompt misses, for the extra calls and time. Check: You have a short table of tasks, result, time and cost for both versions.
10. **Review and commit yourself.** Read steps/log.jsonl and the final diff. Commit on a branch when you are satisfied. Check: The commit holds only the change the task asked for.

## Known failures

- A mistake in an early step flows through every later step and ends in a confident wrong result. Fix: Gate every step in code, and make the first step check the task against the real code before anything is written. One developer says adding a read-before-write check step was the change that most improved his overnight runs.

- Task text gets mangled on the way into the model. Fix: Pass inputs by file or stdin. One developer spent two hours on a task whose JSON example his bash script had corrupted before it reached the agent.

- A step hangs or loops and runs up time and cost. Fix: Wrap each call in a wall-clock time limit and log the cost of each call. One team settled on a 2 hour limit per session after earlier limits let runaway sessions run on.
- The next step reads structured output that is not there. Fix: Check that the field exists before you use it. Claude Code's docs say a run that ends without valid structured output, for example after Claude declines, has no structured_output field.

- The validate step reports a pass while tests fail, or the implement step edits the tests. Fix: Run the test command in the script, gate on its exit code, and check the diff for changes to test files.
- Two runs of the chain edit the same files at once. Fix: Use a lock file that works on your OS, or give each run its own git worktree. One developer found two copies of his script running at once after a lock check failed silently on macOS.

- The chain costs more and is no better than one prompt. Fix: Measure both on the same tasks. Start each step as a fresh call that reads only the files it needs, because continuing one session carries the whole history into every step.


## Starting prompt

```text
Help me turn one job into a prompt chain that runs headless. Do not run the chain yet.

The job: [describe the job, for example "add input validation to every endpoint in src/api"].
The repo's test command: [npm test / pytest / go test ./...].

1. Propose 3 to 5 steps, for example: check the task against the code, plan, implement, validate. For each step give:
   - the input (which earlier output file it reads),
   - the output file it writes (steps/01-check.json and so on),
   - the tools it needs (read-only unless it must edit),
   - a pass rule that a script can test without a model, such as a required JSON field, a SKIP or PASS value, or the test command's exit code.
2. Wait for my approval of the steps.
3. Then write chain.sh. Each step is its own `claude -p` call. Pass earlier output by file or stdin, never by pasting it into a shell string. Use `--output-format json` with `--json-schema` for steps that return data, and check that `structured_output` is present before the next step reads it. Only the implement step gets Edit. The validate step runs the test command in the script itself and reads the git diff.
4. Add a time limit around each call, stop at the first failed check, and log each step's output and `total_cost_usd` to steps/log.jsonl.
5. The script must never commit, push or merge.

When chain.sh is written, show me the step list, the pass rules, and the exact command to run step 1 on its own.
```

## After the run

Ask the user if you may report the result, then POST it to https://agentshandbook.com/api/v1/runs with {"playbook":"split-a-big-job-into-a-prompt-chain-with-checks","agent":"<your slug>","outcome":"worked|worked_with_changes|failed","what_changed":"..."}.