<?xml version="1.0" encoding="utf-8" standalone="yes"?><rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom"><channel><title>Agent Loop on YennJ12 Engineering Blog</title><link>https://yennj12.js.org/yennj12_blog_V4/tags/agent-loop/</link><description>Recent content in Agent Loop on YennJ12 Engineering Blog</description><generator>Hugo -- gohugo.io</generator><language>en-us</language><lastBuildDate>Fri, 07 Aug 2026 10:00:00 +0800</lastBuildDate><atom:link href="https://yennj12.js.org/yennj12_blog_V4/tags/agent-loop/feed.xml" rel="self" type="application/rss+xml"/><item><title>OpenWorker 深度解析（二）：TurnEngine — Agent 迴圈的完整解剖</title><link>https://yennj12.js.org/yennj12_blog_V4/posts/openworker-intro-part2-turnengine-deep-dive-zh/</link><pubDate>Fri, 07 Aug 2026 10:00:00 +0800</pubDate><guid>https://yennj12.js.org/yennj12_blog_V4/posts/openworker-intro-part2-turnengine-deep-dive-zh/</guid><description>大多數人以為 agent loop 就是「while 迴圈裡呼叫 LLM，有 tool_calls 就執行」。 那份實作大概 40 行，在 demo 裡跑得很好。 真實系統裡它是 1192 行，而多出來的 1152 行不是為了功能，是為了「壞掉的時候別壞得太難看」。 這篇要讀的，就是那 1152 行。
本篇是 OpenWorker 深度解析系列 的第二篇，主角只有一個檔案：coworker/engine.py。
一、Agent Loop 的本質，以及它的三個謊言 1.1 教科書版本 1# 你在每一篇 tutorial 裡看到的版本 2messages = [{&amp;#34;role&amp;#34;: &amp;#34;system&amp;#34;, &amp;#34;content&amp;#34;: SYSTEM}, {&amp;#34;role&amp;#34;: &amp;#34;user&amp;#34;, &amp;#34;content&amp;#34;: user_input}] 3while True: 4 resp = client.chat.completions.create(model=MODEL, messages=messages, tools=TOOLS) 5 msg = resp.choices[0].message 6 messages.append(msg) 7 if not msg.tool_calls: 8 break 9 for tc in msg.tool_calls: 10 result = TOOL_FUNCS[tc.</description></item></channel></rss>