DEV Community

Nokka
Nokka

Posted on

Loop Engineering ภาคปฏิบัติ ตอนที่ 1: จาก Prompter สู่ Loop Designer — เปลี่ยนวิธีคิดก่อนเปลี่ยนเครื่องมือ

Loop Engineering ภาคปฏิบัติ ตอนที่ 1: จาก Prompter สู่ Loop Designer — เปลี่ยนวิธีคิดก่อนเปลี่ยนเครื่องมือ

โดย Nokka (นก-กา) | 2 กรกฎาคม 2026


TL;DR — สำหรับคนที่รีบ

Loop Engineering ไม่ใช่แค่ buzzword — มันคือการเปลี่ยนบทบาทของคุณจาก "คนนั่ง Prompt" เป็น "คนออกแบบระบบที่ Prompt แทน" แต่ก่อนจะไปถึงโค้ดหรือคำสั่ง สิ่งที่ต้องเปลี่ยนก่อนคือ วิธีคิด

บทความนี้จะอธิบาย 3 concepts หลักที่คนส่วนใหญ่เข้าใจผิด:

  1. GoalLoopRoutine — สามคำนี้ใช้แทนกันไม่ได้
  2. 5 Building Blocks ที่ทุก Loop ต้องมี
  3. State/Memory — ทำไม context window ถึงไม่พอ

คุณยังนั่ง Prompt Agent อยู่หรือเปล่า?

ลองถามตัวเองด้วยคำถาม 3 ข้อนี้:

  1. คุณนั่งรอ Agent ทำงานเสร็จ แล้วค่อยอ่าน diff ทุกรอบไหม?
  2. ถ้าคุณลุกไปกินข้าว Agent ก็หยุดทำงานใช่ไหม?
  3. คุณเป็นคนตัดสินใจเองว่างานเสร็จหรือยัง?

ถ้าตอบ "ใช่" มากกว่า 2 ข้อ — คุณคือ Loop คุณกำลังเป็น engine ที่ขับเคลื่อน Agent อยู่

ในมุมมองของผม นี่ไม่ใช่เรื่องผิดอะไร — ทุกคนเริ่มจากตรงนี้ แต่ปัญหาคือถ้าคุณยังเป็น Loop อยู่ คุณจะ scale การทำงานไม่ได้ เพราะเวลาคุณมีจำกัด


1. สามคำที่คนสับสนที่สุด: Goal, Loop, Routine

Addy Osmani (วิศวกรของ Google) เขียนไว้ในบทความ Loop Engineering ว่า [1] สิ่งแรกที่ต้องเข้าใจคือความแตกต่างระหว่าง 3 concepts ที่คนเอามาปนกันตลอด:

Goal — "ทำงานจนกว่าจะเสร็จ แล้วหยุด"

มิติ คำอธิบาย
Trigger คนสั่งครั้งเดียว
Stop เมื่องานสำเร็จ (verified)
เหมาะกับ งานที่มี exit criteria ชัดเจน
ตัวอย่าง "ทำให้ test/auth ทั้งหมดผ่าน และ lint clean"
Claude Code /goal
ความเสี่ยง ถ้า goal ไม่ชัด → Loop ไม่มีวันหยุด

Goal คือการบอก Agent ว่า "นี่คือเส้นชัย — วิ่งจนถึงแล้วค่อยหยุด" สิ่งสำคัญคือ คนตรวจสอบไม่ใช่คนเดียวกับคนทำงาน — Claude Code ใช้ Haiku เป็น judge แยกต่างหากเพื่อตัดสินว่างานเสร็จหรือยัง [2]

Loop — "ทำงานซ้ำตอนที่เราดูอยู่"

มิติ คำอธิบาย
Trigger คนสั่ง, ทำงานต่อเนื่อง
Stop คนกดหยุด หรือ ครบรอบ
เหมาะกับ งานที่ต้องลองผิดลองถูก, debugging
ตัวอย่าง Ralph loop: while ! verify; do agent "fix"; done
Claude Code /loop
ความเสี่ยง ต้องนั่งดู — ไม่ได้ช่วยเรื่องเวลา

Loop คือการให้ Agent ทำงานซ้ำๆ จนกว่าจะถูก แต่คุณยังต้องนั่งดูอยู่ — ต่างจาก Goal ตรงที่คุณเป็นคนตัดสินเองว่างานเสร็จหรือยัง

Routine — "ทำงานตอนเราหลับ"

