Claude · Coding

Code generation

Build from a spec, one layer at a time, with agreed types and an existing file as the pattern to follow.

Claude code generation

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.

Frequently asked
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.