Simple definition#
Once Claude Code can check its own work, it no longer needs you at the keyboard. Automation means starting runs from a script, a timer or an event (claude -p, GitHub Actions, /schedule). Parallel work means running several Claude workers at once — sub-agents inside a session, background sessions in agent view, coordinated agent teams, or a scripted dynamic workflow — usually each in its own git worktree.
The unattended-run contract#
Before anything runs without you, answer five questions for that task — the harness, not the prompt, should hold the answers where possible.
| Question | Answer with |
|---|---|
| What does done look like? | /goal condition, SPEC.md, a failing test to turn green |
| How does it check its work? | Test suite, Stop hook, /code-review, a reviewer sub-agent |
| What may it touch? | --allowedTools, --permission-mode dontAsk, sandbox, a worktree, one pushable branch |
| What stops it? | --max-turns, --max-budget-usd, an organisation spend limit |
| Who signs off? | A PR, branch protection, a person approving merge or deploy |
Scripts and CI: print mode#
claude -p runs once and exits. In scripts you almost always add:
| Flag | Why |
|---|---|
--bare |
Ignore the machine’s hooks, skills, plugins, MCP and CLAUDE.md — same behaviour everywhere. Auth must come from ANTHROPIC_API_KEY, an apiKeyHelper, or cloud credentials |
--settings, --mcp-config, --append-system-prompt |
Pass in exactly what the run needs (hooks passed via --settings don’t run under --bare) |
--allowedTools + --permission-mode dontAsk |
Pre-approve the tools; refuse everything else |
--max-turns, --max-budget-usd |
Hard stops |
--output-format json or stream-json --verbose |
Parse the result or stream events |
--json-schema |
Validated structured output |
# Fan out a migration, one bounded run per file
for file in $(cat files.txt); do
claude --bare -p "Migrate $file to the new logger API and commit. Reply OK or FAIL." \
--allowedTools "Read,Edit,Bash(git add *),Bash(git commit *)" \
--permission-mode dontAsk --max-turns 20
done
# Triage a log, get JSON back
cat build.log | claude -p "Classify the failure: flaky, infra or code. Return JSON." \
--output-format json --max-turns 3
Auto mode also works with -p (--permission-mode auto): a classifier block denies that one action and the run continues — it does not pause for a human. For long-lived CI credentials on a subscription, use claude setup-token. For programmatic control in Python or TypeScript, use the Agent SDK.
GitHub, GitLab and pull requests#
| Integration | What it does | Set up |
|---|---|---|
| Claude Code GitHub Actions | Responds to @claude in issues and PR comments; turns issues into PRs; runs any prompt in a workflow |
/install-github-app, or add anthropics/claude-code-action@v1 |
| Code Review (Team/Enterprise) | Reviews every PR, verifies findings, comments inline | Admin enables it; billed through usage credits |
/code-review --comment |
Local review posted as inline PR comments | In a session |
/autofix-pr |
Cloud session that watches a PR and pushes fixes for CI failures and review comments | Needs a claude.ai login |
| GitLab CI/CD | Same patterns in GitLab pipelines | GitLab CI/CD guide |
name: Claude Code
on:
issue_comment: { types: [created] }
pull_request_review_comment: { types: [created] }
jobs:
claude:
if: contains(github.event.comment.body, '@claude')
runs-on: ubuntu-latest
permissions: { contents: write, pull-requests: write, issues: write, id-token: write }
steps:
- uses: actions/checkout@v6
with: { fetch-depth: 1 }
- uses: anthropics/claude-code-action@v1
with:
anthropic_api_key: ${{ secrets.ANTHROPIC_API_KEY }}
claude_args: "--max-turns 30 --model sonnet"
The action follows the repo’s CLAUDE.md on every run, so keep it concise and the same conventions apply in CI as on laptops. Keep the API key in secrets, give the job the least GitHub permissions it needs, and protect main so the bot cannot merge its own work.
Keeping one session working#
| Mechanism | Next turn starts when | Stops when | Use for |
|---|---|---|---|
| Auto mode | — (approves tools within a turn) | Claude judges the work done | Fewer prompts, not more turns |
/goal <condition> |
The previous turn finishes | A separate evaluator confirms the condition, or judges it impossible | Migrations, design docs with acceptance criteria, backlog burn-down |
Stop hook |
The previous turn finishes | Your script passes | Deterministic “done” for every session |
/loop [interval] <prompt> |
An interval elapses (or Claude self-paces 1 min–1 h) | You stop it, or after 7 days | Babysitting CI, polling a deploy |
Write goal conditions as something Claude’s own output can demonstrate: one measurable end state, the check that proves it, constraints that must hold, and a bound — /goal all tests in test/auth pass and lint is clean; don't modify other test files; or stop after 20 turns. The evaluator reads the transcript; it doesn’t run commands itself.
Scheduling work#
/loop |
Desktop scheduled task | Cloud scheduled task (/schedule) |
|
|---|---|---|---|
| Runs on | Your machine, in the session | Your machine | Anthropic cloud (or self-hosted environment) |
| Needs machine on / session open | Yes / Yes | Yes / No | No / No |
| Local files | Yes | Yes | No — fresh clone |
| Permissions | Inherits session | Per task | Auto mode by default |
| Minimum interval | 1 minute | 1 minute (1 hour in forms) | 1 hour |
| Triggers | Time | Time | Time, GitHub events, API |
Parallel work: five approaches#
| Approach | Who coordinates | Workers talk? | Scale | Use when |
|---|---|---|---|---|
| Sub-agents | Claude, turn by turn | Report back to caller | A few per turn | A side task would flood your context; you want a read-only reviewer |
Agent view (claude agents, --bg, /background) |
You | Report to you | Many independent sessions | Hand off independent tasks, check status at a glance |
| Agent teams (experimental) | A lead session | Directly, with a shared task list | A handful of long-running peers | Competing hypotheses, cross-layer features, research that must be challenged |
| Dynamic workflows | A script Claude writes | Via the script | Dozens to hundreds of agents | Codebase-wide audits, 500-file migrations, cross-checked research |
| Projects (claude.ai/code, desktop; beta) | One ongoing conversation that starts threads | Via the project | Ongoing, days to weeks | Long programmes of work that should continue while your machine is off |
Supporting tools: worktrees (separate checkouts), cross-session messaging (sessions pass findings), /batch (one large change split into 5–30 worktree-isolated units, one PR each), and the desktop app’s side-by-side sessions.
Sub-agents#
Claude already delegates broad searches to Explore and forks side tasks. Define your own in .claude/agents/<name>.md with restricted tools, a cheaper model, and isolation: worktree when they edit. /subtask <task> starts a forked sub-agent that inherits the conversation; Ctrl+B backgrounds a running one; /tasks lists them.
Agent view and background sessions#
claude --bg "upgrade eslint to v10 and fix all new warnings" # start detached
claude agents # one screen for every session
claude attach <id> · claude logs <id> · claude stop <id>
A dispatched session moves into its own worktree before it edits. Admins can turn this off with disableAgentView.
Agent teams#
Enable with CLAUDE_CODE_EXPERIMENTAL_AGENT_TEAMS=1, then describe the team: “Spawn three teammates to explore this: one on UX, one on architecture, one playing devil’s advocate.” The lead creates a shared task list; teammates claim tasks and message each other; you can talk to any teammate directly. Teams don’t isolate teammates in worktrees — partition files so each owns a different set. Interactive sessions only.
Dynamic workflows#
The plan lives in a JavaScript script instead of Claude’s turn-by-turn judgment, so intermediate results stay out of your context, runs are resumable, and agents can adversarially verify each other’s findings. Try /deep-research <question>, then ask Claude to write one for your task; watch with /workflows, save a good run as your own command.
Worktrees#
claude -w feature-auth # session in <repo>/.claude/worktrees/feature-auth
claude -w feature-auth --tmux # in its own tmux pane
- List gitignored files a fresh checkout needs (
.env, dev credentials) in.worktreeinclude; it copies, so keep large folders out — useworktree.symlinkDirectoriesor an install command inCLAUDE.md. - Unnamed, unchanged worktrees are removed on exit; sub-agent worktrees vanish if unchanged; leftovers are swept after
cleanupPeriodDays. claude -p --worktreeruns don’t clean up —git worktree remove <path>.
Choosing quickly#
| You want… | Use |
|---|---|
| A one-off answer in a script | claude -p |
@claude on issues and PRs |
GitHub Actions |
| Review on every PR | Code Review, or the action with /code-review --comment |
| Keep going until tests pass | /goal (session) or Stop hook (always) |
| Check something every 10 minutes while you work | /loop 10m … |
| A nightly job with your laptop shut | /schedule |
| One big mechanical change | /batch |
| Several unrelated tasks at once | claude --bg + claude agents, or worktrees |
| Workers that debate and coordinate | Agent teams |
| A 500-file audit with cross-checking | Dynamic workflow |
Common mistakes#
Review questions#
Why use --bare in CI?
It skips the runner’s hooks, skills, plugins, MCP servers and CLAUDE.md, making the run reproducible; you pass exactly what it needs with --settings, --mcp-config and --append-system-prompt.
How does /goal decide the work is finished?
After each turn a separate evaluator model reads the conversation and judges whether the condition holds; Claude continues until it does, the evaluator judges it impossible, or an error clears it.
When would you choose a dynamic workflow over agent teams? When the job is large (dozens to hundreds of agents), repeatable, and benefits from scripted cross-checking, rather than a few peers that need to discuss and coordinate.
Summary#
- Automate with
claude -p(bounded,--bare,dontAsk), GitHub Actions and Code Review. - Keep a session going with
/goalor aStophook; repeat with/loop; schedule with/schedule. - Parallelise with sub-agents, agent view, agent teams or dynamic workflows — isolated in worktrees, and paid for knowingly.
- Every unattended run: done, check, scope, cap, human sign-off.
References#
- Anthropic, Run Claude Code programmatically and GitHub Actions.
- Anthropic, Keep Claude working toward a goal, Run prompts on a schedule and Cloud scheduled tasks.
- Anthropic, Run agents in parallel, Agent view, Agent teams and Dynamic workflows.
- Anthropic, Worktrees and Code Review.