Agentic Research

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.

How a harness runsFlow diagram: a user request enters the harness, which assembles context (system prompt, tool list, memory), calls the LLM for one forward pass, and parses the output. Plain text converges straight to a final answer; a tool call first passes a permission gate (allow, ask, or deny), runs in a sandbox, and the observation is folded back into the context for another model call. The loop repeats until the model stops requesting tools.HARNESStool callplain textloop backUser requesta task, in plain wordsAssemble contextsystem prompt + tools + memoryCall the LLMone forward passParse outputtext, or a tool call?Permission gateallow / ask / denySandboxed runtool runs constrainedObservationresult folded back inFinal answerno more tool calls
1/8User request
The task arrives in plain language. The harness has to turn it into something a model can act on.
Step 1 of 8 User request

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