How Do You Make Claude Match Your Code Style?
Name one existing file that already does something similar, and tell Claude to follow it. Anthropic's Claude Code docs use this as their example of a good prompt: "follow the pattern in HotDogWidget.php and build a calendar widget." You don't have to describe your conventions at all.
Follow the pattern in [path/to/similar-file] and its test in [path/to/similar-test]. Build [new thing], which [what it does]. Put it in [folder]. Match their imports, error handling, naming and file layout. No new libraries. If you have to deviate from the pattern, tell me where and why before you do it. Finish by running [test command], and show me the output.
- Name one current file
- Include its test
- Let the file set the style
- Flag old patterns in CLAUDE.md
- Point at deprecated code
- Describe a different style as well
- Write out your whole style guide
Try it now
- 1Pick the closest sibling to what you're building, not the newest or largest file.
- 2In Claude Code, send the prompt with bare paths. In the Claude app, click +, choose Add from GitHub or Add files or photos, and add the file.
- 3In Claude Code, run
/diffto read the change. It worked if it uses the same helpers as the reference and adds no new imports you didn't expect.
Use it when
A new route, component, job or test that has a close sibling in the codebase, and any time you want the result to read like the people who wrote the rest of the project wrote it.
Skip it when
A new project with no pattern worth copying yet. Also skip it when the nearest example is the thing you're moving away from, because Claude will copy that faithfully. If nothing fits, describe the target and say no new libraries.
Why it works
Anthropic's docs say Claude works out your file layout, conventions and test framework by reading the repo. What it can't read is your preference between two reasonable approaches, and most older repos contain several. Naming a file settles that in one line. In the Claude app, Claude only sees what you give it, so you hand over the file. Anthropic's prompting docs rate examples among the most dependable ways to control format and structure, and suggest <example> tags to separate them from your instructions.
Example
You need a deleteInvoice route, and src/api/invoices/createInvoice.ts already has the shape you want. You send: Follow the pattern in src/api/invoices/createInvoice.ts and its test. Build deleteInvoice, which soft-deletes an invoice the caller owns. If the reference uses a Result return type and a structured log line, those are what you'd expect to see in the new file, with no style instructions from you.
Going further
Mark what not to copy. Anthropic's docs say CLAUDE.md is the place for misleading code, such as an old pattern that's everywhere but that new code must not copy. Their example line is src/legacy/ is still live but don't copy its patterns into new code. If a convention applies to one area only, put it in .claude/rules/api.md with a paths: filter such as src/api/**/*.ts, so it loads only when Claude works there. Also choose how you reference the file. Anthropic's help center says @ pulls in the whole file plus its CLAUDE.md tree, while a bare path lets Claude read selectively.
Common mistakes
- Pointing at a deprecated file because it was the first one you found. Claude can't tell it's on its way out unless you say so.
- Attaching a file and also describing a different style in words. Anthropic's memory docs warn that when instructions contradict each other, Claude may pick one arbitrarily, so let the file decide.
- Writing your whole style guide into
CLAUDE.md. The docs' test: could Claude find this by reading the code, and would it actually go and look? If both are yes, leave it out. - Expecting the app's GitHub integration to show how a pattern evolved. Anthropic's help page says it gives read-only access to files, without commit history or pull requests.
Sources
- Best practices for Claude Code
- How Claude remembers your project, Claude Code Docs
- Prompting best practices, Claude Platform Docs
- Using the GitHub integration, Claude Help Center
- Uploading files to Claude, Claude Help Center
- Models, usage, and limits in Claude Code, Claude Help Center
Checked 6 November 2026. Products change often, so confirm details with the vendor.