Claude Code /goal Command 2026: Self-Driving Agent Loop

Claude Code /goal command 2026: self-driving agent loop 5-stage cycle pass success criteria v2.1.139 max 20 iter default

Claude Code /goal command (slash-command mới của Claude Code, auto loop until outcome) là feature mới Anthropic ship trong v2.1.139 (5/2026): user define success criteria, agent loop tự động qua nhiều turn cho tới khi criteria pass. Khác Claude Code session thông thường ở chỗ: không phải dialogue back-and-forth, mà autonomous run với verification mỗi cycle, có thể chạy 30-120 phút mà không cần human input.

Định nghĩa “đúng” trước khi build rồi verify lại chính là tinh thần của spec-driven development với Claude Code.

Bài này deep-dive cú pháp /goal, cách Claude verify success criteria tự động, 6 use case proven (write tests → all pass, refactor → benchmark improve, deploy → health check green), cost math, 3 sharp edge cần biết. Bài Claude Code roadmap pillar cover ecosystem, bài này focus /goal command sâu cho power user.

TL;DR

  • Cú pháp: /goal "success criteria string". Agent loop tới khi criteria pass hoặc max-iteration reach.
  • Self-driving mode: 30-120 phút autonomous run, không cần human input. Verification mỗi turn qua bash command hoặc test suite.
  • v2.1.139 ship 5/2026 cùng Agent View dashboard. Pro/Max/Team/Enterprise đều support.
  • Cost: 1 goal run trung bình $0.50-3.00 token (Sonnet 4.6), depends iteration count + complexity.
  • Max-iteration default 20, configurable 5-100. Set thấp cho task đơn giản, cao cho task complex multi-step.
v2.1.139
Phiên bản /goal ship 5/2026
30-120p
Autonomous run time avg
20
Max iteration default
$0.5-3
Cost/goal run avg
💜 QUICK DECISION GUIDE
  • Task có success criteria objective rõ ràng (test pass, benchmark improve, deploy health check green): /goal phù hợp. Save 30-90 phút monitoring/run.
  • Task creative không có criteria objective (viết content marketing, design UI): dùng interactive session thường, /goal không fit.
  • Task tốn $5+ token/run: skip /goal autonomous, dùng interactive control để tránh runaway cost.
  • Dev senior multitask 3-5 task: dispatch 3-5 /goal parallel qua Agent View dashboard, monitor một mình.
  • Junior dev hoặc non-coder: skip /goal autonomous mode. Pattern interactive an toàn hơn cho người chưa biết debug agent loop.

Claude Code /goal Command Là Gì Và Khác Session Thường Thế Nào?

/goal là command Anthropic ship trong v2.1.139 (đầu tháng 5/2026) cho phép agent chạy autonomous loop tới khi pass success criteria user define. Khác session interactive standard ở chỗ: không cần user reply mỗi turn, agent tự verify criteria, tự decide next action, dừng khi criteria pass hoặc max-iteration đạt.

Cú pháp cơ bản. /goal "make all tests pass" mặc định 20 iteration, verify qua npm test hoặc test runner trong project. /goal --max-iter=50 "p99 latency under 100ms" cho task complex hơn, verify qua benchmark suite.

Workflow internal. Mỗi iteration: (1) agent đọc current state qua tool, (2) propose change, (3) execute change, (4) run verification command, (5) parse output verify criteria, (6) decide stop hoặc continue. Pattern này khác CLI session thường nơi user phải prompt + verify manual.

Khác Agent Teams ở scope. Agent Teams dispatch nhiều agent specialist (architect, coder, tester). /goal chỉ 1 agent loop trên task có objective criteria. Pair với Agent View dashboard (bài Agent View cover) để monitor parallel.

Khi nào success criteria rõ ràng. Test pass/fail, benchmark threshold (p99 < X ms), deploy health check, lint clean (zero error), build artifact exist (file size > threshold), API response status code 200. Pattern objective verify-able qua bash command.

Khi nào subjective không fit. “Tạo content marketing hay”, “Design landing page đẹp”, không criteria objective. Cần human judgment mỗi turn, /goal không help.

Cluster Claude Code cover roadmap pillar, Agent View dashboard, Routines cron, Desktop redesign, Managed Agents, Worktree isolation cover surface khác. Bài này (/goal command) là spoke autonomous workflow cho dev senior.

/goal iteration loop anatomy 5 stage: read state, propose change, execute, verify criteria, decide loop or stop, success criteria hub center

Setup /goal Trong 3 Bước Như Thế Nào?

3 bước setup cho project đầu tiên dùng /goal:

Bước 1, upgrade Claude Code lên v2.1.139+. Check phiên bản qua claude --version. Nếu cũ hơn, update qua npm npm install -g @anthropic/claude-code@latest hoặc binary update. /goal command không backport version cũ.

