
🎙 Podcast Version
2-host dialogue — ALEX & SAM discuss this course.
Transitioning from Prompting to Loop-Based Engineering with Claude Code
Overview
This course explores a fundamental shift in how developers interact with Large Language Models (LLMs), specifically moving from "single-shot prompting" to "loop-based engineering." By analyzing the workflow used by the creator of Claude Code, this course teaches students how to stop treating AI as a chatbot and start treating it as an autonomous agent. You will learn how to construct dynamic workflows where the AI iterates on its own output, reducing manual intervention and increasing the reliability of complex code generation.
Background & Context
For the past few years, the dominant paradigm of AI interaction has been "Prompt Engineering"—the art of writing a perfect, detailed instruction to get a desired output. However, as software projects grow in complexity, a single prompt is rarely sufficient. The "vibe-coding" era, where developers rely on a "feeling" and a series of disjointed prompts, often leads to regressions, bugs, and "hallucination loops" where the AI fixes one bug but creates another.
This shift toward "writing loops" was championed by Boris, the creator of Claude Code. This approach solves the problem of manual oversight by automating the feedback loop. Instead of the human acting as the bridge between the AI and the terminal (copy-pasting code, running it, seeing an error, and pasting the error back), the system is designed so the AI can execute, observe, and correct itself. This moves the developer's role from "Writer" to "Architect of the Process."
Core Concepts
The Shift from Prompting to Looping
Traditional prompting is a linear process: Input $\rightarrow$ Output. If the output is wrong, the human provides a new input. In contrast, "Looping" is a cyclical process: Input $\rightarrow$ Execution $\rightarrow$ Observation $\rightarrow$ Correction $\rightarrow$ Execution. In this paradigm, the developer does not write the prompt for the final answer; they write the "loop" that ensures the answer is reached.
For example, instead of prompting "Write a Python script to scrape this site," a loop-based approach would be: "Scrape this site; if the output is empty, analyze the HTML, adjust the selectors, and try again until the data is successfully captured." The "loop" is the logic that governs the AI's persistence and self-correction.
Claude Code
Claude Code is a command-line interface (CLI) tool that allows Claude to interact directly with a local development environment. Unlike a web-based chat interface, Claude Code has the ability to read files, write code, run terminal commands, and analyze the output of those commands in real-time. This integration is what makes "looping" possible, as the AI can see the actual error messages from the compiler or test suite without human intervention.
By operating within the terminal, Claude Code eliminates the "context gap" that occurs when a user manually describes a bug. The AI sees the exact stack trace and the exact file state, allowing it to iterate through a "Plan $\rightarrow$ Act $\rightarrow$ Verify" cycle autonomously.
Dynamic Workflows
A dynamic workflow is a system where the path to the solution is not predefined but is determined by the AI based on the results of its previous actions. Rather than a static script, a dynamic workflow allows the AI to pivot. If a chosen library is deprecated, the AI detects the error during the "execution" phase of the loop and dynamically decides to search for an alternative library.
This differs from a standard automation script because the logic is probabilistic and adaptive. The developer defines the goal (the "what") and the constraints (the "how"), and the dynamic workflow manages the iterative steps required to reach that goal, adjusting its strategy based on the real-time feedback from the environment.
How It Works / Step-by-Step
The loop-based workflow follows a specific architectural cycle that transforms the developer's role. Here is the detailed step-by-step process:
Step 1: Defining the Objective (The Loop Goal)
Instead of writing a prompt for a specific piece of code, the developer defines the "Success State." This is the condition that must be met for the loop to terminate.
- Example: "The loop is complete when all tests in
/tests/api_tests.pypass and the linting score is 100%."
Step 2: The Execution Phase
Claude Code executes the initial plan. It writes the code to the filesystem and runs the necessary shell commands to execute that code. Because it is integrated into the CLI, it doesn't just "suggest" code; it implements it.
Step 3: The Observation Phase
The system captures the output of the execution. This includes standard output (stdout), error messages (stderr), and the current state of the filesystem. The AI "observes" whether the execution resulted in a success or a failure.
Step 4: The Correction/Iteration Phase
If the observation phase reveals an error, the AI does not ask the user for help. Instead, it analyzes the error, hypothesizes the cause, and modifies the code. It then returns to Step 2. This cycle continues—looping—until the "Success State" defined in Step 1 is achieved.
Step 5: Human Verification
The human developer reviews the final result of the loop. The developer's job is no longer to fix the bugs, but to verify that the loop's logic was sound and that the final output meets the business requirements.
Real-World Examples & Use Cases
Scenario 1: Refactoring a Legacy Module
Imagine a developer needs to migrate a large codebase from JavaScript to TypeScript.
- The Prompting Way: The developer prompts Claude to convert one file at a time, manually running the compiler, seeing the type errors, and prompting Claude again to fix those specific errors.
- The Looping Way: The developer writes a loop: "Convert all files in
/srcto TypeScript. For every file, runtsc. Iftscreturns an error, fix the type definition and runtscagain. Repeat until the entire directory compiles without errors."
Scenario 2: Bug Hunting in a Complex System
A developer is facing a race condition that only happens intermittently.
- The Prompting Way: The developer describes the bug and asks for a potential fix, then manually tests it.
- The Looping Way: The developer instructs Claude Code: "Write a stress test that triggers this race condition. Run the test in a loop of 100 iterations. If the test fails, analyze the logs, apply a fix, and restart the 100-iteration test. Do not stop until the test passes 100 times consecutively."
Scenario 3: API Integration
Integrating a third-party API with poorly documented endpoints.
- The Prompting Way: Prompting for a request, seeing a 400 Bad Request error, and manually guessing the correct headers.
- The Looping Way: "Attempt to call the API endpoint. If you receive a 400 error, inspect the response body for hints, check the documentation files in the
/docsfolder, adjust the request payload, and try again until a 200 OK is returned."
Key Insights & Takeaways
- Shift in Identity: The developer's primary job shifts from "writing prompts" to "writing loops" (designing the iterative process).
- Elimination of Manual Bridge: By using tools like Claude Code, the manual process of copying and pasting between the IDE and the LLM is eliminated.
- Self-Correction is Key: The power of the loop lies in the AI's ability to observe its own failures and correct them without human intervention.
- Success-State Orientation: Focus on defining the "Success State" (the end goal) rather than the "Implementation Steps" (the path to the goal).
- Efficiency Gain: This method is significantly more powerful than "vibe-coding" because it relies on empirical evidence (test results/compiler errors) rather than the AI's "feeling" of correctness.
- Architectural Oversight: The human becomes the orchestrator who manages the loops rather than the technician who fixes the syntax.
Common Pitfalls / What to Watch Out For
- Infinite Loops: Without a "timeout" or a "maximum iteration" limit, an AI might enter an infinite loop if it cannot solve a problem, consuming tokens and credits rapidly.
- Over-Reliance on AI Logic: Developers may stop reviewing the code because "the tests passed," potentially missing architectural flaws or security vulnerabilities that tests don't catch.
- Context Window Exhaustion: Long loops with many iterations can fill the context window with error logs, causing the AI to "forget" the original goal. It is important to periodically clear or summarize the loop history.
- Destructive Actions: Since Claude Code can run shell commands, a poorly defined loop could accidentally delete files or overwrite critical data if not restricted to specific directories.
Review Questions
- Explain the fundamental difference between a "single-shot prompt" and a "loop-based workflow." How does the role of the developer change in each?
- In the context of Claude Code, why is the "Observation Phase" critical for the success of a dynamic workflow?
- You are tasked with updating a library across 50 different files. Design a "loop" that ensures all files are updated and the project still builds successfully. What is the "Success State" and what are the "Observation" triggers?
Further Learning
- Agentic Frameworks: Explore frameworks like LangGraph or AutoGPT, which implement these looping patterns (Plan $\rightarrow$ Act $\rightarrow$ Observe) at a systemic level.
- Test-Driven Development (TDD): Learn TDD, as the "Looping" method is essentially an automated version of TDD (Write test $\rightarrow$ Fail $\rightarrow$ Write code $\rightarrow$ Pass).
- CLI Automation: Study shell scripting and Bash to better understand how to constrain and guide the environment in which Claude Code operates.