Stop Hand-Tuning Prompts: The AI Loop Pattern Automates Generation and Validation
AI Loop Automation Engineering Practice: Abandon Manual Prompt Tuning, Loops Are the Standard Answer!
1. What is an AI Loop? First, Understand the Core Idea
1. Industry Status: Abandon Manual Prompt Tuning, Loops Are the Standard Answer
A technical tweet with 7 million views overseas proposed a core idea: Inefficient manual prompt debugging is outdated; the core of AI engineering is designing automated Loop cycles. Even the author of Claude Code publicly stated that they almost never manually tweak prompts in daily development, relying entirely on automated loop iterations.
2. The Underlying Nature of Loops
Loops are one of the three most fundamental elements of computing: a starting point, a repeating action, and a termination condition.
- Traditional program loops: Batch processing Excel or text, replacing manual repetitive operations;
- Large model training itself is a giant Loop: Feed data → Calculate loss → Fine-tune parameters → Repeat trillions of times, eventually forming an LLM with conversational ability (the general logic behind DeepSeek, Qwen, Claude);
- The primitive mode of ordinary people using AI (manual loop): Write a prompt → Generate content → Manually check for flaws → Modify the prompt and regenerate. The whole process requires manual supervision, wasting a lot of time.
3. The Standard AI Loop Closed-Loop
Hand over the manual process to code for automatic execution, fixing two core logical steps:
completion: AI generates the target contentcheck: AI automatically verifies whether the content complies with the rules Cycle back and forth, automatically exiting after the termination condition is met.
4. Pros and Cons of AI Loops
- Advantages: Completely liberates manpower, eliminating the need for manual refreshing and prompt modification, enabling batch standardized content output.
- Disadvantages: Two rounds of LLM calls continuously consume Tokens, leading to a potential explosion in Token consumption and rising costs. Therefore, multiple braking limits must be set in the code.
2. Complete Prerequisite Dependencies and Environment Configuration Code Analysis
const { OpenAI } = require('openai');
const dotenv = require('dotenv');
dotenv.config();
// Initialize the DeepSeek large model client
const client = new OpenAI({
apiKey: process.env.DEEPSEEK_API_KEY,
baseURL: process.env.DEEPSEEK_BASE_URL
});
Dependency Description
openai: A universal OpenAI-compatible SDK. Models compatible with the OpenAI interface, such as DeepSeek, Qwen, and Claude, can reuse this package.dotenv: Reads the.envfile in the project root directory to store API keys and interface addresses, avoiding hardcoding keys and preventing leaks.
Logical Flow
dotenv.config()loads environment variables;- Instantiates a client, filling in the DeepSeek-specific key and interface address. All subsequent AI requests are initiated through
client.
3. Loop Boundary Control: The Braking Configuration limit Object
// Controllable boundaries for the Loop, triple insurance to prevent infinite loops/budget overruns
const limit = {
maxRound: 5, // Maximum iteration rounds, forced stop after 5 loops
maxToken: 2000, // Total Token consumption upper limit, terminates directly if exceeded
sameStop: 2 // If the output is identical for 2 consecutive rounds, the model is judged stuck, and the loop stops
}
Three major termination insurances solve two fatal problems of Loops:
- Infinite Loop Risk:
maxRoundlimits the maximum number of iterations, preventing infinite model calls if rules are never satisfied; - Cost Runaway Risk:
maxTokenaccumulates and counts the Tokens of all requests, terminating directly if the budget is exceeded; - Ineffective Model Iteration:
sameStopdetects if the model outputs the exact same text for multiple consecutive rounds, indicating repetitive output, making further looping unnecessary.
4. Task Standardization Configuration: The task Object
const task = {
desc: "CS2 tips copy", // Core task objective
rules: ["Title contains a number", "Body < 300 words", "Reach Global Elite", "End with a call to action"] // List of validation rules
}
Decouples business logic from loop logic, managing task information uniformly:
desc: Tells the AI what content needs to be generated this time;rules: An array of validation criteria. The generation and validation functions automatically read this array to concatenate rules. To modify requirements, only this section needs to be changed, without modifying the generation and validation code.
5. Global State Variables: Recording Loop Runtime Data
let round = 0, totalToken = 0, lastText = "", sameCount = 0;
Variable Role Breakdown:
round: Records the current loop round, increments with each loop;totalToken: Accumulates the Tokens consumed by each round of AI calls, used for budget braking judgment;lastText: Saves the copy generated in the previous round for comparison to check for duplication;sameCount: Counter for consecutive duplicate copies, terminates when reachinglimit.sameStop.
6. Termination Judgment Function needStop(): Unified Braking Logic
function needStop() {
return round >= limit.maxRound ||
totalToken >= limit.maxToken ||
sameCount >= limit.sameStop;
}
Unified encapsulation of loop termination conditions, returning a boolean value: as long as any one limit is met, the loop must stop. The judgment priority is undifferentiated; any condition triggers an immediate brake.
7. Generation Function gen(): AI Produces the Target Copy
async function gen() {
const res = await client.chat.completions.create({
model: 'deepseek-v4-flash',
messages: [{
role: 'user',
content: `Pretend you are a CS2 player,
write a piece of ${task.desc}, strictly comply with:
${task.rules.join(", ")}, only output the copy
`
}]
});
// Print the tokens consumed this round + the generated copy
console.log(res.usage.total_tokens,
res.choices[0].message.content);
// Return structured data: copy text, tokens consumed this round
return {
text: res.choices[0].message.content.trim(),
token: res.usage.total_tokens
}
}
asyncasynchronous function: The large model interface is a network request and must wait for the response asynchronously;Request parameters: Specifies the lightweight DeepSeek model
deepseek-v4-flash, passing in the integrated task prompt;Data processing:
.trim()removes leading/trailing newlines and spaces to avoid misjudging blank characters as duplicates;- Extracts the Tokens consumed by this call and returns them to the main loop function for cumulative statistics;
Outputs a structured object, making it easy for the main function to destructure and read the text and token.
8. Validation Function check(): AI Automatic Quality Inspection, Replacing Manual Review
async function check(text) {
const res = await client.chat.completions.create({
model: "deepseek-v4-flash",
messages: [
{
role: "user",
content: `Validate the copy: ${text}
Rules: ${task.rules.join(", ")},
Only output JSON {pass: Boolean, fail: Array}
`
}
]
});
// Parse the JSON string returned by the model into a JS object
return JSON.parse(res.choices[0].message.content.trim())
}
The core soul of the AI Loop: Using a large model to validate the output of a large model
Input parameter: The complete copy generated by
genin the previous step;Prompt constraint: Forces the model to return only standard JSON in a fixed format:
pass: Boolean value, true = all rules met; false = violations existfail: Array, stores the names of unsatisfied rules;
JSON.parse()converts the model's returned text into a JS object, allowing the program to directly read the validation result and achieve automated judgment without manual visual inspection.
9. Main Loop Function runLoop(): The Core of Complete AI Closed-Loop Scheduling
async function runLoop() {
console.log('AI Loop Start');
// Loop switch: continues iterating as long as no braking condition is triggered
while(!needStop()) {
round++;
console.log(`\n Round ${round}`);
// 1. Call the generation function, destructure to get copy and this round's token
const { text, token } = await gen();
// Accumulate total consumed tokens
totalToken += token;
// Judge if it's identical to the previous round's copy, update the duplicate counter
sameCount = text === lastText ? sameCount + 1 : 0;
// Save the current copy for the next round's comparison
lastText = text;
// 2. Call AI automatic validation
const { pass, fail } = await check(text);
if (pass) {
// All rules passed, end the loop directly, output the finished copy
console.log(`All rules passed, loop ends`);
console.log(`Final copy: ${text}`)
return
}
// Print the unsatisfied rules, enter the next round of iteration
console.log(`Unsatisfied: ${fail}`)
}
// Jumping out of the while loop means a braking limit was triggered, output the last round's generated content
console.log(`\n Braking triggered, forced stop. Last content: ${lastText}`)
}
Run Result
Complete Single Round Loop Execution Flow
Enter the
whileloop, round + 1;Call
gen()to generate copy, accumulate Token consumption;Compare the current text with the previous round's text, update the duplicate count;
Call
check()to let AI perform automatic quality inspection;Branch judgment:
- Validation passed: Output the finished product,
returnterminates the entire function; - Validation failed: Print the missing rules, return to the beginning of the while loop for the next round;
- Validation passed: Output the finished product,
If the loop triggers any of the maxRound/maxToken/sameStop limits, exit the while loop and output the last generated content.
10. Startup Entry Point
runLoop();
Calls the main loop function, automatically starting the entire AI generation-validation iterative closed-loop.
11. Overall Code Business Process Summary
- Load environment variables, initialize the DeepSeek model client;
- Configure loop limits and business task rules;
- Start the main loop;
- Each round: AI generates copy → Counts Tokens, judges duplication → AI automatically validates rules;
- Validation passed: Directly output the finished product, process ends;
- Validation failed: Automatically enter the next round to regenerate;
- Reaching the maximum rounds / budget / consecutive duplicates: Force termination, return the last generated content.