Knowledge card L2 · Context engineering informational

Point Claude at a File to Copy the Style

What it is

A code-generation pattern where, instead of describing your conventions, you hand Claude one existing file that already does something similar and ask it to write the new code in the same shape.

Style described in prose"use our conventions"· Claude guesses house style· Naming drifts from the repo· Review nitpicks pile upStyle shown by example"match this file"→ Same error handling shape→ Same import + naming pattern→ Merges with fewer edits
A reference file is worth a page of style instructions.

Why it works

Conventions are hard to state completely and easy to state wrongly — naming, error handling, logging, folder layout, how tests are structured. A real file encodes all of that implicitly. Claude is far better at imitating a concrete pattern than at inferring an unwritten style guide, so a reference file collapses a dozen instructions into one.

When to use it

Adding a new module, route, component, or test that has close siblings in the codebase. Any time you want the output to merge cleanly rather than read like it came from a different project.

When not to use it

Greenfield work with no pattern worth copying yet, or when the existing example is itself the thing you want to move away from — pointing at bad code just propagates it.

Prompt

Here is an existing file that does something similar: <paste path + code>.

Write <new thing> in exactly the same style: same import layout, same error-handling shape, same naming and file structure. Do not introduce new patterns or libraries. If you must deviate, call it out and say why.

Example

You need a deleteInvoice handler. Instead of describing your API conventions, you paste createInvoice.ts. Claude mirrors its validation guard, the same Result return type, the same structured-log line, and the sibling test file — the diff reads like the person who wrote the original wrote this too.

Advanced version

Point at two files: one for the happy-path shape and one for the test shape. Ask Claude to produce the implementation and its test together, each matching its respective reference, so the whole change lands in your house style in a single pass.

Common mistakes

  • Pasting a large file and expecting Claude to know which parts are the pattern — highlight the slice that matters.
  • Using an outdated or deprecated file as the exemplar, so Claude faithfully reproduces the thing you're trying to retire.
  • Describing the style in prose and attaching the file — when the two disagree, you get a muddle. Let the file be the source of truth.

Related