Stage 03 · BuilderModule 12 of 26~5h

Code Generation

Generate, refactor, and review code with Claude.

← All modules in this stage

Code is the language Claude is unusually good at — partly because there's enormous training data, partly because syntax errors give a sharp signal. This module teaches you to lean into that strength without losing the engineering discipline that keeps software working.

By the end of this module you'll have

Time: about 1 hour for the basics, ~5 hours with all three notebooks.

Prerequisites: Modules 6 (advanced prompting), 7 (building apps), 10 (conversations).


Three loops worth knowing

Loop When to use it
Spec → code Greenfield: you know what you want, don't want to type it
Bug → failing test → fix Debugging: pin down the bug before asking for a fix
Code → review Existing code: you suspect issues but haven't read carefully

All three follow the same shape: tell Claude what good looks like, then ask for the change.


Loop 1 · Spec → code with tests

Don't ask "write a function that does X". Ask for the function and its tests in one go — and read the tests first.

from anthropic import Anthropic
from dotenv import load_dotenv

load_dotenv()
client = Anthropic()

spec = """\
Function: parse_iso_duration(s: str) -> int
- Input is an ISO-8601 duration like "PT1H30M" or "P1DT2H".
- Returns total seconds as an int.
- Supports days (D), hours (H), minutes (M), seconds (S). Ignore years/months.
- Raises ValueError on bad input.
"""

prompt = f"""\
{spec}

Write:
1. The Python implementation of the function (no external deps, stdlib only).
2. A pytest module testing edge cases: valid, missing-T, all-zero, malformed input.
Reply with two fenced code blocks: implementation, then tests. No prose between them.
"""

response = client.messages.create(
    model="claude-sonnet-4-6", max_tokens=1500,
    messages=[{"role": "user", "content": prompt}],
)

print(response.content[0].text)

When you read the result, read the tests first. They tell you what the model thinks the spec means. If the tests cover what you expected, the implementation usually does too.


Loop 2 · Bug → failing test → fix

The fastest way to fix an intermittent bug is to make it deterministic. Ask Claude for a failing test before asking for a fix.

bug_report = """\
Our `slugify(s)` function crashes on strings containing the em dash "—" (U+2014).
Expected: emit '-' and continue. Actual: UnicodeEncodeError.
Code is in slug.py: ...
"""

prompt_one = f"{bug_report}\n\nWrite a single failing pytest that reproduces this bug. No commentary."
prompt_two = "Now propose the smallest change to slug.py that makes that test pass. Show a unified diff."

Two calls; two prompts. Don't merge them — the test acts as a contract that the patch must satisfy.


Loop 3 · Code review

Hand Claude code with explicit review criteria and you'll get useful feedback. Hand it code with no rubric and you'll get a list of cosmetic suggestions.

code = open("payments.py").read()

review_prompt = f"""\
Review the code below as a senior engineer. Focus on, in this order:

1. Correctness — bugs, race conditions, edge cases
2. Security — injection, privilege escalation, secrets handling
3. Failure modes — what happens on partial success, network errors
4. Tests that are missing

Do NOT comment on style, naming, or formatting unless it causes a real problem.
For each issue, output: SEVERITY (high|medium|low), the file/line, the issue, the fix.

CODE:
{code}
"""

response = client.messages.create(
    model="claude-opus-4-7",        # use Opus for high-stakes review
    max_tokens=2000,
    messages=[{"role": "user", "content": review_prompt}],
)
print(response.content[0].text)

Three things make this work:

  1. Ranked focus areas. "In this order" tells Claude what matters.
  2. Explicit anti-instruction. "Do NOT comment on style" suppresses noise.
  3. Structured output. Severity + location + issue + fix is reviewable in seconds.

What Claude is unusually good at

Where you stay in charge


Try changing one thing


Going deeper: open the notebooks


Module checklist


Next module

Module 13 · Content Creation — applying the same "spec + iterate" discipline to long-form prose.