From Anthropic's own Claude Code documentation. The thread through all of them: give Claude a check it can run, keep the writer and the grader apart, and enforce the limits in settings rather than asking for them.
Give Claude a check it can run, and ask for proof
Claude stops when the work looks done. Without a check it can run, the docs say "you become the verification loop: every mistake waits for you to notice it." A check is anything that returns a pass or fail Claude can read: a test suite, a build exit code, a linter, a script that diffs output against a fixture, or a browser screenshot compared against a design.
How: Put the check in the prompt, as the docs show: "write a validateEmail function. example test cases: user@example.com is true, invalid is false, user@.com is false. run the tests after implementing". For UI work, paste the design and ask Claude to "take a screenshot of the result and compare it to the original. list differences and fix them".
Claude Code docs: Best practices for Claude Code
Reproduce the bug with a failing test, then fix the root cause
Give Claude the symptom, the likely location and what 'fixed' looks like, and have it write a failing test before it changes the code. Be specific about the scenario and your testing preferences, and ask for root-cause fixes instead of suppressed errors.
How: Docs prompts to copy: "users report that login fails after session timeout. check the auth flow in src/auth/, especially token refresh. write a failing test that reproduces the issue, then fix it". For a new test: "write a test for foo.py covering the edge case where the user is logged out. avoid mocks." For a broken build: "fix it and verify the build succeeds. address the root cause, don't suppress the error".
Claude Code docs: Best practices for Claude Code
Separate the writer from the grader
The docs say "A fresh context improves code review since Claude won't be biased toward code it just wrote," and apply the same idea to tests: "have one Claude write tests, then another write code to pass them." Before you count unattended work as done, a reviewer subagent "sees only the diff and the criteria you give it, not the reasoning that produced the change", so the agent doing the work is not the one grading it.
How: Run a Writer session and a Reviewer session in parallel (worktrees keep their edits apart). Run the bundled /code-review skill, which "reviews the current diff for bugs in a fresh subagent".
Claude Code docs: Best practices for Claude Code
Write exact test commands into CLAUDE.md
A project CLAUDE.md is where "build and test commands" and common workflows go, shared with the team through version control. Claude treats CLAUDE.md "as context, not enforced configuration", so instructions must be concrete enough to verify.
How: Run /init to generate a starter; it creates a file "with build commands, test instructions, and project conventions it discovers." Write "Run `npm test` before committing" instead of "Test your changes". Keep each file under 200 lines ("Longer files consume more context and reduce adherence").
Claude Code docs: How Claude remembers your project (CLAUDE.md)
Make 'do not edit the tests' a rule Claude Code enforces
"Permission rules are enforced by Claude Code, not by the model. Instructions in your prompt or CLAUDE.md shape what Claude tries to do, but they don't change what Claude Code allows." Rules are evaluated "deny, then ask, then allow", and "An allow rule can't carve an exception out of a deny rule."
How: Once the tests are reviewed and committed, add an Edit deny rule for the test folder to the settings the implementation session uses, for example Edit(/tests/**). Put the rule in the project's .claude/settings.json or .claude/settings.local.json, where a leading slash resolves to the primary working directory, as in the docs' Edit(/src/**/*.ts) example; in user settings it resolves to ~/.claude. The docs note that an Edit deny rule covers Claude's file tools, the Bash file commands Claude Code recognizes and redirections, but not a script that opens files itself; to stop every process from writing there, enable the sandbox.
Claude Code docs: Configure permissions
Use hooks for checks that must run every time
Hooks are user-defined shell commands that Claude Code runs at set points in its lifecycle, which "gives you deterministic control: certain actions always happen rather than relying on the LLM to choose to run them." A PreToolUse deny blocks the tool "even in bypassPermissions mode or with --dangerously-skip-permissions".
How: In .claude/settings.json: a PostToolUse hook with matcher Edit|Write runs your formatter or linter on each edited file (the docs' example pipes the file path to npx prettier --write). A PreToolUse hook script that exits with code 2 blocks edits to protected paths and tells Claude why (the docs' .claude/hooks/protect-files.sh lists .env, package-lock.json and .git/; add your test paths to that list).
Claude Code docs: Automate actions with hooks
Set a /goal with a measurable end state and a 'tests unchanged' constraint
/goal sets a completion condition and Claude keeps working toward it. After each turn a separate model checks the condition, "so completion is decided by a fresh model rather than the one doing the work." The evaluator "doesn't run commands or read files independently", so the condition must be something Claude's own output in the conversation can show.
How: Docs example: /goal all tests in test/auth pass and the lint step is clean. Give the condition "One measurable end state", "A stated check" such as "`npm test` exits 0", and constraints such as "no other test file is modified".
Claude Code docs: Keep Claude working toward a goal
Run the test suite and code review in subagents
"Running tests, fetching documentation, or processing log files can consume significant context. By delegating these to a subagent, the verbose output stays in the subagent's context while only the relevant summary returns to your main conversation."
How: Prompt: "Use a subagent to run the test suite and report only the failing tests with their error messages". Define project subagents in .claude/agents/ and check them into version control.
Claude Code docs: Subagents
Tell Claude that tests check the solution, they do not define it
Anthropic's prompting guide warns that "Claude can sometimes focus too heavily on making tests pass at the expense of more general solutions". For long tasks across several context windows it suggests tracking tests in a structured file such as tests.json and reminding Claude: "It is unacceptable to remove or edit tests because this could lead to missing or buggy functionality."
How: Add the guide's wording to the prompt or CLAUDE.md: "Implement a solution that works correctly for all valid inputs, not just the test cases. Do not hard-code values or create solutions that only work for specific test inputs." "Tests are there to verify correctness, not to define the solution." "If the task is unreasonable or infeasible, or if any of the tests are incorrect, please inform me rather than working around them." The same guide's safety prompt adds: "don't bypass safety checks (e.g. --no-verify)".
Claude Platform docs: Prompting best practices
Run Claude headless in CI with pinned-down permissions
claude -p runs Claude non-interactively for CI, pre-commit hooks and scripts. It "exits with code 0 on success and a non-zero code when the run fails, so your scripts can branch on the exit status."
How: Docs example: claude -p "Run the test suite and fix any failures" --allowedTools "Bash,Read,Edit". In CI add --bare, which skips auto-discovery of hooks, skills, subagents, plugins, MCP servers, auto memory and CLAUDE.md and is "useful for CI and scripts where you need the same result on every machine".
Claude Code docs: Run Claude Code programmatically (non-interactive mode)
Add Claude review to pull requests, but keep CI and a human merge
anthropics/claude-code-action@v1 runs Claude Code inside GitHub workflows, when someone mentions @claude or on events such as pull_request. The docs' advice: "Grant the workflow only the permissions it needs, and review Claude's changes before merging."
How: Run /install-github-app in Claude Code (or copy examples/claude.yml). For automatic reviews, use the docs' code-review workflow, which installs the code-review plugin and runs /code-review:code-review --comment when a pull request is opened, updated, reopened or marked ready for review; Claude skips drafts, closed pull requests and some others.
Claude Code docs: Claude Code GitHub Actions
Stay the approver, and isolate risky runs
The security guide is explicit: "You're responsible for reviewing proposed code and commands for safety before approval." For sensitive code it lists "Review all suggested changes before approval", "Use project-specific permission settings for sensitive repositories" and "Regularly audit your permission settings with /permissions".
How: With untrusted content: "Review suggested commands before approval", "Avoid piping untrusted content directly to Claude" and "Verify proposed changes to critical files". For extra isolation, run the whole Claude Code process inside the sandbox runtime or a dev container, and use virtual machines to run scripts and make tool calls, especially when they interact with external web services.
Claude Code docs: Security