Claude Code Workflows: Explore, Fix, Refactor, Test, Ship
Most Claude Code work is one of a handful of jobs: understand unfamiliar code, fix a bug, refactor, add tests, write documentation, open a pull request. Each one goes better with the same discipline: narrow the scope, give Claude something it can run to check itself, and review the evidence rather than the claim. This guide turns Anthropic's common-workflows and best-practices pages into recipes for each job, with prompts you can copy, and adds the session tools that keep long tasks on track: plan mode, subagents, rewind, and /clear.
It assumes you have a working setup; if not, start with Getting Started with Claude Code. For making checks run automatically after edits, see Claude Code Hooks.
Explore an unfamiliar codebase
Start broad, then narrow. From the project root:
1give me an overview of this codebase2explain the main architecture patterns used here3what are the key data models?4how is authentication handled?Then locate the code for a specific feature and follow it end to end:
1find the files that handle user authentication2how do these authentication files work together?3trace the login process from front-end to databaseUse the project's own domain words in questions; they match the identifiers Claude searches for. Ask for a glossary of project-specific terms and the coding conventions the code follows. If you already know where to look, reference it directly: @src/auth/session.ts puts the file's contents in the prompt (up to the Read tool's limit, 25,000 tokens by default), and @src/components/ shows a directory listing.
Exploration reads many files, and every file read fills the context window. For a broad question, delegate it: "use a subagent to investigate how our auth system handles token refresh". The subagent reads in its own context and returns only a summary.
Quick reference
- A code intelligence plugin for your language gives Claude precise go-to-definition and find-references instead of text search.
@references also pull inCLAUDE.mdfiles from that file's directory and its parents.- Unscoped "investigate X" requests are a documented failure pattern: Claude reads hundreds of files before doing anything useful. Scope them or use a subagent.
Remember this
Ask broad, then trace one flow end to end; push wide reading into a subagent so your main context stays usable.
Fix a bug: reproduce first
Give Claude the symptom, the command that reproduces it, and what "fixed" means. The docs' best-practices page contrasts two prompts:
1# weak2the build is failing3 4# strong5the build fails with this error: [paste error]. fix it and verify the build6succeeds. address the root cause, don't suppress the errorFor a behaviour bug, ask for a failing test first:
1users report that login fails after session timeout. check the auth flow in2src/auth/, especially token refresh. write a failing test that reproduces the3issue, then fix it and run the auth testsThe failing test is the important part. It proves Claude understood the bug, it turns "looks fixed" into a pass/fail check Claude can iterate against, and it stays behind as a regression test. Tell Claude whether the error is intermittent or consistent, and paste stack traces rather than describing them.
When Claude says it is done, ask for evidence: the test command it ran and its output, before and after.
Quick reference
- "Don't suppress the error" matters: without it, a quick fix such as
@ts-ignoreor a broad try/catch can make the symptom disappear. - For intermittent bugs, ask Claude to run the test several times, or to add logging and reproduce, before proposing a fix.
- If two attempts fail, the context is full of wrong theories.
/clearand restate the bug with what you learned.
Remember this
Symptom, reproduction command, and a failing test first; accept the fix when the same test passes and the suite is green.
Refactor in small, verified steps
Refactoring is where an agent can change the most code with the least visible effect, so it needs the strongest guardrails. The recipe from the docs:
1find deprecated API usage in our codebase2suggest how to refactor utils.js to use modern JavaScript features3refactor utils.js to use ES2024 features while maintaining the same behavior4run tests for the refactored codeThree habits make it safe. Plan first for anything touching several files: switch to plan mode (Shift+Tab until the status bar shows plan mode, or claude --permission-mode plan), have Claude list the files and steps, and edit the plan with Ctrl+G before approving it. Keep behaviour fixed: say "maintain the same behavior" and make sure tests cover the code before you start; if they do not, write characterisation tests first. Go in increments: one module or pattern per step, with tests run after each.
Point Claude at an existing example of the target pattern ("follow how HotDogWidget.php does it") rather than describing the pattern in prose.
Quick reference
- Commit before a large refactor. Rewind restores Claude's file edits but not changes made by shell commands such as codemods.
- For mechanical changes across many files,
/batch <instruction>splits the work across subagents, each in its own worktree. - Ask Claude to explain the benefit of each change; "modernise" without a reason tends to produce churn.
Remember this
Plan, pin behaviour with tests, change one thing at a time, and run the tests after every step.
Write tests that check something
Let Claude find the gaps and match your existing style:
1find functions in NotificationsService.swift that are not covered by tests2add tests for the notification service3add test cases for edge conditions in the notification service4run the new tests and fix any failuresClaude reads your existing test files to match the framework, structure, and assertion style. The quality risk is tests that pass because they assert little. Be specific about the behaviour to verify, ask for edge cases (error conditions, boundary values, unexpected input), and say what to avoid, for example "avoid mocks" when you want integration behaviour tested.
One pattern from the best-practices page works well for important code: have one session write the tests and a fresh session write the code that makes them pass. The second session cannot shape the tests around its own implementation.
Quick reference
- Read a few generated tests before trusting the count. Ask Claude which behaviour each test would catch breaking.
- A test Claude wrote and a fix Claude wrote in the same session are not independent evidence; a fresh-context reviewer is.
- Coverage thresholds are a team decision; tell Claude yours rather than letting it pick one.
Remember this
Ask for specific behaviours and edge cases, read the assertions, and separate test-writing from implementation for critical code.
Documentation and pull requests
Documentation follows the same find-generate-review loop:
1find functions without proper JSDoc comments in the auth module2add JSDoc comments to the undocumented functions in auth.js3check if the documentation follows our project standardsSpecify the style (JSDoc, docstrings), ask for examples, and focus on public APIs and complex logic rather than documenting every getter.
For pull requests, create a pr for my changes works on its own, or step through it:
1summarize the changes I've made to the authentication module2create a pr3enhance the PR description with more context about the security improvementsAsk Claude to call out risks and anything a reviewer should look at closely. When Claude creates the PR with gh pr create (or glab mr create), Claude Code links the session to it, so claude --from-pr 1234 later opens the session picker filtered to that PR.
Quick reference
- Install the
ghCLI. Claude uses it for issues, PRs, and comments, and unauthenticated API calls hit rate limits quickly. - Review the generated description against the diff; summaries can describe intent rather than what actually changed.
- Put your PR conventions (title format, required sections) in CLAUDE.md so every PR follows them.
Remember this
Generate docs and PR descriptions, but review both against the code; ask for risks explicitly.
Session tools that keep workflows on track
Most workflow failures are session failures: the wrong plan, a context full of old attempts, or a mixed bag of unrelated tasks. The tools:
- Plan mode before multi-file changes or unfamiliar code. Skip it when you could describe the diff in one sentence.
- Subagents for research: "use a subagent to review this diff for security issues" or "to investigate X". They work in a separate context and report back.
Escstops Claude mid-turn with context kept, so you can redirect.Esctwice or/rewindrestores code, conversation, or both to an earlier prompt./clearbetween unrelated tasks, and after two failed corrections on the same issue.claude --continueresumes the latest session in the directory;claude --resumepicks from a list. Name sessions with/renameso you can find them.- Worktrees for parallel work:
claude --worktree feature-authin one terminal and another name in a second.
And for scripts: git log --oneline -20 | claude -p "summarize these recent commits" runs Claude like any Unix tool, which is how these workflows move into CI.
Quick reference
- The "kitchen sink session" (one task, then an unrelated one, then back) is the most common way context fills with noise.
- A fresh session with a better prompt usually beats a long session full of corrections.
/btwasks a side question whose answer does not stay in the conversation.
Remember this
Plan before big edits, delegate reading, rewind instead of arguing, and clear between tasks.
Mistakes that cost the most time
Each of these is a variation of one problem: Claude has the wrong context or no way to check itself. The fixes are cheap. Keep sessions to one task, give every task a check, keep CLAUDE.md short enough that each line earns its place, and move anything that must happen every time, such as formatting or tests before finishing, into hooks, which run deterministically.
The flip side from the docs is worth keeping too: these are defaults, not laws. Sometimes accumulated context on one deep problem is valuable, and sometimes a vague prompt is the right way to see how Claude reads a problem before you constrain it.
| Pattern | What happens | Fix |
|---|---|---|
| Kitchen sink session | Context fills with unrelated material | /clear between unrelated tasks |
| Correcting over and over | Failed approaches pollute context | After two failures, /clear and write a better prompt |
| Over-specified CLAUDE.md | Important rules get lost and ignored | Prune it; move must-always rules to hooks |
| Trust-then-verify gap | Plausible code misses edge cases | Always give a test, script, or screenshot to check |
| Infinite exploration | Hundreds of files read, context exhausted | Scope the question or use a subagent |
Quick reference
- When Claude keeps ignoring a rule, the CLAUDE.md is usually too long, not too weak.
- If Claude asks questions CLAUDE.md answers, the wording is ambiguous; rewrite that line.
- Track what worked: the prompt shape, the context you gave, the mode you were in.
Remember this
Wrong context and no check cause most wasted time; one task per session and a check per task prevent it.
Hands-on: one bug, end to end
Pick a real bug or a small missing behaviour in a repository you know.
1. Start a fresh session. Ask for an overview of the module involved, then "trace the code path for <the behaviour>".
2. Write the bug prompt with the symptom, a reproduction command, and "write a failing test that reproduces it, then fix the root cause".
3. Before accepting, ask: "show me the test output before and after the fix".
4. Ask "use a subagent to review this diff for edge cases and regressions; report only real problems".
5. Run the full test suite, then create a pr and ask Claude to list risks in the description.
6. /clear, and in the fresh session ask whether the PR's description matches the diff.
Pass criterion: the PR contains a test that failed before the fix and passes after it, the review step found or ruled out at least one specific edge case, and you read the evidence for each step rather than Claude's summary.
Quick reference
- If Claude fixes the symptom without a failing test, stop it with
Escand ask for the test first. - If the subagent reports style nits, tell it to flag only correctness problems.
- Note the prompt that worked best; it becomes your team's template.
Remember this
Reproduce, fix, verify, review, ship, with evidence at every step.
Key takeaway
Claude Code's everyday workflows share one shape: scope the request to the right files and symptom, give Claude a check it can run (a failing test, a build, a script), let it iterate, and review the evidence rather than the claim. Exploring works best broad-then-narrow with subagents for wide reading; bug fixes start with a failing test; refactors need a plan, pinned behaviour, and small steps; generated tests and PR descriptions need a human read.
Around all of them sit the session tools: plan mode before big changes, Esc and /rewind when a turn goes wrong, /clear between tasks, and worktrees for parallel work. For the rules that keep risky actions off the table, see Claude Code Permissions; for running these workflows on parallel branches, see Claude Code Worktrees.
Practice (45 min): take one real bug through the six steps above and save the prompt you used as a template.
References
- Claude Code docs: Common workflows — checked Oct 17, 2026
- Claude Code docs: Best practices for Claude Code — checked Oct 17, 2026
Shoaib Hossain
Lead Author & Systems ArchitectSoftware engineer and distributed systems architect specializing in backend scalability, cloud-native infrastructure, databases, and AI engineering workflows. Author and maintainer of Core Concept Learning.
Related Articles
Explore this topic