Build something new from an existing pattern

Point Claude Code at code that already works the way you want, so the new feature matches its structure, naming, tests and error handling.

Task: Build something new from an existing pattern · Other tasks

Fill in the details

A file, class or feature that is a good model. In Claude Code you can type @ to reference a file.

Optional. Anything the example does that the new code must not copy.

Optional. Leave empty and Claude will look for the tests that cover the example.

Copy your prompt

This site doesn't run Claude or show model output. Results depend on your input and the model you use.

2 required fields are empty.

What you type is kept in this tab's session storage so a reload doesn't lose it. Use "Clear this task" to remove it. Browsers can restore session data when they reopen tabs, so closing a tab isn't a guaranteed way to erase it.

When to use this template

  • The project already has a feature that works the way the new one should, such as another handler, widget, endpoint or job of the same kind.
  • Consistency matters more than novelty: you want the new code to look as if the same person wrote both, so reviewers and future readers find it where they expect.
  • You are new to the codebase and do not yet know its conventions well enough to describe them yourself.

When not to use it

  • The existing code is the thing you want to get away from. Copying a pattern you plan to replace spreads it further; use the migration template instead.
  • There is no close example. Pointing at something only loosely related produces code that copies the wrong details; plan the change instead.
  • The task is in a plain chat without file access. Paste the example code and use a regular coding prompt, because Claude cannot open the file by its name.

Why this structure

  • Naming a concrete example is a direct way to convey conventions. One working file carries naming, layout, error handling and test style in a form Claude can read, which a description rarely does.
  • Asking Claude to describe the pattern before writing code makes its reading visible. You can correct a misunderstanding in one line before it is repeated across several files.
  • The differences field covers the details the example gets right for its own case but wrong for yours, which is where copied code most often goes astray.
  • The rule against new dependencies keeps the new code within what the project already uses, as the example does.
  • The tests line, with your command when you give one, gives Claude a check it can run and a reason to keep going until it passes, instead of stopping when the code looks finished.

Example input (fictional)

Existing code to follow
the GitHub webhook handler in src/webhooks/github.ts
What to build
A webhook handler for payment events that records refunds and ignores event types it does not know.
Where the new code should differ
Payment events are signed with an X-Pay-Signature header instead of X-Hub-Signature-256.
Command that checks the result
npm test -- webhooks

Follow-ups to send Claude

  • Compare the new handler with the example side by side and list every place they differ. Is each difference intended?
  • The example has no test for a malformed payload. Add one for both handlers.
  • Write the pattern you described into CLAUDE.md in three or four lines, so later work follows it without being told.

Common mistakes

  • Choosing an example that is old or atypical. If the codebase has two styles, name the one you want to keep, or Claude may copy the one you are phasing out.
  • Leaving out the differences. Claude will then copy details that only made sense for the example, such as its header name or its table.
  • Skipping the pattern summary and going straight to code. The summary costs one short reply and lets you check Claude's reading of the example before any code is written.
  • Write tests first, then implement: Have Claude Code write tests that describe a feature before any implementation, confirm they fail, then write code until they all pass.
  • Code review prompt: Ask Claude to review a diff for supported issues, ordered by severity, with a location, impact and suggested fix for each finding.
  • Migrate a pattern across a codebase: Move every use of an old API, library or pattern to its replacement: Claude Code lists each place first, then changes them in checked batches.

See all templates

Sources