跪拜 Guibai
← All articles
JavaScript

Stop Writing Prompts. Design Loops That Make AI Iterate Until It's Right.

By 默_笙 ·
Read original on juejin.cn ↗ Google Translate ↗ Alt translation

Prompt engineering is brittle and labor-intensive. Shifting to loop design treats AI output as a process to be managed rather than a one-shot guess, which cuts out the tedious manual revision cycle and makes quality control programmable.

Summary

The manual back-and-forth of writing a prompt, checking the output, and rewriting the prompt is itself a loop — just an inefficient, human-driven one. An AI Loop formalizes this into three automated steps: a generation model produces content, a separate checker model validates it against a rule set, and the cycle repeats until the rules pass or a safety valve trips.

Safety valves are critical because unchecked loops cause token costs to explode. A practical implementation caps the number of rounds, sets a total token budget, and halts execution when the model produces identical output consecutively, signaling it is stuck.

DeepSeek's OpenAI-compatible API makes this straightforward to build. A single Node.js script defines a task description and an array of pass/fail rules, then hands control to a while-loop that calls generation and checking functions until the acceptance criteria are met or a limit is breached.

Takeaways
A manual prompt-and-revise workflow is a human-driven loop; automating it turns the developer into a rule designer rather than a loop controller.
An AI Loop has three stages: a generation model creates output, a checker model validates it against rules, and the cycle repeats on failure.
Three safety valves prevent runaway token costs: a maximum round limit, a total token budget, and a same-output detector that stops a stuck model.
DeepSeek's API is compatible with the OpenAI SDK, requiring only a baseURL change to use standard client libraries.
The checker model must return structured JSON (a pass boolean and a fail array) so the loop logic can decide whether to continue or exit.
Defining a task requires only a natural-language description and an explicit list of acceptance rules; the loop handles the rest.
Conclusions

Prompt engineering is often treated as a skill, but the manual revise-and-retry pattern is just an unoptimized human loop that burns time instead of tokens.

Separating generation and checking into distinct model calls mirrors how human teams use writers and editors, and it sidesteps the single-call reliability ceiling of LLMs.

Token cost is the real constraint on autonomous AI workflows, making budget-aware loop design a more practical concern than model intelligence for many tasks.

The same-output safety valve is an underappreciated signal: a model repeating itself verbatim indicates it has exhausted its useful variance under the current constraints.

Adopting OpenAI-compatible APIs as a de facto standard means infrastructure built for one provider ports to others with minimal friction, lowering the risk of vendor lock-in for loop-based systems.

Concepts & terms
AI Loop
An automated workflow where a generation model produces output, a checker model validates it against rules, and the cycle repeats until the rules pass or a safety limit is reached.
Safety Valve
A hard limit that stops an AI Loop to prevent infinite execution and runaway token costs. Common valves include maximum rounds, a token budget, and duplicate-output detection.
Completion
The generation step in an AI Loop where a model creates content based on a task description and a set of rules.
Check
The validation step where a separate model call inspects generated content against predefined rules and returns a structured pass/fail result.
sameStop
A safety valve that terminates a loop when the model produces identical output on consecutive rounds, indicating it cannot improve further under the current constraints.
Source: juejin.cn ↗ Google Translate ↗ Backup ↗