มิติ คำอธิบาย
Trigger Time-based (cron, schedule)
Stop เมื่องานเสร็จ หรือ timeout
เหมาะกับ งานประจำ, CI/CD, maintenance
ตัวอย่าง "ทุกเช้า 9 โมง ตรวจ PR ที่ค้างแล้วแจ้งเตือน"
Claude Code /loop --schedule, Routines
ความเสี่ยง ถ้า verifier แย่ → โค้ดพังตอนคุณหลับ

Routine คือการตั้งเวลาให้ Loop ทำงานอัตโนมัติ — คุณไม่ต้องอยู่ด้วย นี่คือจุดที่ Loop Engineering มีพลังมากที่สุด

ทำไมต้องแยก?

Peter Steinberger (ผู้สร้าง OpenClaw) บอกว่า [3]: "Get the verb right and every pattern falls into place. Get it wrong and you either leave a hands-on loop running into an empty room, or you point a 'while I sleep' routine at a task that needed you watching."

แปล: ถ้าใช้ผิด verb — คุณจะเสียเงินเปล่า หรือได้โค้ดพังกลับมา


2. 5 Building Blocks ที่ทุก Loop ต้องมี

Addy Osmani อธิบายว่า Loop ที่ดีต้องมี 5 อย่าง [1]:

┌─────────────────────────────────────────────────────────┐
│                    LOOP ENGINE                          │
├──────────┬──────────┬──────────┬──────────┬─────────────┤
│          │          │          │          │             │
│  1.      │  2.      │  3.      │  4.      │  5.         │
│ Auto-    │ Work-    │ Skills   │ Plugins  │ Sub-agents  │
│ mations  │ trees    │          │ & Conns  │             │
│          │          │          │          │             │
│ ค้นหา+   │ แยก env  │ บันทึก   │ เชื่อม   │ แยกคนคิด   │
│ จัดการ   │ แต่ละ    │ ความรู้  │ ต่อ      │ กับคนตรวจ  │
│ งาน      │ งาน      │ โปรเจกต์ │ เครื่องมือ│            │
│          │          │          │          │             │
└──────────┴──────────┴──────────┴──────────┴─────────────┘
                        │
                        ▼
              ┌─────────────────┐
              │    STATE /      │
              │    MEMORY       │
              │  (Markdown /    │
              │   Linear /      │
              │   AGENTS.md)    │
              └─────────────────┘
Enter fullscreen mode Exit fullscreen mode

1. Automations — ตัวค้นหาและจัดการงาน

Automation คือส่วนที่ทำให้ Loop ทำงานโดยไม่ต้องมีคนสั่ง — มันไปดูว่ามีงานอะไรค้าง, มีอะไรพัง, มีอะไรต้องทำ แล้วเริ่มทำงานเอง

ตัวอย่าง: Matt Van Horn รัน Loop ที่เปิด PR ข้าม ~30 open-source repos อัตโนมัติทุกคืน [4] — โดยที่เขาไม่ต้องทำอะไรเลย (ดูรายละเอียดเพิ่มเติมในส่วนท้าย)

2. Worktrees — แยก Environment แต่ละงาน

ถ้า Loop ทำงานหลายอย่างพร้อมกัน — แต่ละงานต้องมี environment ของตัวเอง ไม่งั้นงานหนึ่งไปทับอีกงานหนึ่ง

Claude Code ใช้ git worktree หรือ --worktree flag เพื่อแยก environment แต่ละรอบ [5]

3. Skills — บันทึกความรู้ของโปรเจกต์

Skills คือไฟล์ .claude/skills/ หรือ SKILL.md ที่บอก Agent ว่าควรทำงานยังไง — เหมือนคู่มือการทำงานที่ Agent อ่านก่อนเริ่ม

ตัวอย่าง Skill สำหรับ verify งาน:

---
name: verify-frontend-change
---

# Verifying frontend changes
Never report a UI change as complete based on a successful edit alone:
1. Start the dev server and open the edited page
2. Interact with the change directly — click it, confirm state change
3. Check browser console: zero new errors
4. Run a performance trace and audit Core Web Vitals
Enter fullscreen mode Exit fullscreen mode

4. Plugins & Connectors — เชื่อมต่อเครื่องมือที่ใช้อยู่

Loop ต้องเข้าถึง tools ที่คุณใช้อยู่แล้ว — GitHub, Linear, Slack, Gmail — ผ่าน MCP (Model Context Protocol) [6]

5. Sub-agents — แยกคนคิดกับคนตรวจ

หัวใจของ Loop Engineering: คนทำงานไม่ควรตรวจสอบงานตัวเอง — ให้ใช้ sub-agent ที่แยกต่างหากมา verify แทน


3. State/Memory — ทำไม Context Window ถึงไม่พอ

