跪拜 Guibai
← Back to the summary

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.

  1. Traditional program loops: Batch processing Excel or text, replacing manual repetitive operations;
  2. 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);
  3. 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:

  1. completion: AI generates the target content
  2. check: 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

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
});
  1. 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 .env file in the project root directory to store API keys and interface addresses, avoiding hardcoding keys and preventing leaks.
  2. 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:

  1. Infinite Loop Risk: maxRound limits the maximum number of iterations, preventing infinite model calls if rules are never satisfied;
  2. Cost Runaway Risk: maxToken accumulates and counts the Tokens of all requests, terminating directly if the budget is exceeded;
  3. Ineffective Model Iteration: sameStop detects 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:

5. Global State Variables: Recording Loop Runtime Data

let round = 0, totalToken = 0, lastText = "", sameCount = 0;

Variable Role Breakdown:

  1. round: Records the current loop round, increments with each loop;
  2. totalToken: Accumulates the Tokens consumed by each round of AI calls, used for budget braking judgment;
  3. lastText: Saves the copy generated in the previous round for comparison to check for duplication;
  4. sameCount: Counter for consecutive duplicate copies, terminates when reaching limit.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
    }
}
  1. async asynchronous function: The large model interface is a network request and must wait for the response asynchronously;

  2. Request parameters: Specifies the lightweight DeepSeek model deepseek-v4-flash, passing in the integrated task prompt;

  3. 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;
  4. 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

  1. Input parameter: The complete copy generated by gen in the previous step;

  2. Prompt constraint: Forces the model to return only standard JSON in a fixed format:

    • pass: Boolean value, true = all rules met; false = violations exist
    • fail: Array, stores the names of unsatisfied rules;
  3. 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 image.png

Complete Single Round Loop Execution Flow

  1. Enter the while loop, round + 1;

  2. Call gen() to generate copy, accumulate Token consumption;

  3. Compare the current text with the previous round's text, update the duplicate count;

  4. Call check() to let AI perform automatic quality inspection;

  5. Branch judgment:

    • Validation passed: Output the finished product, return terminates the entire function;
    • Validation failed: Print the missing rules, return to the beginning of the while loop for the next round;
  6. 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

  1. Load environment variables, initialize the DeepSeek model client;
  2. Configure loop limits and business task rules;
  3. Start the main loop;
  4. Each round: AI generates copy → Counts Tokens, judges duplication → AI automatically validates rules;
  5. Validation passed: Directly output the finished product, process ends;
  6. Validation failed: Automatically enter the next round to regenerate;
  7. Reaching the maximum rounds / budget / consecutive duplicates: Force termination, return the last generated content.