Agent loop
Also: 代理迴圈 · agent 循環 · 智能體迴圈 · agentic loop
The agent's heartbeat: assemble context, call the model, parse its output, execute the action, feed the result back — repeat until the task is done or a stop condition fires.
When you will meet it
The AI-agent entry gives you four parts — model, context, tools, loop. This entry is how the fourth part turns; it is the single most important mechanism on the site. Without it you credit the model for every magical behaviour and blame it for every failure, when both usually live in a link of the loop: context assembled wrong, output misparsed, stop condition missing.
An analogy
Like a cook who tastes as they go: look at the pan, decide the next move, act, taste, decide again — until the dish is done. The LLM is the deciding cook, but the stirring, and the spoon carried back to their lips, are the surrounding code.
Minimal example
while True: # 這個迴圈就是 Agent 本身
ctx = 系統提示 + 對話歷史 + 工具結果 # ① 組上下文
out = llm(ctx) # ② 呼叫模型(唯一的 AI 步驟)
if out 是純文字:
回給用戶; break # ⑤ 停止條件
result = run(out.tool_call) # ③ 解析請求 ④ 執行工具
對話歷史 += [out, result] # 結果接回,進入下一輪Only step ② is the model; ①③④⑤ are ordinary code. Every other agent term you will meet — ReAct, planning, checkpoints — modifies some part of this loop, so learn the loop first.
What people get wrong
- Believing the model itself runs the loop. The model is invoked once per turn and has no idea a loop exists; the surrounding code feeds results back and asks again.
- No stop condition. Without exits for done and give-up, the loop burns tokens forever or oscillates between two actions.
Related terms
Next
- AI Agent 是什麼:從 ChatGPT 到會自己做事的 AI12 minChinese only
- The Ultimate Comparison of 7 AI Agent Loop Engineering Architectures: From while(true) to Multi-Agent Orchestration35 min