ปัญหาที่ programmer ใหม่เจอตอนสร้าง Loop: Agent ลืมว่าทำอะไรไปแล้วบ้าง เพราะ context window มีจำกัด

วิธีแก้ของคนที่ใช้ Loop จริง: เก็บ state ไว้ในไฟล์ — ไม่ใช่ใน context

Addy Osmani อธิบายว่า [1]: "The model forgets everything between runs so the memory has to be on disk and not in the context. The agent forgets, the repo doesn't."

ตัวอย่าง State File (AGENTS.md):

# Project State — Updated: 2026-07-02 09:15 UTC

## Done
- [x] Refactored auth module (PR #142)
- [x] Added rate limiting to API endpoints

## In Progress
- [ ] Migrate database schema v3→v4 (agent: migrator-1)

## Blocked
- [ ] Deploy to staging (waiting for DevOps to approve firewall rule)

## Decisions
- Use PostgreSQL 16 for new features
- Keep Redis for session cache (no migration needed)
Enter fullscreen mode Exit fullscreen mode

Peter Steinberger ใช้ VISION.md ที่ระดับโปรเจกต์เพื่อบอกทิศทางให้ Agent [3] — และ AGENTS.md สำหรับ state ปัจจุบัน


4. ตัวอย่างจากคนที่ใช้ Loop จริง

Boris Cherny — ผู้สร้าง Claude Code

"I don't prompt Claude anymore. I have loops that are running. They're the ones that are prompting Claude and figuring out what to do. My job is to write loops." [7]

Boris ไม่ได้ prompt Claude ด้วยมืออีกต่อไป — เขาเขียน Loop ที่ prompt Claude แทน

Peter Steinberger — ผู้สร้าง OpenClaw

"More than 40% of my code is already written by loops. 90% of companies we work with don't use verification loops — that's their biggest mistake." [8]

40% ของโค้ดที่ Steinberger เขียน — จริงๆ แล้ว Loop เขียนให้

Matt Van Horn — Overnight PR Loop

Matt รัน Loop ที่เปิด PR ข้าม ~30 open-source repos ทุกคืน [4] — Loop ของเขาค้นหาปัญหา, แก้ไข, สร้าง PR, และรายงานผล โดยที่เขาไม่ต้องทำอะไรเลย


สรุป — สิ่งที่ต้องจำก่อนเริ่ม

ก่อนที่คุณจะแตะ /goal หรือ /loop ใน Claude Code — ให้ถามตัวเอง 3 ข้อนี้ก่อน:

  1. Verb ไหน? — Goal (จนกว่างานเสร็จ), Loop (ทำซ้ำตอนเราดู), หรือ Routine (ทำงานตอนเราหลับ)?
  2. 5 blocks ครบไหม? — Automations, Worktrees, Skills, Plugins, Sub-agents?
  3. State อยู่ที่ไหน? — ใน context window (หายเมื่อจบ) หรือในไฟล์ (อยู่ถาวร)?

ในมุมมองของผม การเปลี่ยน mindset นี่สำคัญกว่าการเรียนรู้คำสั่งใดๆ เพราะถ้าคุณยังคิดแบบ "prompter" อยู่ — คุณจะใช้ Loop สร้างปัญหาแทนที่จะแก้ปัญหา

ตอนหน้า: เราจะลงมือสร้าง Loop แรกด้วย Claude Code — ตั้งแต่ /goal จนถึง scheduled routine พร้อมตัวอย่าง PR review loop ที่ใช้งานได้จริง


เอกสารอ้างอิง

[1] Addy Osmani — Loop Engineering (O'Reilly Radar, June 7, 2026)
[2] Developers Digest — The Definitive Guide to Loop Engineering in Claude Code and Codex (June 20, 2026)
[3] explainx.ai — Loop Engineering: How to Design Coding Agent Loops That Run While You Sleep (June 9, 2026)
[4] Lenny's Newsletter — How to design AI agent loops: schedules, goals, and subagents in Claude Code and Codex (June 17, 2026)
[5] Claude by Anthropic — Getting started with loops (June 30, 2026)
[6] Claude Code Docs — Connect Claude Code to tools via MCP (2026)
[7] Boris Cherny — Claude Code & the Future of Engineering (Acquired Unplugged, June 2026)
[8] Lushbinary Team — Loop Engineering: Designing Systems That Prompt AI Agents (June 9, 2026)


บทความนี้เขียนโดย AI (DeepSeek V4 Flash) ผ่าน Hermes Agent ภายใต้การควบคุมและตรวจสอบคุณภาพโดยมนุษย์ — Nokka (นก-กา)

Top comments (0)