Contents Lesson 8 of 9

12 min read · practitioner

Hand a task off: background agents and pull requests

By the end of this lesson you will have one task finished by an agent you did not watch — a new test file on its own branch, made in its own worktree while you did something else — reviewed by you as a diff, run green by you, and merged; and a short list of the tasks you will never hand off.

The idea

Everything so far happened with you at the keyboard, reading each step. That does not scale past one task at a time, and some tasks do not need you watching: you need the result, checked. A background agent works on its own while you work on something else, and you meet its work the way a reviewer meets a colleague's: as a branch to read.

Two ways to hand off

A session on your machine that you do not watch. The two agents do this differently.

  • Claude Code: claude --bg "the task" starts a session that goes straight to the background. claude agents opens a screen listing every background session — working, waiting for you, or done — where you can look in, reply, or attach to the full conversation. Before it edits anything, a background session moves into its own git worktree under .claude/worktrees/, so two of them, or one of them and you, never write over each other. Agent view is a research preview: the screen and its keys may change, the idea will not.
  • Codex: codex exec "the task" runs one task without stopping to ask, and ends when the task does. Run it in a second terminal, in a worktree you made yourself, and walk away. It starts read-only; --sandbox workspace-write lets it edit inside that folder, with no network, and .git stays read-only, so it cannot commit. The commit is yours, after you read the diff.

An agent on GitHub. In Claude Code, /install-github-app installs the Claude GitHub App and prepares a workflow, after which mentioning @claude in an issue or pull request comment starts an agent on GitHub's machines. It needs a Claude credential stored as a repository secret — one more secret to guard. Codex's counterpart is its cloud: you connect the repository to Codex in ChatGPT's settings, and @codex review in a pull request comment asks it for a review. Either way the agent reaches your repository and spends your plan's usage while you are not looking. Learn the local way first.

Each background session spends your plan's usage on its own. Three at once is three times the spend.

What to hand off, and what never

hand off                              keep
a test for code that exists            anything that touches the key
a doc you can check against the code   anything you cannot describe in three lines
"find why this test is flaky"          a design choice between two good options
a mechanical change in many files      anything that spends live API calls unattended

The left column has one thing in common: the result can be checked by a command or a quick read, and a wrong result costs a deleted branch. The right column fails that test. A background agent with your MCP sign-in can spend a week of free calls before you look; a design choice made for you is one you will be reviewing without having decided it.

The pull request is the review point

A hand-off ends at a branch, never on main. Read the diff like any other — the four questions from Start From Zero, and whether it did only what was asked. Run the checks yourself rather than trusting its report that it did. Then merge, or delete the branch: a deleted branch is the cheapest outcome a hand-off can have, and you should be willing to choose it.

The vaguer the task, the bigger the review. "Add a test for ageMinutes" comes back as one file you can read in a minute. "Improve the page" comes back as whatever the agent decided an improvement was, spread across the repository. Hand off tasks you could have written the acceptance criteria for.

Try it now

The prompt

Commit everything first: the background session starts from your current state. Run git status; if it lists changes you did not make yourself, find out what they are before you commit them. Then, in your shell, not inside a session, hand off the same task with your agent.

With Claude Code, from the starter folder:

claude --bg "Add tests/quotes.test.ts with tests for ageMinutes() in src/lib/quotes.ts: a single row gives the minutes since its timestamp, an empty list gives null, and a timestamp in the future gives 0. Do not change anything under src/. Run npm test, commit on a new branch named test/age-minutes, and stop."

If it answers Workspace not trusted, the folder has never been opened in an interactive session: run claude there once, accept the question about trusting the folder, quit, and run the command again.

With Codex, make the branch and its worktree yourself, install there — the sandbox will not let the agent reach the network — and start it in a second terminal:

git worktree add ../starter-age-minutes -b test/age-minutes
cd ../starter-age-minutes
npm install
codex exec --sandbox workspace-write "Add tests/quotes.test.ts with tests for ageMinutes() in src/lib/quotes.ts: a single row gives the minutes since its timestamp, an empty list gives null, and a timestamp in the future gives 0. Do not change anything under src/. Run npm test and stop. Do not commit."

Go and do something else for ten minutes. Then come back: with Claude Code, run claude agents and look at the row; with Codex, read the last message codex exec printed in its terminal.

What the agent should do

  • Work in its own worktree: Claude Code moves into one before writing anything; Codex starts in the one you made.
  • Create one file, tests/quotes.test.ts, with three tests. Your rules from lesson 2 may say to wait for a yes before editing; a background agent cannot ask you, so it will say it took your task as the yes. That is what a hand-off is, and it is why the task has to be small.
  • Run npm test and finish without touching src/. Claude Code commits on test/age-minutes; Codex leaves the commit to you.

Check it yourself

  1. The hand-off finished: claude agents lists the session, finished or waiting for you, or codex exec has returned to the prompt.
  2. git worktree list shows the worktree — under .claude/worktrees/ for Claude Code, ../starter-age-minutes for Codex. With Codex, read git diff in that worktree now and, if it holds, commit tests/quotes.test.ts there yourself.
  3. git diff main..test/age-minutes --stat shows one new file and nothing under src/.
  4. In that worktree, run npm test yourself. Green, including the three new tests.
  5. From the starter folder, on main, merge the branch: git merge --no-ff test/age-minutes -m "merge test/age-minutes: tests for ageMinutes, written in the background", and run npm test once more. Then tidy up: git worktree remove the worktree that git worktree list showed (add --force if git says it has changes) and git branch -d test/age-minutes, so finished hand-offs do not pile up. main has not moved since the branch started, so a plain git merge would fast-forward and leave no merge commit to carry your message; --no-ff makes one. If your copy lives on GitHub, push the branch and open a pull request instead, and merge it there after reading the diff.

Break it on purpose

Hand off a vague task — claude --bg "make the page better", or codex exec --sandbox workspace-write "make the page better" in a fresh worktree — and read what comes back. Count the files it touched and how many of the changes you would have asked for. Discard it without merging: git worktree remove --force the worktree git worktree list shows for it, and git branch -D its branch. That diff is the cost of a task you could not have written criteria for.

Compare with ours

Our hand-off test file
import { describe, expect, it } from "vitest";
import { ageMinutes, type Quote } from "@/lib/quotes";

const at = (timestamp: number) => ({ timestamp }) as Quote;
const now = Date.UTC(2026, 0, 2, 12, 0); // a fixed clock, not the real one

describe("ageMinutes", () => {
  it("is the minutes since a single row", () => {
    expect(ageMinutes([at(now / 1000 - 600)], now)).toBe(10);
  });
  it("is null for no rows", () => {
    expect(ageMinutes([], now)).toBeNull();
  });
  it("never goes below zero", () => {
    expect(ageMinutes([at(now / 1000 + 300)], now)).toBe(0);
  });
});

It is on the branch ref/work-with-an-agent/8 of the starter repository.

Checkpoint

Nothing more to commit: Claude Code made the branch's commit (with Codex you made it after reading the diff), and step 5 made the merge. Check the state:

git log --oneline -3   # your merge message on top
git worktree list      # the starter folder alone
git status             # nothing to commit

Then answer this lesson's questions.