"หยุดเขียน Prompt เอง — ออกแบบ Loop ที่สั่งงาน AI Agent แทนคุณ"
Loop Engineering คือการออกแบบระบบควบคุมที่ค้นหางาน มอบหมายให้ Agent ตรวจสอบผลลัพธ์ และจดจำสถานะ — แทนที่จะนั่งพิมพ์ Prompt ทีละครั้ง
"I don't prompt Claude anymore. I have loops running that prompt Claude and figuring out what to do. My job is to write loops."
— Boris Cherny, Head of Claude Code @ Anthropic
จุดที่ให้พลังเปลี่ยนไปแล้ว — จาก "เขียน Prompt ให้ดี" เป็น "ออกแบบระบบที่ควบคุม Agent ตลอดเวลา"
ทุกครั้งที่เปิด Session ใหม่ AI เริ่มจากศูนย์ ไม่รู้บริบทโปรเจกต์ ไม่รู้ว่าเมื่อวานทำอะไร คุณเสียเวลา "สอน AI" ซ้ำแล้วซ้ำเล่า — เรียกว่า Intent Debt (หนี้เจตนา)
Loop คือระบบอัตโนมัติที่ทำงานตามตาราง ค้นหาปัญหา → มอบหมายให้ Sub-agent → ตรวจสอบผล → จดจำสถานะ คุณเป็น วิศวกรที่ออกแบบ Loop ไม่ใช่คนพิมพ์ Prompt
Loop หนึ่งวงประกอบด้วยส่วนต่างๆ เหล่านี้ นำไปประกอบเหมือน LEGO — เริ่มจากน้อยแล้วเพิ่มทีละอย่างเมื่อพิสูจน์ค่าแล้ว
หัวใจของ Loop — กำหนดจังหวะที่ Loop ทำงาน: ทุก 5 นาที ทุกวัน เมื่อ CI พัง หรือจนกว่าจะสำเร็จ
/loop 1d (Grok) · cron (Claude Code) · hermes cron create "0 8 * * *"เมื่อ Agent แก้โค้ด จะทำใน Git Worktree แยกต่างหาก ไม่กระทบโค้ดหลัก ป้องกัน Merge Conflict ระหว่างการพยายามแก้ไขหลายครั้ง
git worktree add fix-123 → Agent ทำงานในโฟลเดอร์แยก → ลบเมื่อเสร็จเก็บ conventions, คำสั่ง build/test, "ห้ามทำแบบนี้เพราะเคยเกิดบั๊ก X" ทำให้ Loop ไม่ต้องเริ่มต้นจากศูนย์ทุกครั้ง — ลด Intent Debt
SKILL.md ที่บอก "ใช้ pnpm ไม่ใช่ npm" หรือ "ห้ามแก้ไฟล์ใน auth/"MCP (Model Context Protocol) ช่วยให้ Loop เชื่อมต่อเครื่องมือจริง: อ่าน/เขียน GitHub PRs, Linear tickets, Slack, Database
สำคัญที่สุด! Agent ที่เขียนโค้ดห้ามตรวจสอบงานตัวเอง — ใช้คนละ Agent (อาจเป็นโมเดลที่แข็งแกร่งกว่า) เป็น Verifier
เสาหลักเชื่อมทุกอย่าง — STATE.md เก็บ: กำลังทำอะไร · ทำอะไรไปแล้ว · รออะไุจากมนุษย์ ทำให้ Loop จดจำข้าม Session
"กำลังแก้ #1241, รอ human review PR #1250, พบ flaky test"แต่ละ Pattern แก้ปัญหาเฉพาะ — คลิกเพื่อดูรายละเอียดเต็ม
เป้าหมาย: เริ่มวันด้วยภาพรวมที่จัดลำดับความสำคัญแล้ว โดยไม่ต้องไปตรวจ CI, Issues, PRs เอง
เช้า หรือทุก 2 ชม. ระหว่าง Sprint
อ่าน CI failures (24h), Issues, Commits, STATE.md
เพิ่ม High Priority ลง STATE.md พร้อม action
Worktree → Implementer → Verifier
ลบรายการที่ resolved/merged
# Grok
/loop 1d Run loop-triage. Update STATE.md. No auto-fix in week one.
# Claude Code
/loop 1d Run $loop-triage. Read STATE.md. Merge findings into
High Priority and Watch List. Do not edit code.
# Hermes
hermes cron create "0 8 * * *" --name "Daily triage" \
--deliver local --skill loop-triage --workdir "$PWD" \
"Run loop-triage. Read STATE.md first."
# Loop State — Project X
Last run: 2026-07-13 08:15 UTC
## High Priority
- [ ] #1241 — flaky test in auth flow (CI red)
Loop action: Opened worktree. Fix proposed. Waiting for review.
## Watch List
- PR #1238 open 4 days with no activity.
## Recent Noise (ignored)
- Dependabot PRs (separate automation)
| สถานการณ์ | Token/รอบ | หมายเหตุ |
|---|---|---|
| ไม่มีอะไรทำ | ~5k | State ว่าง |
| Triage เต็มรูปแบบ (L1) | ~50k | สแกน CI+Issues+Commits |
| แก้ไขด้วย (L2) | ~200k | Worktree+Implementer+Verifier |
เป้าหมาย: ลดเวลาที่มนุษย์ต้องเฝ้า PR ผ่าน Review, CI, Rebase และ Merge
ทั้งหมดที่ทีมสนใจ
CI แดง? → แก้. มี Comments? → เสนอ Patch. พร้อม? → แจ้งมนุษย์
อัปเดต PR comment + State
เสนอปิดหรือส่งต่อ
# Grok
/loop 5m /pr-babysit check
# ความคิดเห็นต้องชัดเจน:
# "🤖 Loop Engineering — PR Babysitter"
| สถานการณ์ | Token/รอบ | หมายเหตุ |
|---|---|---|
| ไม่มีอะไรทำ | ~3k | เป้าหมาย — exit early |
| Triage pass | ~80k | สแกน PR + CI status |
| Fix attempt (L2) | ~250k | Worktree+minimal-fix+verifier |
เป้าหมาย: ตอบสนอง CI พังบน main อย่างรวดเร็ว — วินิจฉัย เสนอ Fix ขั้นต่ำ และส่งต่อเมื่อแก้ไม่ได้
บน main, release/*, PRs
Flake? Regression? Infra? Config?
เคยเห็นแล้วและเป็น intermittent
Worktree → Implementer → Verifier (รัน tests ใน worktree)
ครั้งที่ 3 ที่ล้มเหลว → หยุด ส่งให้มนุษย์
# Grok
/loop 15m Check CI on main and open PRs. For new failures:
classify, and if actionable draft minimal fix in worktree with
verifier. Update ci-sweeper-state.md. Escalate after 3 attempts.
# Claude Code
/goal All tests on main pass and lint is clean
# ตรวจสอบก่อน retry:
npx @cobusgreyling/loop-context --check --ledger loop-ledger.json
# Exit 0 = ทำต่อ · Exit 2 = ส่งให้มนุษย์
เป้าหมาย: ค้นหา dependencies เก่า/มีช่องโหว่ อัปเดตเฉพาะที่ปลอดภัย (patch) ส่งต่อสิ่งที่เสี่ยง (major/CVE) ให้มนุษย์
npm audit, Dependabot, OSV
Safe (patch ไม่มี CVE) / Cautious (minor) / High-risk (major หรือ CVE สูง)
Worktree → version bump → verifier (tests+build)
Flag พร้อม changelog diff + CVE link
# Grok
/loop 6h Run dependency-triage. For safe patches create
minimal PRs in worktrees. Never auto-merge majors or CVEs above medium.
เป้าหมาย: สแกน PRs/commits ที่ merge แล้ว ร่าง release notes คุณภาพสูงแบ่งหมวดหมู่ — มนุษย์ review ก่อน publish
tag ล่าสุด หรือวันที่จาก state
ดึง titles, labels, linked issues
Features, Fixes, Breaking, Security, Docs
ตรวจ accuracy, completeness, tone
เสนอ draft ให้มนุษย์ approve เท่านั้น
เป้าหมาย: หลัง merge ไป main กวาดหางานตามมา — deprecations, TODOs, tech debt, stale flags, doc gaps
24h หรือตั้งแต่รอบล่าสุด
"remove after", feature flags, broken links
Worktree → minimal-fix → verifier
ส่งให้มนุษย์พร้อม context
# Grok
/loop 1d Scan merges to main in the last 24h. Identify cleanup
items. For small low-risk items: worktree + minimal fix + verifier.
Update post-merge-state.md. Create tickets for larger items.
# ทำงาน off-peak เท่านั้น (กลางคืน)
# จำกัด auto-PRs: สูงสุด 2 PRs/วัน
เป้าหมาย: ค้นพบ หาซ้ำ จัดลำดับ และติด label ให้ Issues ใหม่ — ให้ทีมมี top-of-queue ที่สะอาดเสมอ
ตั้งแต่รอบล่าสุด
สรุป intent, หาซ้ำ, ดึงสัญญาณ (age, reactions, linked PRs)
P0 (security/prod) / P1 / P2 / P3 / needs-info / duplicate
ห้าม auto-label หรือ close ใน L1
ไม่รู้จะเริ่มจากไหน? ใช้แผนผังนี้เลือก Pattern ที่เหมาะกับสถานการณ์
กฎสัปดาห์แรก: report only — ห้าม auto-fix ห้าม auto-merge อ่านทุกอย่างที่ Loop เขียนก่อน
ไม่รู้เลือกอะไร? เริ่มจาก Daily Triage
npx @cobusgreyling/loop-init . --pattern daily-triage --tool grok
# เปลี่ยน --tool ได้: grok | claude | codex | opencode
สร้าง: STATE.md, LOOP.md, loop-budget.md, loop-run-log.md + พิมพ์ Loop Ready score
npx @cobusgreyling/loop-cost --pattern daily-triage --level L1 --cadence 1dnpx @cobusgreyling/loop-audit . --suggest
# ได้ badge สำหรับ README:
npx @cobusgreyling/loop-audit . --badge# Grok
/loop 1d Run loop-triage. Update STATE.md. No auto-fix.
# Claude Code
/loop 1d Run $loop-triage. Read STATE.md. Do not edit code.
# Hermes
hermes cron create "0 8 * * *" --name "Daily triage" \
--deliver local --skill loop-triage \
--workdir "$PWD" "Run loop-triage"เปิด STATE.md แก้ไขสิ่งที่ผิด — คุณยังเป็นวิศวกร Commit เพื่อให้ audit เห็น activity
# Scaffold
npx @cobusgreyling/loop-init . --pattern daily-triage --tool grok
# Cost check
npx @cobusgreyling/loop-cost --pattern daily-triage --level L1
# Audit + suggestions
npx @cobusgreyling/loop-audit . --suggest
# Badge สำหรับ README
npx @cobusgreyling/loop-audit . --badge
# ตรวจ STATE.md ↔ LOOP.md drift
npx @cobusgreyling/loop-sync .
# L2: worktree สำหรับ fix attempts
npx @cobusgreyling/loop-worktree create --run-id fix-1 --pattern ci-sweeper
npx @cobusgreyling/loop-worktree mark --run-id fix-1 --status rejected
npx @cobusgreyling/loop-worktree cleanup --older-than 24h
ห้ามข้ามขั้น! เริ่มจาก L1 เสมอ แล้วค่อย progress
เอกสารความตั้งใจเท่านั้น ยังไม่รัน
Checklist: §1Triage → State, ไม่มี auto-action (เริ่มที่นี่!)
Checklist: §1-3, §5Auto-fix ขนาดเล็ก พร้อม Verifier + Worktree
Checklist: §1-7รันโดยไม่มีมนุษย์เฝ้า — ต้องมี denylist, budget, metrics, human gates
Checklist: ทั้งหมดLoop ขยายพลังการตัดสินใจ — ทั้งที่ดีและที่แย่ Guardrails เหล่านี้คือมาตรฐานขั้นต่ำ
Loop ห้ามแก้ไฟล์เหล่านี้โดยไม่ได้รับอนุญาตจากมนุษย์:
.env, .env.*
**/secrets/**
**/credentials/**
**/*_key*, **/*_secret*
.terraform/**
k8s/production/**
**/migrations/**
auth/**
payments/**
billing/**
ค่าเริ่มต้น: ห้าม Auto-merge
| อนุญาต (ถ้ามี allowlist) | ห้ามเด็ดขาด |
|---|---|
| Typo ใน comment/docs | Behavior changes |
| Lint auto-fix (test files) | Dependency version bumps |
| Import ordering | Lockfile changes |
| Config ใน docs/ paths | Denylist paths ใดๆ |
| Connector | Read | Write |
|---|---|---|
| GitHub | issues, PRs, checks | comment, label (not merge) |
| Linear | team issues | comment, status (not delete) |
| Slack | channel history | post ไป #loop-escalations เท่านั้น |
| Database | — | ห้าม production write |
ความผิดพลาดในการออกแบบที่ต้องหลีกเลี่ยงก่อนเปิด unattended loops
ทำไมพัง: Confirmation bias, test อ่อนผ่านหมด
แก้: Verifier คนละ Agent/โมเดล ค่าเริ่มต้น = REJECT
ทำไมพัง: Infinite fix loops, burn tokens
แก้: Hard cap 3 ครั้ง → escalate
ทำไมพัง: Loop ไม่ parse ได้, คนไม่อ่าน
แก้: Structured markdown + one-line items
ทำไมพัง: กระทำจากสัญญาณผิด
แก้: L1 report-only สัปดาห์แรกเสมอ
ทำไมพัง: State rot, ghost items
แก้: ไฟล์ State แยก per pattern
ทำไมพัง: Blast radius ใหญ่เกินไป
แก้: L1 read-only ก่อน
ทำไมพัง: Alert fatigue, budget overrun
แก้: Document pause/kill ใน LOOP.md
ทำไมพัง: ปิดปัญหา infra ซะ
แก้: Classify → quarantine → escalate
ทำไมพัง: Security bugs ผ่าน verifier อ่อน
แก้: Explicit path allowlist
ทำไมพัง: Debug ไม่ได้ "ทำไมทำแบบนั้นวันอังคาร?"
แก้: Append to loop-run-log.md ทุกรอบ
เรียนรู้จากความล้มเหลวจริง — และวิธีป้องกัน
| Failure | Severity | สาเหตุ | การป้องกัน |
|---|---|---|---|
| Infinite Fix Loop | S2 | Verifier อ่อน / root cause ผิด | Hard cap 3 → escalate |
| State Rot | S1→S2 | ไม่ prune, state เก่า | Prune closed/merged ทุกรอบ |
| Verifier Theater | S2 | Verifier ไม่รัน tests | Verifier ต้องรัน test/lint |
| Notification Fatigue | S1→S2 | แจ้งทุกรอบ | แจ้งเฉพาะตอนต้องมี decision |
| Token Burn | S1 | Cadence เร็ว + sub-agent หนัก | Early exit + budget cap |
| Over-Reach | S2→S3 | Scope กว้างเกิน | Denylist + smallest diff |
| Comprehension Debt | S2 | คนเลิกอ่าน output | Weekly digest review |
| Cognitive Surrender | S2 | "Loop จัดการเอง" | Human gates ทุก pattern |
| Parallel Collision | S2 | 2 agents แก้ไฟล์เดียวกัน | Worktree isolation |
| Escalation Failure | S2 | ไม่แจ้งมนุษย์ | Connector ping on escalation |
npm packages ทั้งหมด — ไม่ต้อง clone repo รันผ่าน npx ได้เลย
Scaffold skills, state, budget files + พิมพ์ Loop Ready score
npx @cobusgreyling/loop-init . --pattern daily-triage --tool grokLoop Readiness Score (0-100) + suggestions + badge
npx @cobusgreyling/loop-audit . --suggestประเมิน Token spend ตาม cadence
npx @cobusgreyling/loop-cost --pattern ci-sweeper --cadence 15mตรวจ drift ระหว่าง STATE.md ↔ LOOP.md
npx @cobusgreyling/loop-sync .Circuit breaker + stateful memory สำหรับ L2+
npx @cobusgreyling/loop-context --check --ledger run.jsonManage isolated git worktrees สำหรับ fix attempts
npx @cobusgreyling/loop-worktree create --run-id fix1 --pattern ci-sweeperMCP runtime lookup — patterns, skills, state on demand
LOOP_PROJECT_ROOT=. npx @cobusgreyling/loop-mcp-serverLoop Engineering ไม่ผูกกับ tool ใด — สิ่งสำคัญคือ ความสามารถ ไม่ใช่ชื่อสินค้า
| Primitive | Grok | Claude Code | Codex | Hermes |
|---|---|---|---|---|
| Scheduling | /loop 1d | /loop, cron | Automations tab | hermes cron create |
| Worktrees | isolation: "worktree" | --worktree | Built-in per thread | Standard git worktree + --workdir |
| Skills | .grok/skills/ | .claude/skills/ | $skill-name | ~/.hermes/skills/ |
| Connectors | CallMcpTool | MCP servers | Connectors (MCP) | hermes mcp add |
| Sub-agents | Task tool | .claude/agents/ | TOML agents | delegate_task |
| State | STATE.md | AGENTS.md | Markdown/Linear | STATE.md + hermes memory |
| Run-until-done | /goal | /goal | /goal | Chained cron via context_from |
SKILL.md) 2) นิยาม state schema 3) Document verification split 4) Map scheduling ไปยัง TUI/editor/Action ปัจจุบันรัน Loop คืองาน operations — ต้องดูเรื่อง cost, logging, และเมื่อไร่ะ pause/kill
| Loop | Cadence | Runs/วัน | Token/วัน (โดยประมาณ) |
|---|---|---|---|
| Daily Triage (report) | 1d | 1 | ~50k |
| CI Sweeper (light) | 15m | 96 | ~5M (หลีกเลี่ยง!) |
| PR Babysitter | 5m | 288 | สูง — ใช้ early exit |
Best Practice: Triage pass ถูก — spawn sub-agents เฉพาะเมื่อ state บอกว่า actionable ว่าง → exit ใน <5k tokens
{
"run_id": "2026-07-13T08:15:00Z",
"pattern": "daily-triage",
"duration_s": 45,
"items_found": 4,
"actions_taken": 1,
"escalations": 0,
"tokens_estimate": 52000,
"outcome": "success"
}
รันหลาย Loop ปกติ — แต่ต้องมีขอบเขตชัดเจน ไม่งั้น Loop ตีกัน
main แดง blocks ทุกอย่าง
PRs time-sensitive
Pause ถ้า CI แดง
Off-peak, ด่วนน้อยสุด
Report only L1
แทนที่ตัวเองในฐานะ "คนพิมพ์ Prompt" — ออกแบบระบบที่ค้นหางาน มอบหมาย ตรวจสอบ และจดจำสถานะ
สภาพแวดล้อมที่ Agent ตัวเดียว ทำงาน: tools, context, permissions. Harness = sandbox; Loop = ที่ schedule + orchestrate
ระบบที่ สร้าง software: pipelines, agents, checks, handoffs. Loop Engineering = วิธีดูแล factory floor
ทุก session AI เริ่มจากศูนย์ — intent ที่หายไปถูกแทนด้วย "การเดาที่มั่นใจ" Skills คือวิธีจ่ายหนี้นี้
ช่องว่างระหว่าง "โค้ดที่มี" กับ "สิ่งที่คุณเข้าใจจริงๆ" Loop เร็ว = ship โค้ดที่คุณไม่ได้เขียนเยอะขึ้น
กับดักของการปล่อย Loop รันไป โดยคุณเลิกมีความคิดเห็น — ออกแบบ Loop ด้วย "วิจารณญาณ" คือทางรักษา
ต้นทุนมนุษย์ในการประสานงาน agents ขนาน: review bandwidth, merge conflicts, context switching
Pattern: agents คนละบทบาท (explore, implement, verify) — Implementer ห้ามตรวจงานตัวเอง