A bakery writes a job ad. It must follow ten house rules, such as a word count and a fixed last line.
The first try asks once, with all ten rules in the prompt. The ad reads well. It still breaks some rules.
This is reflection. The model checks its own draft, then writes it again.
- The model writes a first draft.
- A second request only judges. It quotes the words, then says pass or fail for each rule.
- Code lists every rule that failed or was skipped. This list is the gate.
- The model rewrites the draft. This can happen two times.
- Every rewrite is judged, the last one too. The draft with the fewest failed rules wins.
For example, a review may find an exclamation mark. The rewrite takes it out.
sequenceDiagram
participant Shop as Your code
participant Model
Shop->>Model: write the ad
Model-->>Shop: draft
Shop->>Model: check each rule
Model-->>Shop: pass or fail
alt a rule failed or was skipped
Shop->>Model: rewrite and fix it
Model-->>Shop: new draft
else all rules passed
Shop->>Shop: keep the draft
end
- Reflection is the loop of draft, review and rewrite.
- The list of failed rules works as a gate.
uv run lab reflection naive
uv run lab reflectionAdd --case 2 or --case 3 to try the other cases.
There are three jobs with ten rules each. Code checks the final ad, out of 30.
| Version | Rules followed |
|---|---|
| Ask once | 23 |
| Draft and review | 29 |
Now break it. Open the pattern file and find to_fix. Make it return an empty list. Then
run the test:
uv run pytest tests/promises/test_reflection.pyThe test fails, because a draft with broken rules is now kept.
- A "no key found" message means the key file is missing. Copy the example file and paste your key.
- If a rule still fails at the end, the two rounds ran out. The last review shows which one.
The review is the model judging itself. It can miss things or complain about good text. When code can check a rule, let code check it.
It costs more calls than asking once.
Go to Call tools. It lets the model look things up.