PTENES
Skip to content
MODULE 2.4

🤖 Loop Patterns in Practice

Real-world loop recipes you can copy and run: tests, lint, migration, data, docs, and composite loops.

6
Topics
~45
Minutes
2.3
Prerequisite
Practical
Type
Progress: 0% 0 of 6
1

Test loop (fix, test, fix)

Imagine it this way: The natural cycle: try, see the result, fix it. It's what every programmer does manually. The difference is that Claude does dozens of cycles in minutes.

The classic pattern: "run pytest, fix the reported errors, run it again, repeat until everything passes." Claude reads the test output, identifies the cause of the failure, edits the code, and runs the tests again. In projects with good test coverage, this is devastatingly efficient.

Tip: include the specific test command in the prompt. "Run pytest tests/test_auth.py and fix issues until all tests pass" is much better than "run the tests." The more specific the command, the fewer tokens you spend figuring out what to run.

In one sentence: test loop = run, read the error, fix, repeat. The most useful pattern in loop mode.

2

Lint/format loop

Imagine it this way: Cleaning a messy house: you mop, see a stain, and mop again. Linting works the same way: run it, fix issues, run it again, until there are zero warnings.

Cleaning up lint warnings across a codebase is tedious for humans and perfect for loops. The prompt: "run eslint . and fix all errors automatically. Don’t change business logic, only formatting and style issues. Repeat until there are zero errors." Claude will iterate through the files one by one.

Be careful with aggressive auto-fixes: configure the loop so it doesn't change logic. Use flags like --fix for what's safe, and leave logic warnings for manual review. A useful filter: "fix only errors of type 'formatting' and 'style'; ignore 'logic' and 'security'."

In one sentence: lint loop = run, fix style issues, run again. Don't change logic, only formatting.

3

Migration loop

Imagine it this way: Moving house means packing box by box, room by room. You don't throw everything into the truck at once. Code migration works the same way: file by file, with validation between each one.

API, dependency version, or framework migrations are perfect tasks for a loop: repetitive, mechanical, and with clear success criteria (compiles/doesn't compile, tests pass/fail). The ideal prompt: "migrate each API v2 file to v3, running type-check after each file".

The key is to migrate one file at a time and validate between each one. If you migrate 50 files at once and something breaks, you won't know which one caused it. One at a time, with a commit between each, gives you a granular rollback and clarity about which migration introduced the problem.

In one sentence: migrate one file at a time, validating after each one. Granular commits = easy rollback.

Tests Lint Migration Data + Docs COMPOUND LOOP all criteria green Local CI everything green

Each type of loop can be used on its own or combined in a composite loop.

4

Data loop

Imagine it this way: Sifting for gold: run the material through the sieve, see what remains, adjust the mesh size, and sift again. Dirty data needs multiple cleaning passes.

Processing datasets is iterative by nature: parse, validate the schema, fix inconsistencies, and revalidate. Claude can run validation scripts, read the errors, and adjust the data or the transformation script. Especially useful for malformed CSVs, inconsistent JSON files, or database migrations.

Define clear assertions: "no email field can be null", "all timestamps must be in ISO 8601", "no duplicate rows by ID". The loop runs the validation, identifies violations, and fixes them until the assertions pass.

In one sentence: data loop = parse, validate, fix, re-validate. Clear assertions are the stopping criterion.

5

Documentation loop

Imagine it this way: Documentation is like painting: if you paint half the wall, it looks worse than not painting at all. The docs loop keeps everything consistent.

Generating and updating docs is a task humans put off forever. The ideal loop: "update the docstrings for all public functions in src/, validate that the examples compile and the internal links aren't broken. Repeat until everything is consistent".

Combine this with validation tools: link checkers, doc generators (pydoc, typedoc), and example runners. Claude can run these validators as a stopping criterion: "stop when the link checker reports zero broken links".

In one sentence: docs loop = generate, validate links and examples, fix, repeat. Living documentation.

6

Compound loop

Imagine it this way: The grand finale: not only do the tests pass, but lint is clean, the types are correct, AND the build works. Four exit gates, all green.

The compound loop combines multiple criteria: "repeat until (1) pytest passes, (2) eslint returns zero errors, (3) tsc --noEmit passes, and (4) npm run build completes successfully". It's the closest thing to a CI pipeline running locally.

The order of criteria matters: start with the fastest (lint) and finish with the slowest (build). This saves tokens because quick failures are fixed first, without spending time running the entire build only to find a formatting error.

In one sentence: composite loop = multiple criteria combined. Start with the fastest and finish with the slowest.

comandos-de-loop.sh
# Loop de testes
claude "rode pytest tests/ e corrija ate tudo passar" --allowedTools Edit,Bash

# Loop de lint
claude "rode eslint . --fix e corrija ate zero erros" --allowedTools Edit,Bash

# Loop composto
claude "corrija ate: pytest passe, eslint zero erros,
tsc --noEmit passe e npm run build ok" --allowedTools Edit,Bash

# Loop de migracao
claude "migre cada arquivo de lodash para es-toolkit.
Rode tsc apos cada arquivo." --allowedTools Edit,Bash,Read

Module Summary

✓
Test loop (fix, test, fix) — test loop = run, read the error, fix, repeat. The most useful loop pattern.
✓
Lint/format loop — lint loop = run, fix style issues, run again. Don’t change logic, only formatting.
✓
Migration loop — migrate one file at a time, validating each one. Granular commits = easy rollba.
✓
Data loop — data loop = parse, validate, fix, revalidate. Clear assertions are the crite.

Next module:

2-5 — Combining Plan + Loop: the complete pipeline for complex tasks.