跪拜 Guibai
← All articles
Architecture

Stop Hand-Tuning Prompts: The AI Loop Pattern Automates Generation and Validation

By mONESY ·
Read original on juejin.cn ↗ Google Translate ↗ Alt translation

Prompt-by-prompt workflows don't scale; they burn engineer time on inspection and rework. An AI Loop shifts the cost from human attention to token spend, which is cheaper and faster for any task with clear, machine-checkable success criteria.

Summary

Manual prompt engineering is a dead end for consistent, high-volume content tasks. The AI Loop pattern automates the entire feedback cycle: a generation function produces output, a validation function checks it against a ruleset, and the loop repeats until the rules are satisfied or a hard limit on rounds, token spend, or repetitive output is hit. The approach turns an LLM into a self-correcting worker rather than a one-shot oracle.

The implementation uses a standard OpenAI-compatible client pointed at DeepSeek, with a `limit` object enforcing three brakes — max rounds, max total tokens, and a same-output detector. A `task` object decouples the business rules from the loop mechanics, so changing requirements means editing a single array of strings.

A full Node.js walkthrough shows the `gen`, `check`, and `needStop` functions working together inside a `while` loop. The validation step itself is an LLM call that returns structured JSON, letting the program branch on `pass`/`fail` without any human inspection. The result is a production-ready template for any text-generation job that needs to hit a quality bar automatically.

Takeaways
An AI Loop has exactly two LLM calls per iteration: one to generate content and one to validate it against a ruleset.
Three hard brakes prevent runaway costs: a maximum round count, a total token budget, and a same-output detector that stops the loop when the model gets stuck.
Decoupling the task description and rules into a config object lets you change requirements without touching the generation or validation logic.
The validation function forces the LLM to return structured JSON with a pass/fail flag and a list of broken rules, enabling fully automated branching.
All state — round number, cumulative token spend, last output, and duplicate counter — is tracked in plain variables and checked by a single `needStop()` function.
Conclusions

Treating LLM validation as a separate, structured API call is the key architectural move; it turns a fuzzy quality problem into a boolean decision the program can act on.

The pattern acknowledges that LLMs are non-deterministic and often wrong on the first try, so it builds retry logic into the system rather than expecting a perfect prompt to fix everything.

Token cost is the main tradeoff, and the triple-brake design is a practical admission that LLM calls can spiral without explicit, programmatic guardrails.

Concepts & terms
AI Loop
A programmatic pattern where an LLM generates content and a second LLM call validates it against rules, repeating until the output passes or a limit is reached.
Token Budget Brake
A cumulative counter that tracks total tokens spent across all loop iterations and halts execution when a predefined limit is exceeded, preventing cost overruns.
Same-Output Detector
A loop termination condition that compares the current LLM output to the previous one; if they match for a set number of consecutive rounds, the model is considered stuck and the loop stops.
Source: juejin.cn ↗ Google Translate ↗ Backup ↗