Skip to content

Refine Claude Code's output with examples and tests

Pick the refinement technique that fits a failure in Claude Code's output (input/output examples, tests first, the interview pattern, or one combined message) and write the examples or test cases that pin down a transformation.

Scenario

Scenario: Code Generation with Claude Code. A developer at a shipping company asks Claude Code for a function that turns the free-text weights customers type ("2 lb 4 oz", "1.5kg", "900 g") into grams.

The first version rounds to whole grams. The second keeps decimals. The third rejects "1,5 kg". Each time she rewrote the description in more detail, and each time Claude read it a little differently.

More prose was not the fix. A few examples would have been.

Explanation

When Claude Code's output is close but not right, the next message matters more than its length. Match the technique to the kind of failure.

1. Prose is read inconsistently → give 2-3 input/output examples. If two careful descriptions produce two different behaviours, show the transformation instead. Pick examples that differ from each other and cover the edge cases, so Claude doesn't copy an unintended pattern. Examples are one of the most reliable ways to steer format and structure.

2. Correctness depends on edge cases → write the tests first. Ask Claude to write tests from expected input/output pairs, run them and confirm they fail, then write code that passes them without changing the tests. Each run gives Claude a pass or fail it can read, so it iterates on its own. When a test fails, share that failure: it says exactly what is wrong. Cover expected behaviour, edge cases and any performance requirement.

3. An edge case is mishandled → give that exact case. "Handle nulls better" is vague. "With middle_name: null, the output field is an empty string, not "None"" is a test Claude can run.

4. You don't know what you don't know → the interview pattern. In an unfamiliar domain, ask Claude to interview you before it writes code: "Interview me in detail. Ask about edge cases, failure modes and tradeoffs. Then write a spec." It raises questions you hadn't considered. Then implement from the spec, ideally in a fresh session.

5. Several problems at once → combine or separate by whether they interact. If fixing one changes how another must be fixed, describe them all in one message and say how they interact. If they are independent, fix them one at a time, so each fix is easy to check.

One more signal: if you have corrected Claude more than twice on the same issue, the session is cluttered with failed attempts. Run /clear and start again with a better prompt that includes what you learned.

Worked example

Before: three rounds of prose.

Write parseWeight(text) that converts a weight in any common unit to grams. Be careful with rounding and with different ways of writing units.

"Careful with rounding" and "different ways of writing units" can each be read several ways.

After: the same request, pinned down by examples and turned into tests.

Write parseWeight(text) that returns grams as an integer, rounded half up. First write these as unit tests and run them; they should fail. Then implement until they pass, without changing the tests.

"2 lb 4 oz" -> 1021
"1,5 kg" -> 1500 (comma is a decimal separator)
"900g" -> 900
"2.5 g" -> 3
"2.4 g" -> 2
"heavy" -> throws InvalidWeightError

Six lines settle what three paragraphs didn't: integer grams, the rounding rule (exactly half rounds up, less than half rounds down), a mixed unit, a European decimal comma, a missing space and invalid input.