Anthropic Best Practices for Claude Code: A Real Guide
What "best practices" actually means for Claude Code
Searching for Anthropic best practices for Claude Code usually means one of two things: you want to stop wasting turns on vague prompts and context bloat, or you're trying to figure out how to run Claude Code safely and predictably inside a real codebase and team, not just a demo repo. Both come down to the same core habits: give Claude Code a clear, scoped picture of your project, manage what it can see and touch, and treat every session like a code review conversation rather than a magic wand.
This guide covers the concrete habits that make a measurable difference: setting up your repo so Claude Code understands it fast, writing instructions that reduce back-and-forth, controlling permissions and tool access, and keeping context tight enough that responses stay accurate over long sessions. It ends with what to do once your workflow outgrows a local CLI session and needs to run as part of a product or pipeline.
Set up your project so Claude Code doesn't have to guess
Claude Code performs noticeably better when the repo tells it what it needs to know before you ask a single question. Two things matter most:
- A
CLAUDE.mdfile at the project root. Keep it short and factual: build commands, test commands, linting rules, directory conventions, and anything unusual about the architecture (monorepo boundaries, generated files, deploy targets). Avoid prose explanations of "why" the project exists — Claude Code needs operational facts, not a mission statement. - Consistent, discoverable structure. If your test files live in a nonstandard location, or your API routes are auto-generated, say so explicitly. Claude Code will infer conventions from file layout, but confirming them in writing saves it from re-deriving the same facts every session.
# CLAUDE.md
Build: npm run build
Test: npm run test -- --watch=false
Lint: npm run lint
Do not edit files in /generated — they are build outputs.
API routes live in src/routes, one file per resource.
This single file often eliminates the first few exploratory turns of a session, which matters because those turns consume context budget you'd rather spend on the actual task.
Write scoped instructions, not open-ended requests
The single biggest lever on output quality is how you phrase the task. "Fix the bug" produces a broad, exploratory response. "The /checkout endpoint returns a 500 when cartId is missing instead of a 400 — add validation and a test" gives Claude Code a target, a failure mode, and a definition of done.
Good instructions for Claude Code share three traits:
- A concrete, verifiable outcome ("tests pass," "endpoint returns X for input Y").
- Explicit boundaries ("don't touch the auth middleware," "only modify files under
src/billing"). - A stopping condition — tell it when to stop and report back rather than continuing to refactor unrelated code.
If a task is genuinely large, break it into stages and review after each one. Long, unsupervised runs are where unwanted changes creep in — not because the model is unreliable, but because ambiguity compounds over many autonomous steps.
Manage context deliberately
Claude Code's context window is generous but not infinite, and quality degrades as irrelevant history piles up. A few habits keep sessions sharp:
- Start a new session for unrelated tasks instead of continuing an old thread "just in case it remembers something useful."
- Periodically summarize progress yourself and paste the summary into a fresh session for long-running work, rather than letting the transcript grow indefinitely.
- Point Claude Code at specific files or directories instead of asking it to "look through the codebase" — targeted reads are faster and cheaper than open-ended exploration.
Use permissions and sandboxing carefully
Claude Code can run shell commands and edit files directly, which is powerful and also the part that needs the most deliberate control. Best practice here is boring but important:
- Run it in a git-tracked directory with a clean working tree so every change is diffable and reversible.
- Review diffs before committing, especially for anything touching infrastructure, migrations, or auth.
- Restrict destructive commands (deletes, force-pushes, production deploys) to require explicit confirmation rather than letting them run unattended.
- Keep credentials and secrets out of the working directory Claude Code has access to; use environment variables injected at runtime instead of files it can read.
None of this is about distrust — it's the same discipline you'd apply to any automated tooling that can modify a live repository.
Prefer structured tool use over freeform text parsing
When Claude Code needs to interact with external systems — running tests, querying a database, calling an internal API — defining those as explicit tools with typed inputs produces far more reliable behavior than asking it to write and interpret ad hoc shell output. Structured tool calls are easier to validate, log, and constrain, and they fail more predictably when something goes wrong.
This same principle carries over once you move beyond the CLI into production usage. If you're integrating Claude into an application rather than a terminal session, defining tools explicitly (as documented at /docs/tools) keeps behavior consistent across requests instead of relying on the model to parse loosely structured text each time.
From local sessions to production API access
Claude Code is built for interactive development — a human in the loop, reviewing diffs, running commands. Best practices there don't automatically translate once you need Claude running inside a product: a backend service, a support tool, a CI pipeline generating changelogs. That's a different problem: you need stable API keys, streaming responses, usage tracking per team member, and predictable request/response contracts instead of an interactive terminal.
This is the gap SubToAPI (https://subtoapi.app) is built for. It turns the Claude access you already have into a standard HTTPS API — issue scoped sub_live_... keys per application or environment, stream responses the same way you would from any LLM API, and see usage broken down by key and by teammate instead of guessing at a shared bill. Setup follows the same /docs/quickstart, and the /docs/messages and /docs/streaming references map closely to the API-based patterns most teams already know:
curl https://api.subtoapi.app/v1/messages \
-H "Authorization: Bearer $SUBTOAPI_KEY" \
-H "content-type: application/json" \
-d '{
"model": "claude-3-5-sonnet",
"max_tokens": 1024,
"messages": [{"role": "user", "content": "Summarize this pull request diff."}]
}'
Plans start at €9 for solo use, with per-seat pricing for teams at /pricing, and a free trial at /signup if you want to test the setup before committing.
questions
Do I need a CLAUDE.md file for Claude Code to work well? No, but it noticeably reduces exploratory turns. Without it, Claude Code has to infer build commands, conventions, and boundaries from scratch each session, which costs context and time.
Is it safe to let Claude Code run shell commands unattended? For routine tasks in a clean git repo, yes. For anything destructive — deletes, deploys, force-pushes — require explicit confirmation and review diffs before committing, the same discipline you'd apply to any automated tooling.
How is using Claude Code different from calling Claude through an API? Claude Code is an interactive CLI for development sessions with a human reviewing changes. Production use — a backend service or pipeline — needs stable API keys, streaming, and usage tracking, which is what tools like SubToAPI provide on top of existing Claude access.