Code generation
Build from a spec, one layer at a time, with agreed types and an existing file as the pattern to follow.
-
How Do You Build a Whole Module From a Spec? Have Claude interview you until the requirements are pinned down, save them to SPEC.md, then build from that file in a fresh session. Level 3: Workflows
- 1Claude interviews you
- 2SPEC.md
- 3Fresh session builds
- 4Reviewer checks against SPEC.md
-
Should Claude Build a Whole Feature at Once? Build one layer at a time when you plan to read and keep the code. Level 3: Workflows
- 1Datatests, commit
- 2Logictests, commit
- 3APItests, commit
- 4UIrun the app, commit
Then repeat -
How Do Types Make Claude's Code More Accurate? Agree the types and function signatures first, then have Claude implement them. Level 2: Context
- 1Agree the types
- 2Claude implements
- 3Language server reports errors
- 4Tests check behavior
-
How Do You Make Claude Match Your Code Style? Name one existing file that already does something similar, and tell Claude to follow it. Level 2: ContextDo
- Name one current file
- Include its test
- Let the file set the style
- Flag old patterns in CLAUDE.md
Don't- Point at deprecated code
- Describe a different style as well
- Write out your whole style guide
The prompt that gets usable code
A good code request gives Claude three things it can't work out alone: what you want, which existing code to follow, and how it can check itself. Anthropic's docs compare "add a calendar widget" with a prompt that names a file to copy, says what the widget does and rules out new libraries. Name the check as well, such as a test command or a type checker, because Claude can then fix its own mistakes before you see them. You don't need to explain your conventions if the repo already shows them.
How much to generate at once
Size the request by how much you'll read. A small, well-scoped change with a test can run end to end. For a larger feature, write a spec first, or build layer by layer with a commit between each. These cards cover four ways to control the output: a spec file, types agreed up front, a reference file, and one layer at a time. Use them together on bigger work, for example a spec with the types inside it.
How do I get better code from Claude?
Name the file to follow, describe what the code must do, say what's off limits such as new libraries, and give a command that checks the result. Anthropic's docs say a specific prompt usually saves a round of corrections.
Why doesn't Claude's code match my codebase?
In the Claude app, it can only see the code you add, so it falls back on general habits. In Claude Code, it reads the repo but can't always tell which of two patterns you prefer. Naming one existing file to follow usually settles it.
Should Claude write a whole feature in one go?
If the scope is clear and tests can judge it, it can. If you'll have to review and maintain the code, build one layer at a time and commit between them, so a wrong assumption stays small.
Why does Claude add files I didn't ask for?
Anthropic's Sonnet 5.5 guide says that model tends to add tests, docs and small supporting files, more so at higher effort. Tell it to mention extras at the end instead of adding them.