{
  "source_file": "0930",
  "source_platform": "Ghost",
  "author": "Avi Chawla / Daily Dose of Data Science",
  "title_listed": "Loop Engineering Clearly Explained",
  "keywords": "",
  "domain": "大模型算法",
  "content_type": "深度技术解析",
  "is_paid_flag": "否",
  "url": "https://www.dailydoseofds.com/p/loop-engineering-clearly-explained/",
  "publish_date": "2026-06-26",
  "corpus_stage": "",
  "notes": "",
  "id": "0a2366e40aa6",
  "collected_at": "2026-10-02T13:46:54",
  "title": "Loop Engineering, Clearly Explained!",
  "body_markdown": "Jun 27, 2026\nAgents\nLoop Engineering, Clearly Explained!\nUsed by top products, including Anthropic, Google, etc.\nAvi Chawla\n👉\nEvery agent, underneath whatever framework you’re using, runs the same loop.\nwhile True:\n    response = model(context)\n    if response.has_tool_calls():\n        context += run_tools(response.tool_calls)\n    else:\n        break\nSend the context to the model\nIt responds with tool calls\nRun those tools\nAppend the results to the context\nAnd send it back.\nIt keeps going until the model replies without asking for a tool.\nThat loop is short, and it’s nearly identical across LangGraph, the OpenAI Agents SDK, and Claude Code, so nobody competes on the while statement.\nThis is exactly why the engineering effort moved somewhere else.\nMore specifically, the model and the loop are the parts you don’t write.\nWhat you write is everything around it, like when the loop stops, what stays in the context, which tools the model can reach, and how you check the result.\nSo let’s go through the loop itself, then the four parts of it that are hard to get right.\nWhere the engineering actually moved\nIt moved outward, into the layers that wrap the model.\nPrompt engineering is the words you send.\nContext engineering is everything the model sees on a turn, not just your instructions.\nHarness engineering is the code around the model that runs tools, tracks state, and recovers from errors.\nLoop engineering is the outer cycle that decides what the agent works on and when it’s done.\nEach layer wraps the one before it, so your prompt is now one input to a much larger system.\nHere’s how these systems break today.\n1) Ending a turn is not finishing the job\nThe loop stops on exactly one condition, when the model replies without a tool call. So it ends the moment the model decides it’s finished, which means the model is judging its own completion.\nwhile True:\n    response = model(context)\n    if response.has_tool_calls():\n        context += run_tools(response.tool_calls)\n    else:\n        break\nThat judgment is often wrong. A coding agent makes an edit, returns a confident summary with no further tool call, and the loop exits even though it never ran the tests, or ran them and they failed. The turn ended, but the task wasn’t done.\nSince you can’t trust the model’s own stop signal, you add conditions it doesn’t control:\nMax iterations, a hard cap so a stuck agent can’t run forever.\nBudget and time limits, a ceiling on tokens, money, and wall-clock seconds.\nNo-progress detection, which catches the agent repeating the same call with the same arguments.\nA real completion check, an automated condition that proves the job is done.\nThe completion check is super important, because it’s the only brake that replaces the model’s self-assessment with an objective signal.\n“Done” should mean the tests pass, not the model reporting that it’s done. Claude Code’s\n/goal\ncommand works this way, running the loop until a verifiable condition holds and using a separate model to confirm it.\n2) Context rot and the doom loop\nThe longer a loop runs, the more its context fills with junk, like old tool outputs, abandoned dead ends, and stale reasoning. Model quality drops as that pile grows, which the field calls context rot.\nThe loop turns rot into a spiral, where a rotted context produces a worse decision, which adds more noise, which rots the context further.\nThe community calls this the doom loop, and the agent gets less useful the longer it runs. LangChain added middleware specifically to detect doom loops in their harness.\nYou solve this by treating context as a budget, not a bucket:\nCompaction, summarizing the conversation once it gets long, and continuing from the summary.\nOffloading, pushing large outputs to a file, and keeping only the slice you need.\nSub-agents, handing a messy subtask to a separate agent so that only its clean result returns.\nThe instinct is to keep everything in case it matters later. The skill is knowing what to throw away.\n3) Tool design changes inside a loop\nAdding tools makes selection harder, not easier.\nGive the agent a hundred overlapping tools and it loses track of which one to call, so a small set of focused, non-overlapping tools works better.\nAnthropic’s rule of thumb is that if a human engineer can’t say for certain which tool fits, neither can the agent.\nVercel found that cutting an agent’s available tools raised its success rate.\nTwo more properties matter specifically because this is a loop, not a single call:\nWrites have to be safe to repeat, since the loop retries a failed step, and a retried “create customer” that makes a second customer leaves you with duplicate records and double billing.\nError messages have to tell the agent what to do next, not just what went wrong, because in a loop, that message becomes the input to the next turn, so a vague error wastes a turn and a precise one fixes the bug.\n4) Put something in the loop that can say no\nThe completion check from earlier is one case of a wider rule.\nWhatever decides if the work is good can’t be the same model that produced it. A model asked to grade its own output will usually pass it, so a loop with no outside check is just an agent agreeing with itself.\nSo you separate the maker from the checker. One agent writes the code, and a separate signal grades it, either something hard like a failing test or type error, or a second model running with different instructions.\nThat check is what lets you actually leave the loop alone, because now something other than the author decides when it’s right.\nWhat is user’s job now?\nPrompting steers the agent move by move.\nLoop engineering involves building the system that steers it, and then stepping back. The work becomes three artifacts:\nThe goal is written as a success criterion that the agent can check itself against.\nThe loop, with brakes, so it stops for the right reasons.\nThe verifier, so “done” is proven, not claimed.\nKarpathy said something along these lines too.\n“Don’t tell it what to do, give it success criteria and watch it go.”\nHis AutoResearch project runs exactly this, an agent that tweaks a training script, measures the result, keeps what works, and discards what doesn’t, with no human editing the code between rounds. He arranges it once and lets it run.\nYou don’t need an overnight autonomous agent on day one. Build up to it:\nStart with the basic loop and add a max-iteration cap, a timeout, and a cost ceiling immediately.\nDefine “done” as an automated check before you start, not a vibe afterward.\nProtect the context, compacting long runs, offloading big outputs, and isolating messy subtasks.\nAudit your tools, keeping them few and focused, making writes safe to repeat, and rewriting errors so an agent can act on them.\nPut a critic in the loop, and only go fully hands-off once you trust the thing that says no.\nThe takeaway\nLoop engineering isn’t a framework you install but rather a shift in where you spend effort.\nThe model is becoming a commodity, and the loop around it is where the engineering now lives.\nThe builders getting value this year stopped asking what to tell the agent and started asking what system would do the work without them.\n👉 Over to you: what’s the first brake you’d add to a loop you already run, a completion check, a budget cap, or a separate verifier?\n​\nTo dive deeper into harness engineering, we covered it in detail here →\nThanks for reading!\nPublished on Jun 27, 2026\nComments\nShare\nCopy link\nShare to X\nShare to Facebook\nShare to Linkedin\nCopied\nPrevious\nRLHF: Aligning Language Models with Human Feedback\nNext\nVerifiable Rewards and GRPO",
  "metadata": {
    "title": "Loop Engineering, Clearly Explained!",
    "description": "Used by top products, including Anthropic, Google, etc.",
    "keywords": null,
    "author": null,
    "og:site_name": "Daily Dose of Data Science",
    "og:type": "article",
    "og:title": "Loop Engineering, Clearly Explained!",
    "og:description": "Used by top products, including Anthropic, Google, etc.",
    "og:url": "https://www.dailydoseofds.com/p/loop-engineering-clearly-explained/",
    "og:image": "https://storage.ghost.io/c/3f/df/3fdf6ed2-17ac-4b12-a693-8078bd13e748/content/images/2026/06/18e5ea0c-2a3b-4f0d-9705-7cf90edf3256_1200x1105.png",
    "og:image:width": "1200",
    "og:image:height": "1105",
    "twitter:card": "summary_large_image",
    "twitter:title": "Loop Engineering, Clearly Explained!",
    "twitter:description": "Used by top products, including Anthropic, Google, etc.",
    "twitter:url": "https://www.dailydoseofds.com/p/loop-engineering-clearly-explained/",
    "twitter:image": "https://storage.ghost.io/c/3f/df/3fdf6ed2-17ac-4b12-a693-8078bd13e748/content/images/2026/06/18e5ea0c-2a3b-4f0d-9705-7cf90edf3256_1200x1105.png",
    "twitter:label1": "Written by",
    "twitter:data1": "Avi Chawla",
    "twitter:label2": "Filed under",
    "twitter:data2": "Agents, Loop engineering",
    "twitter:site": "@_avichawla",
    "article:published_time": "2026-06-26T23:11:03.000Z",
    "article:modified_time": "2026-06-26T23:11:03.000Z",
    "article:tag": "Loop engineering"
  },
  "paywall": {
    "listed_paid": false,
    "detected": false,
    "marker": "",
    "public_only": false,
    "policy": "付费/会员内容只保留页面公开可见部分，不绕过付费墙"
  },
  "crawl": {
    "success": true,
    "status_code": 200,
    "error": ""
  },
  "code_blocks": [
    {
      "file": "attachments/0a2366e40aa6/code/body-000.txt",
      "chars": 149,
      "language": "text"
    },
    {
      "file": "attachments/0a2366e40aa6/code/body-001.txt",
      "chars": 149,
      "language": "text"
    }
  ],
  "images": [
    {
      "status": "ok",
      "url": "https://storage.ghost.io/c/3f/df/3fdf6ed2-17ac-4b12-a693-8078bd13e748/content/images/size/w400/2026/03/avi-google.jpg",
      "bytes": 11344,
      "reused": true,
      "local_path": "attachments/0a2366e40aa6/images/000.jpg",
      "src": "https://storage.ghost.io/c/3f/df/3fdf6ed2-17ac-4b12-a693-8078bd13e748/content/images/size/w400/2026/03/avi-google.jpg",
      "alt": "Avi Chawla",
      "downloaded": true
    },
    {
      "status": "ok",
      "url": "https://substackcdn.com/image/fetch/$s_!IXQ_!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fe73db735-ddb7-4ca2-a88c-8594ca858a52_1341x644.jpeg",
      "bytes": 156764,
      "reused": true,
      "local_path": "attachments/0a2366e40aa6/images/001.jpeg",
      "src": "https://substackcdn.com/image/fetch/$s_!IXQ_!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fe73db735-ddb7-4ca2-a88c-8594ca858a52_1341x644.jpeg",
      "alt": "",
      "downloaded": true
    },
    {
      "status": "ok",
      "url": "https://substackcdn.com/image/fetch/$s_!__Zs!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F86e114e0-5516-4951-b39f-c6c6131ab27b_2752x1536.jpeg",
      "bytes": 262946,
      "reused": true,
      "local_path": "attachments/0a2366e40aa6/images/002.jpeg",
      "src": "https://substackcdn.com/image/fetch/$s_!__Zs!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F86e114e0-5516-4951-b39f-c6c6131ab27b_2752x1536.jpeg",
      "alt": "",
      "downloaded": true
    },
    {
      "status": "ok",
      "url": "https://substackcdn.com/image/fetch/$s_!aAgw!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F5dfbc748-ab50-4347-a37b-6397e810f731_1376x768.jpeg",
      "bytes": 146748,
      "reused": true,
      "local_path": "attachments/0a2366e40aa6/images/003.jpeg",
      "src": "https://substackcdn.com/image/fetch/$s_!aAgw!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F5dfbc748-ab50-4347-a37b-6397e810f731_1376x768.jpeg",
      "alt": "",
      "downloaded": true
    },
    {
      "status": "ok",
      "url": "https://substackcdn.com/image/fetch/$s_!SaiS!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fe116ef60-a7e0-4e18-9870-33053eeb8ee7_2335x2208.png",
      "bytes": 195710,
      "reused": true,
      "local_path": "attachments/0a2366e40aa6/images/004.png",
      "src": "https://substackcdn.com/image/fetch/$s_!SaiS!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fe116ef60-a7e0-4e18-9870-33053eeb8ee7_2335x2208.png",
      "alt": "",
      "downloaded": true
    },
    {
      "status": "ok",
      "url": "https://substackcdn.com/image/fetch/$s_!EraS!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F03decd6c-f31e-484d-bd90-bdedb100e8d7_1376x768.jpeg",
      "bytes": 106356,
      "reused": true,
      "local_path": "attachments/0a2366e40aa6/images/005.jpeg",
      "src": "https://substackcdn.com/image/fetch/$s_!EraS!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F03decd6c-f31e-484d-bd90-bdedb100e8d7_1376x768.jpeg",
      "alt": "",
      "downloaded": true
    },
    {
      "status": "ok",
      "url": "https://substackcdn.com/image/fetch/$s_!3yn8!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F4c2152e3-6ffc-4a9f-9cf6-f5ec8af0c7da_2752x1317.jpeg",
      "bytes": 236650,
      "reused": true,
      "local_path": "attachments/0a2366e40aa6/images/006.jpeg",
      "src": "https://substackcdn.com/image/fetch/$s_!3yn8!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F4c2152e3-6ffc-4a9f-9cf6-f5ec8af0c7da_2752x1317.jpeg",
      "alt": "",
      "downloaded": true
    },
    {
      "status": "ok",
      "url": "https://substackcdn.com/image/fetch/$s_!trPx!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F800ce59d-2bf5-453a-a3f4-73c2dfc34153_1376x768.jpeg",
      "bytes": 120883,
      "reused": true,
      "local_path": "attachments/0a2366e40aa6/images/007.jpeg",
      "src": "https://substackcdn.com/image/fetch/$s_!trPx!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F800ce59d-2bf5-453a-a3f4-73c2dfc34153_1376x768.jpeg",
      "alt": "",
      "downloaded": true
    },
    {
      "status": "ok",
      "url": "https://substackcdn.com/image/fetch/$s_!f3sp!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F18e5ea0c-2a3b-4f0d-9705-7cf90edf3256_1200x1105.png",
      "bytes": 288273,
      "reused": true,
      "local_path": "attachments/0a2366e40aa6/images/008.png",
      "src": "https://substackcdn.com/image/fetch/$s_!f3sp!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F18e5ea0c-2a3b-4f0d-9705-7cf90edf3256_1200x1105.png",
      "alt": "",
      "downloaded": true
    }
  ],
  "audio": [],
  "videos": [],
  "links": [
    {
      "href": "https://www.dailydoseofds.com/p/the-anatomy-of-an-agent-harness/",
      "label": "To dive deeper into harness engineering, we covered it in detail here →",
      "type": "a"
    },
    {
      "href": "https://x.com/intent/tweet?url=https%3A%2F%2Fwww.dailydoseofds.com%2Fp%2Floop-engineering-clearly-explained%2F&text=Loop%20Engineering%2C%20Clearly%20Explained!",
      "label": "Share to X",
      "type": "a"
    },
    {
      "href": "https://www.facebook.com/sharer/sharer.php?u=https%3A%2F%2Fwww.dailydoseofds.com%2Fp%2Floop-engineering-clearly-explained%2F",
      "label": "Share to Facebook",
      "type": "a"
    },
    {
      "href": "https://www.linkedin.com/sharing/share-offsite/?url=https%3A%2F%2Fwww.dailydoseofds.com%2Fp%2Floop-engineering-clearly-explained%2F",
      "label": "Share to Linkedin",
      "type": "a"
    },
    {
      "href": "https://www.dailydoseofds.com/rl-course-part-9/",
      "label": "Previous RLHF: Aligning Language Models with Human Feedback",
      "type": "a"
    },
    {
      "href": "https://www.dailydoseofds.com/rl-course-part-10/",
      "label": "Next Verifiable Rewards and GRPO",
      "type": "a"
    }
  ],
  "repair_version": 2,
  "repaired_at": "2026-10-14T10:47:57Z",
  "source_url": "https://www.dailydoseofds.com/p/loop-engineering-clearly-explained/",
  "papers": [],
  "actual_video_files": [],
  "body_chars": 7610,
  "expected_body_images": 9,
  "discovery_status": "ok",
  "delivery_status": "ok",
  "missing_reason": "",
  "raw_html": "attachments/0a2366e40aa6/pages/source.html",
  "local_html": "attachments/0a2366e40aa6/pages/article.html",
  "body_base_path": "attachments/0a2366e40aa6/pages/",
  "body_markdown_path": "attachments/0a2366e40aa6/pages/article.md"
}