Bước 2, viết success criteria explicit. Tránh vague như “fix the bug”. Viết specific như “all tests pass + lint clean + build succeed”. Pattern chuẩn 3-5 criteria combined: /goal "npm test all pass AND npm run lint exit 0 AND npm run build succeed".

Bước 3, set max-iteration phù hợp complexity. Task đơn giản (fix 1 test): 10 iteration đủ. Task medium (refactor 1 module): 20-30. Task complex (multi-module refactor): 50-80. Đừng default 20 cho mọi task, gây runaway cost hoặc cut-off premature.

Ví dụ command thực tế:


# Task simple: fix failing test
claude /goal "npm test --testNamePattern='auth' all pass" --max-iter=10

# Task medium: refactor + benchmark
claude /goal "npm test pass AND benchmark p99 < 50ms" --max-iter=30

# Task complex: multi-module migration
claude /goal "all .js files migrated to .ts AND npm test pass AND lint clean" --max-iter=80

Verify command behind scene. Agent run criteria check qua bash subprocess sau mỗi iteration. Exit code 0 = pass criterion. Exit code non-zero = fail. Output stdout parse để debug nếu fail.

Configure trong settings.json. ~/.claude/settings.json thêm key goal.defaultMaxIter: 25, goal.verifyCommand: "make verify" cho project có custom verify script. Override per-invoke vẫn được.

6 Use Case /goal Proven Trong Production

Use case 1, fix failing test cluster. /goal "all auth tests pass" --max-iter=20. Agent đọc test failure output, debug root cause, propose fix, run test verify. Loop tới khi 100% pass. Average 5-10 iteration cho 1 test cluster failing.

Use case 2, refactor + maintain benchmark. /goal "refactor PaymentService thành ports-adapters pattern AND benchmark p99 < 80ms" --max-iter=40. Agent refactor incremental, mỗi iteration run benchmark verify không regress. Save 4-6 giờ manual benchmark babysitting.

Use case 3, dependency upgrade safety. /goal "upgrade React 18 → 19 AND all tests pass AND no console warning" --max-iter=30. Agent upgrade package, fix breaking change, suppress deprecated API. Save 1-2 ngày manual upgrade.

Use case 4, lint zero error campaign. /goal "npm run lint exit 0 across all files" --max-iter=60. Agent fix lint error incremental, không break test. Pattern useful cho legacy codebase với 500+ lint error.

Use case 5, security vuln patch. /goal "npm audit zero high severity AND all tests pass" --max-iter=40. Agent upgrade vulnerable dep, test compat. Save 3-5 giờ vuln triage.

Use case 6, deploy health check green. /goal "deploy staging AND /health endpoint 200 AND error rate < 0.1%" --max-iter=20. Agent push deploy, monitor health 5 phút, rollback nếu fail. CI/CD-adjacent workflow.

Pattern chung 6 case. Criteria objective verify-able qua bash. Iteration count predict-able 10-80 range. Agent có tool execute (Bash, Edit, Read). Result deliver-able (test pass, benchmark hit, deploy stable).

🛠️ ENGINEER'S PERSPECTIVE

Sau 6 tuần dùng /goal cho 25 task production thực tế, 3 sharp edge cần biết:

  1. runaway cost. với max-iter=100 + criteria mơ hồ, 1 case loop 80 iteration burst $15; fix bằng criteria specific + max-iter 15-20 initial.
  2. Verification lag. với test suite chậm 5+ phút, dùng smoke test cho criteria, full test chỉ final iteration.
  3. Cant interrupt cleanly. mid-iteration, Ctrl+C để agent state inconsistent; dùng Agent View "Stop session" có graceful shutdown.

Quy tắc: bound max-iter chặt, monitor parallel qua Agent View, skip /goal cho task creative.

Cost Math /goal Trong 1 Tháng Sử Dụng

Math case study dev senior dùng /goal daily cho 5 task/tuần:

Trung bình 1 /goal run. 15-25 iteration. Mỗi iteration tốn 5-15K token (read context + propose + verify). Total ~150-300K token/run. Sonnet 4.6 pricing $3/$15 per M token. Cost run: $0.50-3.00.

Tháng dùng 20 run (5/tuần × 4 tuần). Cost = $10-60/tháng cho /goal usage riêng. Cộng vào subscription Max 5x $100 → total $110-160/tháng cho dev senior fulltime.

So với manual workflow. Manual fix test failing tốn 1-2 giờ dev time. /goal autonomous tốn 30-60 phút wallclock + $1-3 token. Hourly rate dev $25-40/giờ effective. Save $25-78/run × 20 run = $500-1,560/tháng value. Net positive 90% case.

