For many developers, the AI workflow looks something like this: write a prompt, get a response, copy what's useful, and move on.
This covers a surprising range of tasks, from summarizing a document to drafting an email or explaining a piece of code.
But when the task involves multiple steps, external data, or a decision that depends on what the model just returned, that workflow starts to break down. You end up re-prompting manually, patching output by hand, and doing work the system should be doing.
That's the point where a single prompt isn't the right tool anymore, and designing a system that runs many prompts becomes the real work.
Two terms describe these two modes of working.
Prompt engineering is how you talk to a model once: the wording, structure, and examples you include to get a useful response.
Loop engineering is the practice of designing a system that repeatedly interacts with the model, evaluates the results, and decides what to do next without waiting for a human to step in.
This guide covers both. You'll learn when a well-crafted prompt is genuinely all you need, when a loop is the better call, and how to start building one without overcomplicating it. Prompt engineering doesn't disappear inside a loop. It becomes the foundation on which everything else runs.
Table of Contents
Prompt Engineering and Loop Engineering, Explained
Prompt engineering is how you talk to a model once. The wording, the structure, the examples you include – all of it shapes the quality of what comes back.
A well-crafted prompt can dramatically change what a model produces, and getting good at it is still a genuinely useful skill. This is what most people mean when they talk about an open loop: a single exchange where a human decides what happens next after every response.
Loop engineering takes that conversation further. Instead of a single exchange, you design a system that talks to the model many times, checks the result, and decides what to do next.
Each step in that cycle can involve a different prompt, a tool call, an API request, or a combination of all three, with the system deciding what comes next rather than waiting for you to step in. This is what a closed loop looks like in practice.
But again, prompt engineering doesn't disappear when you build a loop. Every model call inside a loop still depends on a well-written prompt. The loop is the architecture, and the prompts are what make each step inside it work. Most teams only figure that out after their single-prompt workflow stops keeping up with the work.
How AI Workflows Have Changed
Early AI use was mostly transactional. Teams built internal prompt libraries, ran experiments on phrasing, and treated a well-tuned prompt as a deliverable in its own right. The value came from getting the wording right, structuring the context well, and knowing how to ask.
Tasks like CI failure analysis, issue triage, and documentation updates require the model to read something, make a decision, act on it, and check whether the action worked. A single prompt hands that decision back to a human at every step, which means the human becomes the bottleneck in any workflow with more than one moving part.
This is the difference between an open loop, where a human decides what happens next at every step, and a closed loop, where the system does.
GitHub's Agentic Workflows, which entered public preview on June 11, 2026, is one of the clearest illustrations of what changes when you close that loop.
Before agentic workflows, a developer would use an AI assistant to spot a CI failure, then manually investigate the logs, triage the issue, and push a fix. With GitHub Agentic Workflows, teams define automation goals in plain Markdown files and let coding agents handle the full sequence autonomously inside GitHub Actions.
Carvana, one of the early adopters, put it directly: tasks that previously required hours of manual engineering effort are now completed in minutes. Alex Devkar, SVP of Engineering and Analytics at Carvana, described this as expanding the use of agents for real engineering work at scale, including changes that span multiple repositories.
But not every task needs that level of machinery, and choosing the right approach matters as much as knowing how to build it.
Choosing the Right Approach
A prompt is the right tool when the task has a clear input and a useful output that a human can act on immediately. Drafting a reply to a support ticket, summarizing a pull request description, and explaining a stack trace are tasks where the value lands in a single exchange. Adding a loop to any of them would introduce complexity without adding anything meaningful to the outcome.
A loop works well when the task has multiple steps, when each step depends on the previous one's result, or when running it manually each time would cost more than building the system once. Issue triage across a repository, monitoring a pipeline and responding to failures, or generating a report from live data on a schedule are problems where a loop pays for itself quickly.
A useful test: if you find yourself copy-pasting the output of one prompt into the input of the next on a regular basis, that sequence is a loop waiting to be built.
| Use a prompt when | Use a loop when |
|---|---|
| The task is one-off or low-stakes | The task recurs on a schedule or at scale |
| You need a quick answer or draft | Multiple steps depend on each other |
| A human will decide what to do next | The system should decide what to do next |
| You are exploring or prototyping | You need the output to be reliable and auditable |
Most teams start with prompts and graduate to loops as the same tasks recur. This progression is normal, and there's no reason to over-engineer early. The right time to build a loop is when the manual version of the workflow starts costing more than the automated one.
The Real Costs and Risks of Loop Engineering
A well-designed loop can handle work that would take a human hours, run it on a schedule, and flag anything that needs attention. This is genuinely useful, but loops are software systems, and they carry the same risks as any other software system you put into production without enough testing.
Here's where loops add real value:
They handle multi-step tasks autonomously, without a human stepping in at every decision point.
They run reliably on a schedule, making recurring workflows consistent and repeatable.
They scale work that would otherwise require a proportionally larger number of people to execute.
And here's where loops introduce risk:
Debugging is harder: A single prompt shows you one input and one output. A loop that spans multiple steps and tool calls requires logging and tracing to understand what happened.
Errors compound: A bad output in step two becomes the input for step three, and by the time the loop finishes, you may have a result that looks plausible but is quietly wrong throughout.
Loops can get stuck: A poorly defined stopping condition, an ambiguous success criterion, or an unhandled API failure can cause a loop to spin indefinitely, burning tokens and time without producing anything useful.
Guardrails to Build in From the Start:
Log every step, not just the final output.
Define success and failure conditions before you write the loop, not after.
Set rate limits and maximum retry counts on every external call.
Add human review checkpoints for anything that touches production or affects real users.
Treat a loop like a cron job that makes decisions, not like a prompt that runs itself. Here's what that looks like across three real workflows.
Prompt vs. Loop Engineering: Three Real-World Examples
To make everything concrete, here are three examples of how prompt-only and loop-based approaches handle the same task differently.
Example 1: Email Summarization
Every morning, you open three client inboxes, manually pick out the emails that seem important, paste them into a model, and wait for a summary. The summarization happens quickly enough, but everything around it takes 20 minutes, and that ratio does not improve much, no matter how good your prompt gets.
A loop built around that same prompt fetches new emails on a schedule, filters by sender, subject line, and keywords, runs the summarization prompt on each batch, flags anything marked urgent, and posts a digest directly to Slack before you open your laptop.
The model is doing the same work it always did. But now, the loop is doing everything the human was doing around it.
Example 2: PR Review
A developer on your team opens three pull requests in a single afternoon. You paste the first diff into Claude, get a solid review back, copy the comments manually into GitHub, and move on.
By the third PR, you're copying and pasting the same types of comments you have written a dozen times before, flagging the same categories of issues, and doing work that follows a clear enough pattern that it shouldn't require you at every step.
Building a loop around that pattern means the review process starts the moment a PR is opened. The loop pulls the diff, retrieves relevant context from the codebase, runs the review prompt, and posts comments directly to the PR without waiting for a human to copy anything. Anything touching authentication, payments, or a sensitive part of the system gets flagged for mandatory human review before the loop proceeds.
Marks & Spencer, one of the early adopters of GitHub Agentic Workflows, built this kind of reusable workflow across their entire repository catalog. It covered vulnerability remediation, dependency maintenance, and routine change reviews across security, quality, and delivery pipelines.
Example 3: Content Operations
A content team needs to publish three technical articles a week. A writer prompts the model for a draft, edits it manually, runs a separate prompt for SEO suggestions, makes those changes, and sends it to an editor. Each article takes a full day of back-and-forth, and half of that time is spent on steps that follow the same checklist every single time.
A loop built for that pipeline researches the topic by pulling from live sources, generates a first draft, and passes it to a second model call that critiques it against a defined style guide. The model then revises based on that feedback, runs an SEO check against target keywords, and queues the final version for human approval before publishing. The writer is still in the loop for the judgment calls, and the loop handles everything else.
If any of those three examples resembles work you're already doing manually, building your first loop is a reasonable next step.
How to Start Building Your First Loop
The mistake most teams make is trying to automate too much at once. A better starting point is to create one loop for one task, along with a clear definition of what done looks like before writing a single line of code.
Identify a repetitive, multi-step task: Look for work that you or your team does manually on a schedule. If the steps are predictable and the output follows a pattern, it's a candidate for a loop.
Define success and failure upfront: What does a good output look like? What should cause the loop to stop, retry, or escalate to a human? Answering these questions before building saves a significant amount of debugging time later.
Design the sequence: Map out each step: what the loop needs to fetch, what prompt runs at each step, what it checks before moving forward, and what triggers the next action.
Add tools gradually: Start with the model alone, then add API calls, database reads, or code execution one at a time. Each addition is a new failure point, and introducing them incrementally makes debugging manageable.
Build in safety from the start: Log every step. Set rate limits and maximum retry counts on every external call. Add a human review checkpoint for anything that writes to production or affects real users.
Iterate and monitor: Run the loop on a small dataset first. Check the output manually before letting it run unsupervised. Treat the first version as a draft, not a finished system.
The prompts inside each step still matter. A loop with poorly written prompts produces unreliable output at scale, which is harder to debug than a single bad response. Good prompt engineering and good loop design are not separate skills. One depends on the other.
To put these steps into practice, here is a simple PR review loop built with Python and the Mistral API that follows exactly this pattern. The full code is available on GitHub.
The Review Prompt
Everything starts with a well-written system prompt. This is where prompt engineering still matters inside the loop. A weak prompt produces weak reviews at scale.
SYSTEM_PROMPT = """You are an experienced code reviewer. You will be given a git diff. Review it for bugs, security issues, unclear code, and missed edge cases. Only comment on things that matter - skip style nitpicks and praise. If the diff looks fine, return an empty comments list."""
The Loop Structure
The loop has four steps: load the diff, check for sensitive areas, call the model, and post the comments.
def review(diff_text: str) -> None:
if not diff_text.strip():
print("Empty diff - nothing to review.")
return
sensitive_reasons = find_sensitive_matches(diff_text)
if sensitive_reasons:
flag_for_human_review(sensitive_reasons)
return
client = Mistral(api_key=os.environ.get("MISTRAL_API_KEY"))
comments = get_ai_review(client, diff_text)
post_comments(comments)
The loop doesn't call the model if the diff touches a sensitive area. It stops, flags it for human review, and exits. This check runs before any API call is made.
The Guardrails in Practice
The sensitive area check scans both file paths and changed lines for keywords like auth, login, password, token, payment, and api_key. If any match, the loop short-circuits:
SENSITIVE_PATH_KEYWORDS = [
"auth", "login", "logout", "session", "password", "credential",
"token", "jwt", "oauth", "payment", "billing", "stripe",
]
SENSITIVE_CONTENT_KEYWORDS = [
"password", "secret", "api_key", "private_key",
"authenticate", "authorize", "permission",
]
This means a diff touching auth/login.py stops the loop before any API call is made. The flag gets printed to the console, and someone on the team handles the review manually.
Running the Loop
We can test the loop against a diff containing a get_user_by_name function with an intentional SQL injection vulnerability:
+def get_user_by_name(name):
+ query = "SELECT * FROM users WHERE name = '" + name + "'"
+ return db.execute(query)
Running the loop against this diff produces the following output:
[CRITICAL] app.py:13 - SQL injection vulnerability: The query is constructed
using string concatenation with user-provided input (`name`). This allows
an attacker to inject malicious SQL code. Use parameterized queries inhttps://cdn.hashnode.com/uploads/covers/629e46c5a6bfa05457952a41/26da1a9c-4180-4ae4-89d3-6574e119a0d9.pngstead.
[WARNING] app.py:14 - The function `get_user_by_name` does not handle the
case where no user is found. It should return `None` or raise a specific
exception to be consistent with `get_user`.
It caught two real issues, both with specific file references, line numbers, severity levels, and actionable suggestions. The loop found what a manual reviewer would have found, without anyone copying and pasting anything.
Final Thoughts
Prompt engineering and loop engineering aren't competing approaches. Every loop depends on good prompts at each stage, and getting one right makes the other more valuable.
If your task is one-off or still being figured out, a well-crafted prompt is the right tool. If the same task keeps coming back, involves multiple steps, or requires the system to act on its own output, it's worth building a loop around it.
Start with one task, define what success looks like, and build from there.