Advanced Claude Code Workflows
One agent in one session is the baseline. This page covers what changes when the work is bigger than that: changes that span many files, parallel sessions that must not collide, orchestration across many agents, verification that does not rely on trust, CI integration, huge repositories, and tasks that outlive a single context window. The mechanics are exercised on the bookshelf project; the multi-agent features are described from Anthropic's documentation and labelled by maturity.
claude -p are described from the documentation and were not executed.Who Holds the Plan?
Anthropic's workflows page draws the key distinction between ways of running a multi-step task: who decides what runs next (Source 1). With subagents, skills and agent teams, Claude orchestrates turn by turn and results land in a context window. With a dynamic workflow, a script holds the plan and only the final answer reaches Claude's context.
Reach for the lightest option that fits. Anthropic's agent guidance (Chapter 3) advises starting with the simplest solution and adding complexity only when it measurably helps; the agent-teams page says the same in practice — they add coordination overhead and use significantly more tokens, and for sequential tasks, same-file edits or work with many dependencies a single session or subagents are more effective (Source 2).
Complex Multi-File Changes
For a change that touches several packages — a shared type plus every call site — Anthropic's large-codebase guidance gives two techniques: hand Claude the whole change in one session, so the decisions behind each edit stay consistent, and plan first in plan mode. It adds that Claude writes the plan to a file, and Claude Code re-injects that plan file after each context compaction, so the plan survives where conversation history may not (Source 3).
Notice the order: files in dependency order, a check after each group, and a stop rule. A multi-file change fails when an early assumption is wrong and later edits compound it; frequent checks turn that into a one-step revert instead of a repository-wide cleanup.
Large-Scale Refactoring
Two ingredients carry over from Chapter 9: an oracle (compare behaviour against the original) and small, checkable steps. What changes at scale is that one conversation cannot hold hundreds of files. This is what dynamic workflows are for: Anthropic's own example prompts include migrating every file in a directory "working on each file in its own isolated copy", and auditing every route handler with adversarial verification of each finding before it is reported (Source 1).
- Availability: dynamic workflows are available on all paid plans, with Anthropic API access and on Amazon Bedrock, Google Cloud's Agent Platform and Microsoft Foundry; on Pro you turn them on from the Dynamic workflows row in
/config(Source 1). - Approve before it runs: Claude Code shows the planned phases and lets you view the raw script first; the exact prompt depends on your permission mode.
- Try a slice first: the docs advise running on one directory before the whole repository, because one run can use much more than the same task in conversation. A Large workflow warning appears above 25 agents or about 1.5 million projected tokens, and you can set a size guideline (
smallunder 5 agents,mediumunder 10,largeunder 50). - Hard limits: up to 16 concurrent agents by default, 4,096 items per
parallel()orpipeline()call, and 1,000 agents per run. - Not interactive mid-run: a run pauses only for agent permission prompts or a usage-limit wait; for sign-off between stages, run each stage as its own workflow.
Parallel Task Execution
The simplest, most documented form of parallelism is the one from Chapter 10: one git worktree per session, so edits do not collide. Here is that pattern run for real on bookshelf. Two independent changes — a title-search endpoint and a stats endpoint — each lived in its own worktree and branch, and their test suites ran at the same time.
Then the merge, which is where parallel work succeeds or fails:
The stats change added only new files, so it merged cleanly with the search change. A third worktree edited the same insertion point in BookController:
What it shows: two sessions run in half the wall-clock of doing them one after another (27 s for both here, because Maven ran concurrently), and independence is a property of which files each session touches. This is exactly Anthropic's advice for agent teams too: break the work so each teammate owns a different set of files, because two editing the same file leads to overwrites (Source 2). Plan the split before you start the sessions.
Multi-Agent Development and Subagent Orchestration
| Subagents | Agent teams | Dynamic workflows | |
|---|---|---|---|
| Status (as documented) | Documented | Experimental, disabled by default | Documented; plan-dependent |
| Who decides what runs next | Claude, turn by turn | The lead, turn by turn; peers self-coordinate | The script |
| Where intermediate results live | Claude's context | A shared task list and mailboxes | Script variables |
| Scale | A few per turn (up to 20 concurrent by default) | A handful of long-running peers; docs suggest starting with 3–5 | Dozens to hundreds per run |
| Repeatable? | The worker definition | The team definition | The orchestration itself (saveable) |
The table combines the workflows page's comparison with the subagent and agent-team pages (Source 1, Source 2, Source 4).
- Enable: set
CLAUDE_CODE_EXPERIMENTAL_AGENT_TEAMS=1. The page's warning says agent teams are experimental, with known limitations around session resumption, task coordination and shutdown. - Shape: one session is the team lead; teammates are separate Claude Code sessions with their own context windows, a shared task list and a mailbox. You can message any teammate directly.
- Best first uses: research and review, not parallel code edits — for example three reviewers on one pull request with distinct lenses (security, performance, test coverage), or several investigators testing competing hypotheses and trying to disprove each other. The page notes sequential investigation suffers from anchoring and that adversarial investigators make the surviving theory more likely to be the root cause.
- Costs and limits: significantly more tokens than one session; one team per session; no nested teams; no resumption of in-process teammates; teammate permission prompts surface in the lead; teammates inherit the lead's permission mode, so a lead on
--dangerously-skip-permissionsmakes them all bypass. The costs page says agent teams use roughly seven times more tokens than a standard session when teammates run in plan mode (Source 5).
Automated Testing and Verification
More autonomy needs a stricter gate. A useful gate answers two questions mechanically: do the tests pass? and is the change the size we expected? The second matters because an agent that "fixes" a failure by rewriting half the code is a risk the tests alone do not reveal. Here is the bookshelf gate:
Reading it: the whole feature branch is 29 files away from main — a legitimate but large change that a reviewer should see in pieces — while the latest unit of work is 10 files. The right budget depends on the task; the point is that the number is visible and enforced. Three ways to wire a gate in, from weakest to strongest:
| Where | How | Strength |
|---|---|---|
| In the prompt / CLAUDE.md | "Run scripts/verify.sh before you say you are done." | A request; Claude usually follows it |
| A Stop hook | Exit code 2 sends Claude back to work (Chapter 10); capped at eight consecutive blocks | Enforced by Claude Code on every stop |
| CI | The same script as a required status check on the pull request | Enforced outside the agent entirely |
- Cap the spend and turns of unattended runs:
--max-turnsand, with-p,--max-budget-usd(the latter appears in this machine'sclaude --help, Chapter 6). - Ask for evidence, not assurance: the command and its output (Chapter 8).
- Adversarial verification: workflows can have independent agents try to refute each other's findings before reporting; Anthropic describes this as a way to a more trustworthy result than a single pass (Source 1). It lowers false positives; it does not replace a deterministic check.
CI/CD Integration
Two entry points exist, both built on the same engine: the CLI's non-interactive mode, and the Claude Code GitHub Action, which is itself built on the Agent SDK (Source 6, Source 7).
claude -p- Basics:
claude -p "…"prints the response and exits; it exits 0 on success and non-zero on failure. Output formats:text(default),json(result, session ID, metadata; includestotal_cost_usdas a client-side estimate) andstream-json.--json-schemawithjsonoutput returns validatedstructured_output. - Permissions with nobody watching: in
-pa call that needs approval is denied unless something approves it. Use--allowedToolswith narrow rules (the docs' example allows onlygit diff,git log,git statusandgit commit),--permission-mode dontAskfor locked-down runs, or--permission-prompts noneso denied requests do not stall the run. - Reproducibility:
--bareskips auto-discovery of hooks, skills, subagents, plugins, MCP servers, auto memory and CLAUDE.md, so the same prompt behaves the same on every machine; Anthropic calls it the recommended mode for scripted and SDK calls and says it will become the default for-pin a future release. In bare mode authentication usesANTHROPIC_API_KEYor anapiKeyHelper, not your subscription login. - Piping: stdin is read (capped at 10MB), so
git diff main | claude -p "…"works without giving Claude shell permission to read the diff.
A pull-request workflow that runs the tests first and only then asks Claude to review. It follows the structure of the docs' review example (a prompt input puts the action in automation mode; claude_args passes CLI flags). I wrote this file for bookshelf; it was parsed to confirm it is valid YAML with the intended jobs, but it was not run on GitHub.
(One quirk seen while checking: PyYAML reads the bare key on as the boolean True, a YAML 1.1 behaviour, so the script looked the triggers up under True. GitHub itself treats on as the trigger key.)
- Secrets: store the key as a GitHub secret, never in the repository. The docs warn against committing API keys or OAuth tokens. For a shared organisation secret, use an API key; an OAuth token belongs to whoever ran
claude setup-token. - Who can trigger it: by default the triggering user needs write access, and bot actors are rejected unless listed in
allowed_bots, which keeps bots from triggering Claude in a loop. - Cost controls:
--max-turns, workflow timeouts and concurrency limits; runs consume GitHub Actions minutes and API tokens. - Least privilege: the review job above gets read permissions only; the docs'
@claudeexample grants write because it can push commits. Grant what the job needs. - Fork pull requests: on public repositories GitHub withholds secrets from fork-triggered runs, so the review runs only for same-repository branches.
Working with Large Repositories
In a large codebase the defaults tuned for small projects can fill the context window with unrelated instructions and file reads, costing tokens and degrading performance. Anthropic's large-codebase guide scopes Claude to the part you are touching (Source 3):
| I want to… | Use |
|---|---|
| Load only the conventions for the code I touch | Per-directory CLAUDE.md files (ancestors load at launch; subdirectories on demand) |
| Skip another team's package | claudeMdExcludes glob patterns (at any settings scope; arrays merge) |
| Stop Claude opening build output or vendored code | Read deny rules such as Read(./**/dist/**/*) |
| Find definitions without scanning files | A code-intelligence plugin (language server) |
| Check out only some directories in a worktree | worktree.sparsePaths (plus symlinkDirectories to share node_modules) |
| Edit a sibling package from a subdirectory | --add-dir or permissions.additionalDirectories |
| Share conventions as versioned units | Skills, then a plugin, as per-directory files stop scaling |
Two caveats from the same page matter. Project settings in .claude/settings.json are not inherited from parent directories the way CLAUDE.md files are, so each starting directory needs its own. And Read deny rules cover Claude's built-in file tools and recognised file commands, but a Bash grep -r over a directory containing denied files can still list them, and subprocesses that open files themselves are not covered. For Spring/Maven monorepos the same pattern holds: per-module CLAUDE.md with that module's test command, deny **/target/**.
Long-Running Tasks
A project that spans many context windows hits a problem Anthropic's engineering team describes plainly: each new session starts with no memory of the last; with only a high-level prompt a capable model tends to try to build too much at once, leaving half-finished work, and a later session may see some progress and wrongly declare the project done (Source 8). Their remedy is a harness pattern: an initializer step that creates a feature list, a progress log, a startup script and an initial git commit; then coding sessions that each advance one item and leave the repository ready to merge.
The article's other findings map to this guide's lessons: agents marked features complete after unit tests or simple API checks without confirming end-to-end behaviour (so verify like a user would); git lets agents revert to working states; and structured JSON status was chosen because models alter it less casually than Markdown. Anthropic also names open questions — whether one general agent beats specialised testing or QA agents is unclear, and the approach was tested mainly on full-stack web development (Source 8). Here is the pattern at bookshelf scale:
- Only statuses change. Instruct the agent that it may flip
statusonly after the verification command passes, and never edit or delete features. - Tools for the unattended case:
/looprepeats a prompt in a session;claude --bgstarts a background session (agent view is a research preview); a run that hits a claude.ai usage limit can pause and resume itself in an interactive session whenautoContinueAtUsageLimitis on, but not in-p, background or Remote Control sessions (Source 1). An expired login stops unattended work. - Checkpoints are session-scoped. Git commits are the durable record.
Repeatable Engineering Workflows
A process you run on every branch should not be re-typed. Anthropic's docs offer three levels, each more portable than the last:
| Level | What you save | Where |
|---|---|---|
| Skill | Instructions Claude follows (/verify-build) | .claude/skills/<name>/SKILL.md |
| Saved workflow | The orchestration script from a good run: in /workflows, select the run and press s; it becomes /<name> and accepts input through args | .claude/workflows/ (shared) or ~/.claude/workflows/ (yours) |
| Plugin | Skills, agents, hooks, MCP servers and workflows as one versioned unit | A marketplace; plugin workflows are namespaced, e.g. /acme-tools:release-audit |
A repeatable workflow also benefits from being run the same way in CI: the GitHub Action can run a skill from your repository (check out the repo first, pass /skill-name as the prompt) or a plugin skill (Source 7). Packaging is the subject of Chapter 12.
Scaling Up Too Fast
CONFLICT above). Decide file ownership first./workflows token use; stop early if the first units look wrong.--max-turns, --max-budget-usd and timeouts; a stuck agent loop is billed like a working one.--allowedTools rules or dontAsk, and run in an isolated runner.Key Takeaways, Exercises and Sources
- Ask who holds the plan: you, Claude, peers or a script. Use the lightest option that works.
- Parallel work is about file ownership. Two worktrees ran at once and merged cleanly; a third touching the same lines conflicted.
- Scale needs the oracle inside each unit of work, not only at the end.
- Gate with numbers: tests passing and change size within budget, enforced by a hook or CI.
- CI integration is the same engine:
claude -pand the GitHub Action, with narrow permissions, secrets in secrets, and spend caps. - Long tasks live in files and git; sessions are disposable.
- Know the maturity: agent teams are experimental; agent view is a research preview; workflows are documented but plan-dependent.
Exercise 1 Beginner
Create two worktrees from a small repository, make an independent one-file change in each, and merge both. Then make a third change to the same lines as the first and observe the conflict markers.
Learning outcome: see that independence comes from disjoint files.
Exercise 2 Intermediate
Write a verify.sh for your own project that runs the tests and enforces a changed-files budget. Make it fail on purpose twice (a failing test; an over-budget change) and record the exit codes.
Learning outcome: build a mechanical gate you can attach to a Stop hook or CI.
Exercise 3 Advanced
Design a long-running task for a feature set of ten items: write the feature_list.json, a progress file, the orientation script and the rule for when a status may change. Then run two fresh sessions on it and check whether the second one correctly picked up where the first stopped.
Learning outcome: test whether your harness survives a context reset.
- Anthropic, Orchestrate subagents at scale with dynamic workflows — comparison table, availability, approval, saving, limits, cost, size guideline, usage-limit behaviour.
- Anthropic, Orchestrate teams of Claude Code sessions — experimental status, architecture, use cases, limitations, permissions, message handling.
- Anthropic, Monorepos and large codebases — CLAUDE.md layering, excludes, deny rules, code intelligence, sparse worktrees, additional directories, cross-package changes.
- Anthropic, Subagents — concurrency limit and nesting (also Chapter 10).
- Anthropic, Manage costs — agent-team token cost; enablement variable.
- Anthropic, Run Claude Code programmatically —
-p, output formats,--bare, permissions in non-interactive runs, piping. - Anthropic, Claude Code GitHub Actions — setup, modes, who can trigger, example workflows, secrets, costs.
- Anthropic, Effective harnesses for long-running agents — initializer/coding-agent pattern, feature lists, progress files, testing, limitations. Engineering article; not product documentation.
Executed on this machine, 2026-10-30: git worktree/merge, two concurrent ./mvnw -B test runs and the merged suite (Spring Boot 4.1.2, OpenJDK 21.0.2, Maven 3.10.0 wrapper), the verification script, jq status script, and a PyYAML parse of the workflow. Not executed: claude -p, dynamic workflows, agent teams, background sessions, GitHub Actions runs. The two "agents" were a person making edits by hand.