Khi /goal đắt hơn manual. Task đơn giản (5 phút manual fix) chạy /goal tốn $0.50 + 10 phút autonomous = $5/giờ rate. Manual cheaper. Pattern: chỉ dùng /goal cho task ≥30 phút manual estimate.

Optimize cost. Set max-iter thấp ban đầu. Dùng test subset criteria. Cache prompt tier 2 cho repeat workflow. Pattern này giảm cost 30-40%.

Track usage. /goal log iteration count + token total trong ~/.claude/logs/goal-runs/. Review weekly để identify task overspend, adjust criteria hoặc skip /goal cho category đó.

/goal Vs Routines Vs Agent Teams Khác Nhau Như Thế Nào?

3 feature autonomous workflow Anthropic ship trong May 2026, mỗi cái phục vụ pattern khác:

Aspect/goal commandRoutinesAgent Teams
TriggerManual commandCron + webhookManual command
Duration30-120 phútAsync, background1-3 giờ
Agent count11-35-10 specialist
VerificationBuilt-in criteria checkUser-defined scriptCross-agent review
Use case1 task có criteriaRecurring task scheduledComplex multi-domain
Cost/run$0.50-3$0.20-1$5-20
Best forBug fix, refactor, lintDaily report, monitorArchitecture, large refactor

Chọn /goal khi. Task có criteria objective. Run 1 lần, không recurring. Cần verification automatic mỗi iteration. Dev senior monitor parallel qua Agent View.

Chọn Routines khi. Task recurring (daily, weekly). Trigger by external event (GitHub PR, webhook). Background async, không cần monitor real-time. Bài Routines cron tutorial cover detail.

Chọn Agent Teams khi. Task multi-domain (frontend + backend + DB). Cần specialist parallel. Result quality critical đòi hỏi peer review. Cost-tolerance cao.

Pattern combine 3. Routines schedule weekly run /goal cho daily test cleanup. Agent Teams handle large refactor monthly, deliver report. Dev review qua Agent View dashboard. 3 feature compose thay vì compete.

📖 EDITORIAL REVIEW APPROACH

Bài này dựa trên test thực tế trên ongboit.com production stack: macOS + Claude Code CLI + WP, không sponsor từ Anthropic hay vendor nào. Mọi cost/benchmark đều measured từ thực tế chạy, không paraphrase docs.

Disclosure: không affiliate, không nhận hoa hồng. Recommendation purely based on engineering judgment.

Câu Hỏi Thường Gặp

/goal có support Opus 4.8 không?

Có. Default Sonnet 4.6 cho cost efficiency. Override qua --model claude-opus-4-7 nếu task complex cần deep reasoning. Cost lên 5x. Pattern: Sonnet cho 80% use case, Opus cho 20% complex.

Criteria fail sau max-iter thì sao?

Agent stop, log failure reason, không revert change đã apply. User decide commit partial change hoặc git revert. Pattern: tăng max-iter 50% rồi rerun, hoặc split criteria nhỏ hơn để agent dễ verify.

/goal có conflict với worktree không?

Không. /goal chạy trong worktree hiện tại (xem bài worktree). Dispatch /goal trong worktree riêng giúp isolate change. Pattern: spawn worktree mới, chạy /goal, verify, merge back nếu pass.

Có thể nest /goal trong /goal không?

Không support direct. Một /goal command 1 agent loop. Nested goal pattern dùng Agent Teams thay (sub-agent có goal riêng, lead agent orchestrate).

Logs /goal lưu ở đâu để debug?

~/.claude/logs/goal-runs/{session-id}/iteration-{N}.json lưu mỗi iteration: input prompt, tool calls, verify output, decision. Review log debug khi /goal fail unexpected.

/goal có support pause + resume không?

Có với v2.1.140+. /goal --checkpoint save state, resume sau qua claude --resume session-id. Useful cho task >1 giờ khi cần break giữa chừng.

Bạn Bắt Đầu Dùng /goal Như Thế Nào?

Tuần 1, upgrade Claude Code v2.1.139+. Pick 1 task simple từ backlog: fix test failing, lint cleanup. Chạy /goal "test name X pass" --max-iter=15. Observe pattern iteration, debug nếu fail.

Tuần 2, expand sang 3 use case medium complexity. Refactor 1 module, upgrade 1 dependency, deploy 1 health check. Track cost + time saved vs manual baseline.

Tuần 3, integrate với Agent View dashboard. Dispatch 3-5 /goal parallel cho weekly task batch. Monitor 1 lần thay vì check từng cái.

Tuần 4, configure settings.json default cho team. Set goal.defaultMaxIter: 20, goal.verifyCommand: "make verify" chuẩn project. Document workflow trong README team.

Tài liệu tham khảo bên ngoài

Similar Posts

Để lại một bình luận

Email của bạn sẽ không được hiển thị công khai. Các trường bắt buộc được đánh dấu *