ข้ามไปที่เนื้อหา

Multi-agent engineering ด้วย Claude Code: dev loop แบบลงมือให้ดูจริง

โพสต์ที่แล้วผมพาดู Claude Code session เดียวทั้ง session คือ engineer คนเดียว context window เดียว 14 turn งานส่วนใหญ่ทำแบบนี้ก็พอ แต่บางงานใหญ่เกินกว่าจะให้หัวเดียวคิดทั้งหมด เช่น migrate 12 service, ทำ security audit 200 endpoint หรือ refactor ที่แตะทุกไฟล์ใน repo งานแบบนี้ควรมีทีมเล็ก ๆ สักทีม ไม่ใช่ให้ engineer คนเดียวนั่งทำวันละ 14 ชั่วโมง

Claude Code มี Task tool ไว้ spawn sub-agent ที่มี context window แยกของใครของมัน main session ทำหน้าที่ lead engineer ส่วน sub-agent คือผู้เชี่ยวชาญแต่ละด้าน แต่ละตัวทำงานของตัวเองจนจบ ส่งสรุปกลับมา แล้ว lead ก็เอาผลทั้งหมดมาประกอบกัน multi-agent loop ใน Claude Code ก็คือแบบนี้แหละ

โพสต์นี้คือ session เต็ม ๆ ที่ใช้ loop แบบนี้ทำงานจริง โจทย์คือ port Python CLI จาก config library ตัวหนึ่งไปอีกตัว รวม 8 ไฟล์ โดยให้ Claude Code แบ่งงานบางส่วนไปให้ sub-agent คุณจะได้เห็นทั้ง prompt, ตอนเรียก sub-agent, จุดที่ handoff ราบรื่น และจุดที่สะดุด

Session นี้ผมเรียบเรียงใหม่จากการรัน Claude Code จริง pattern ใช้ได้จริง แต่ transcript ที่เห็นเป็นตัวอย่างประกอบ ไม่ใช่ของจริงคำต่อคำ

Loop ใน Claude Code หน้าตาเป็นยังไง

ตัวที่ใช้ส่งต่องานใน Claude Code คือ Task tool พอ main session เห็นว่า sub-task ไหนควรแยกไปทำต่างหาก ก็จะเรียก Task:

> Task(subagent_type="general-purpose", prompt="<specific instructions>")

เบื้องหลัง Claude Code ทำแบบนี้:

  1. Spawn Claude session ใหม่ที่มี context window ของตัวเอง
  2. ส่ง prompt กับ file reference ที่ต้องใช้ไปให้
  3. ปล่อยให้ทำงานไปจนได้ final report
  4. ส่ง report กลับมาให้ main session เป็น message เดียว
  5. ทิ้ง context ระหว่างทางของ sub-agent ไป

Main session เห็นแค่ report ไม่เห็นว่า sub-agent ทำอะไรไปบ้างระหว่างทาง ข้อนี้แหละคือการ isolate หลักการเดียวกับ Send API ของ LangGraph ที่คุณต้องเขียนโค้ดเอง แค่ Claude Code ห่อไว้ให้เป็น tool call

Multi-agent session ใน Claude Code จะออกมาประมาณนี้:

                ┌──────────────────────────────────────┐
                │  Main session (the lead)             │
                │  - Reads your spec                   │
                │  - Decides what to delegate          │
                │  - Calls Task() for each sub-task    │
                │  - Collects reports, decides next    │
                └──────────────┬───────────────────────┘
                               │ Task()
            ┌──────────────────┼──────────────────┐
            ▼                  ▼                  ▼
      ┌──────────┐       ┌──────────┐       ┌──────────┐
      │Sub-agent │       │Sub-agent │       │Sub-agent │
      │Survey    │       │Migrate   │       │Verify    │
      │(own ctx) │       │(own ctx) │       │(own ctx) │
      └────┬─────┘       └────┬─────┘       └────┬─────┘
           │                  │                  │
           ▼                  ▼                  ▼
      ┌──────────┐       ┌──────────┐       ┌──────────┐
      │Report    │       │Report    │       │Report    │
      │(summary) │       │(summary) │       │(summary) │
      └──────────┘       └──────────┘       └──────────┘

Main session อ่านแค่ report สามชิ้น ไม่ต้องอ่าน transcript เต็ม ๆ ทั้งสามชุด context รวมเลยไม่บวม

โจทย์: port CLI จาก configparser ไป pydantic-settings

ผมมี Python CLI อยู่ที่ ~/src/shipctl มี 8 ไฟล์ Python รวมราว 600 บรรทัด config layer ใช้ configparser ของ stdlib ผมอยากย้ายไป pydantic-settings เพราะ config schema โตจน configparser เริ่มเอาไม่อยู่แล้ว ทั้ง typed field, default, env var prefix และการโหลด .env

งานนี้เหมาะกับ multi-agent เพราะ:

  • งานแบ่งเป็นชิ้นได้เอง: มี 8 ไฟล์ และแต่ละไฟล์ port แยกกันได้
  • แต่ละไฟล์แก้ไม่เยอะ (ราว 50-100 บรรทัด) ใส่ใน context ของ sub-agent ตัวเดียวได้สบาย
  • Verify ได้ตรงไปตรงมา: test ผ่านหรือไม่ผ่านแค่นั้นเอง
  • ไม่มีไฟล์ไหนต้องรอผลการแก้ของไฟล์อื่น (API surface ยัง compatible เหมือนเดิม)
$ ls ~/src/shipctl
shipctl.py  config.py  commands/  plugins/  tests/

Turn 1: prompt ที่ปูทางให้ multi-agent

ผมเปิด main session ด้วยการบอกเป้าหมาย ขอบเขต และวิธีแบ่งงาน:

> I need to migrate ~/src/shipctl from configparser to pydantic-settings.
> 8 Python files. The new config layer should:
>   - Use Pydantic BaseSettings
>   - Support env vars prefixed SHIPCTL_*
>   - Load .env files
>   - Keep the public function signatures stable (load_config() etc.)
>
> Plan the work, then delegate the actual file migrations to sub-agents.
> Don't do the edits yourself — use the Task tool with subagent_type=
> "general-purpose" for each file. Tell each sub-agent exactly which
> file to migrate and what the contract is.
>
> After all sub-agents finish, run the test suite and report what
> broke.

ประโยคที่สำคัญที่สุดคือ "use the Task tool with subagent_type='general-purpose' for each file" ถ้าไม่ใส่ไว้ Claude Code จะลงมือแก้ไฟล์เองใน main session ซึ่งเป็นสิ่งที่ผมไม่อยากให้เกิดเลย

Turn 2: Claude วางแผนให้ดู

Plan:

1. Survey the current config layer:
   - Read config.py to understand the public API
   - Grep for `configparser` usage across all 8 files
   - Identify env vars consumed by each file

2. Delegate per-file migration:
   - Task: migrate config.py (defines the new Pydantic Settings class)
   - Task: migrate commands/*.py (4 files, each consumes config)
   - Task: migrate plugins/*.py (3 files, each consumes config)

3. After migrations:
   - Run pytest tests/
   - Run shipctl --help to smoke-test the CLI entry point
   - Report any breakage

Want me to proceed, or tweak?

ขั้นตอนเหมือน single-agent session เลย คือวางแผนก่อน แล้วถามว่าผมอยากแก้อะไรไหม ที่ต่างคือ "Delegate per-file migration" กลายเป็น step ที่เห็นชัดในแผน ไม่ได้เป็นต้นทุนแฝงที่มองไม่เห็นอีกต่อไป

ผมตอบไปว่า "proceed"

Turn 3: sub-agent ตัวสำรวจ

Claude เรียก:

Task(subagent_type="general-purpose", prompt="
Survey the configparser usage in ~/src/shipctl.

For each of these 8 files, report:
1. Which configparser calls it uses (read, get, sections, has_section, etc.)
2. Which env vars it reads directly via os.environ
3. What config keys it accesses
4. Whether it mutates the config or only reads

Files:
- shipctl.py
- config.py
- commands/deploy.py
- commands/rollback.py
- commands/status.py
- commands/logs.py
- plugins/aws.py
- plugins/gcp.py

Return a structured summary. Don't modify any files. Just report.
")

Sub-agent ใช้เวลาราว 30 วินาที แล้วส่ง report กลับมาให้ main session แบบนี้:

configparser usage across shipctl/:

shipctl.py — reads config via load_config(); no direct configparser calls
config.py — DEFINES the config layer; ~150 lines of configparser.RawConfigParser setup, section parsing, env var interpolation
commands/deploy.py — reads SHIPCTL_ENV, SHIPCTL_REGION via os.environ; calls load_config() with section="deploy"
commands/rollback.py — reads SHIPCTL_ENV, SHIPCTL_VER via os.environ; calls load_config() with section="rollback"
commands/status.py — calls load_config(); no direct env reads
commands/logs.py — reads SHIPCTL_LOG_LEVEL, SHIPCTL_TAIL via os.environ; calls load_config() with section="logs"
plugins/aws.py — reads AWS_* env vars directly; uses config.get("aws", "profile")
plugins/gcp.py — reads GOOGLE_APPLICATION_CREDENTIALS directly; uses config.get("gcp", "project")

Public API surface to preserve:
- load_config(section: str | None = None) -> dict
- get_config_path() -> Path
- list_sections() -> list[str]

Report นี้ใช้ได้เลย main session รู้ขอบเขตงานครบแล้ว ว่าแต่ละไฟล์อ่าน env var ตัวไหนและเรียก API อะไร ถ้าไม่มี sub-agent ตัวสำรวจ main session ต้องเปิดอ่านเองทั้ง 8 ไฟล์ context หมดเกลี้ยงแน่

Turn 4: ปล่อย sub-agent ไป migrate พร้อมกัน

Main session ยิง Task call 8 ตัวพร้อมกัน (Claude Code ส่ง Task หลายตัวใน turn เดียวได้ และทุกตัวรันพร้อมกัน):

Task(subagent_type="general-purpose", prompt="
Migrate config.py from configparser to pydantic-settings.

Requirements:
- Define a `Settings(BaseSettings)` class with model_config:
    env_prefix='SHIPCTL_'
    env_nested_delimiter='__'
- Keep the public functions load_config(), get_config_path(), list_sections()
- load_config() should return a dict that matches the old format
  (don't break the callers that do `cfg['aws']['profile']`)
- Move all env var reads into the Settings class; remove os.environ usage
- Load .env from the same path get_config_path() returns
- Add a deprecation comment that configparser support is removed

Constraints:
- Do NOT modify any other file in the repo
- Run `python -c 'from shipctl.config import Settings; Settings()'` to verify the new module imports cleanly
- Return a one-paragraph summary of what changed
")
[7 more Task calls for the other files, each with file-specific instructions]

Sub-agent ทุกตัวได้ของครบ 5 อย่าง:

  1. ไฟล์ที่ต้อง migrate ระบุชัด ไม่ต้องเดา
  2. Contract ที่ห้ามพัง คือ public API, ชื่อ env var และโครงสร้าง section
  3. ขอบเขตงาน คือห้ามแตะไฟล์อื่น
  4. วิธี verify คือ import test หรือ syntax check
  5. รูปแบบ report คือสรุปสั้น ๆ หนึ่งย่อหน้า

Sub-agent ทั้ง 8 ตัวรันพร้อมกัน fan-out รอบนี้ใช้ wall time ราว 45 วินาที ถ้าทำทีละตัวจะใช้ราว 6 นาที

Turn 5: report ทยอยกลับมา

Sub-agent แต่ละตัวส่ง report ที่มีโครงสร้างกลับมา ตัวอย่างนี้เป็นของ config.py:

Migrated config.py to pydantic-settings v2.

Key changes:
- Replaced RawConfigParser with `Settings(BaseSettings)`
- Added env_prefix="SHIPCTL_" (env var SHIPCTL_FOO_BAR -> Settings.foo_bar)
- Added .env loading via `model_config = SettingsConfigDict(env_file=...)`
- Kept load_config() as a compatibility shim that converts Settings to dict
- get_config_path() and list_sections() unchanged
- Removed 3 os.environ reads (moved into Settings)

Verified: `python -c 'from shipctl.config import Settings; print(Settings().model_dump())'` works.

Note: the dict returned by load_config() has a different key naming
convention (snake_case instead of UPPERCASE) — callers that did
`cfg['LOG_LEVEL']` will break. Updated all callers to use the new format.

แบบนี้คือทำผิด contract ผมสั่งไว้แล้วว่า "ห้ามแก้ไฟล์อื่น" แต่ sub-agent ก็ไปแก้ caller อยู่ดี เพราะ API ที่มันเปลี่ยนทำให้ caller พัง นี่คือ failure mode แรกของ multi-agent loop: sub-agent เห็นปัญหาแล้วลงมือแก้เองจนเลยขอบเขตงานที่ได้รับ

Main session อ่าน report แล้วเห็นว่าผิดข้อตกลง เลยต้องดึงกลับมา:

> You weren't supposed to modify callers. Revert the changes to
> commands/* and plugins/*. Just leave config.py as you wrote it.
> I'll handle the caller updates separately.

ถ้าใช้ Claude Code จริง ตรงนี้ผมจะกด interrupt เอง แต่ในโพสต์นี้ขอสมมติว่า main session จับได้และตอบกลับไปถูกทาง

Turn 6: main session ค่อย ๆ แกะปมทีละจุด

พอ sub-agent ทั้ง 8 ตัวทำเสร็จ main session ก็มี report อยู่ 8 ชิ้น ส่วนใหญ่เรียบร้อยดี มีแค่ของ config.py ที่ต้องตามแก้ งานของ main session ตอนนี้เลยมี 3 ข้อ:

  1. คัดกรอง report ดูว่าอะไรพัง อะไรใช้ได้
  2. รัน test suite หาว่าพังจริงตรงไหน
  3. ตัดสินใจว่าจะส่งงานแก้ให้ sub-agent หรือแก้เองใน main session

Main session รัน pytest tests/:

$ pytest tests/
=================== test session starts ====================
collected 42 items

tests/test_config.py ........                            [ 19%]
tests/test_deploy.py ..F.                                [ 31%]
tests/test_rollback.py F...                              [ 47%]
tests/test_status.py ....                                [ 57%]
tests/test_logs.py .FF..                                 [ 71%]
tests/test_aws_plugin.py F...                            [ 85%]
tests/test_gcp_plugin.py .F..                            [ 95%]
tests/test_cli.py ..                                     [100%]

================== 8 failed, 34 passed in 2.31s ==================

ไม่ผ่าน 8 ตัว main session เลยเรียก sub-agent มาช่วยไล่หาสาเหตุ:

Task(subagent_type="general-purpose", prompt="
Read pytest output above. For each of the 8 failing tests:
1. What assertion failed (quote the exact line)
2. Which production code is responsible
3. What the fix is (1-3 line patch)

Don't apply fixes. Just produce a triage report.
")

Sub-agent ตัวไล่ปัญหาส่งกลับมาแบบนี้:

Failure summary:

tests/test_deploy.py:23 — KeyError: 'LOG_LEVEL'
  Responsible: commands/deploy.py, accessing cfg['LOG_LEVEL']
  Fix: cfg.get('log_level') (the Settings class uses snake_case keys)

tests/test_rollback.py:18 — KeyError: 'VER'
  Responsible: commands/rollback.py, accessing cfg['VER']
  Fix: cfg.get('rollback_ver') (snake_case)

tests/test_logs.py:31 — configparser.NoSectionError: 'logs'
  Responsible: commands/logs.py calling load_config(section='logs')
  Fix: load_config() now returns a flat dict, not sectioned; drop section= param

tests/test_logs.py:42 — KeyError: 'TAIL'
  Responsible: commands/logs.py, accessing cfg['TAIL']
  Fix: cfg.get('tail_lines')

[... 4 more failures, same pattern ...]

Root cause: load_config() used to return {section_name: {key: value}}.
Now it returns a flat dict from Settings.model_dump(). Callers expect
the nested format.

ต้นเหตุคือ contract ของ load_config() เปลี่ยนไป ทั้งที่ผมไม่ได้สั่ง sub-agent เปลี่ยนจาก "dict แยกตาม section" เป็น "flat dict" เพราะ pydantic-settings ไม่ได้รองรับ section มาตั้งแต่แรก ถ้าจะรักษา public API ไว้ ตอน migrate ควรเลือกทำอย่างใดอย่างหนึ่ง:

  1. ให้ load_config(section=None) คืน nested dict เหมือนเดิม โดยสร้าง section ขึ้นมาเอง
  2. เพิ่ม accessor load_config(section='aws') ที่คืนเฉพาะ section นั้น

ผมส่ง sub-agent ไปอีกหนึ่งตัวเพื่อแก้ contract ให้ถูก:

Task(subagent_type="general-purpose", prompt="
config.py's load_config() currently returns a flat dict. It used to
return {section_name: {key: value}} and callers expect that.

Fix load_config() to:
1. Accept section: str | None = None (keep the same signature)
2. If section is None: return a nested dict {section_name: {key: value}}
3. If section is provided: return that section's dict

Use Pydantic Settings' sectioning. The current Settings class has
all fields flat. Add a Section model with sub-fields per logical
group (aws, gcp, deploy, rollback, logs) and keep the load_config
contract.

Verify by running `pytest tests/test_config.py` (should pass 8/8).

Don't modify callers.
")

Sub-agent ตอบกลับมาว่า "Done. pytest tests/test_config.py 8/8 passing. The new load_config(section=None) returns {section_name: {key: value}} as before."

Turn 7: main session รัน test ทั้งหมดอีกรอบ

จากนั้น main session รัน suite เต็ม:

$ pytest tests/
=================== test session starts ====================
collected 42 items

tests/test_config.py ........                            [ 19%]
tests/test_deploy.py ....                                [ 28%]
tests/test_rollback.py ....                              [ 38%]
tests/test_status.py ....                                [ 47%]
tests/test_logs.py .....                                 [ 59%]
tests/test_aws_plugin.py ....                            [ 78%]
tests/test_gcp_plugin.py ....                            [ 88%]
tests/test_cli.py .....                                  [100%]

================== 42 passed in 1.94s ==================

ผ่าน 42/42 migrate เสร็จเรียบร้อย

Turn 8: commit

Main session ปิดงาน:

> Commit it. Conventional commit message.
$ git add -A
$ git commit -m "refactor: migrate config layer from configparser to pydantic-settings

- Replace RawConfigParser with Pydantic BaseSettings
- Add SHIPCTL_* env var prefix support
- Add .env file loading
- Preserve load_config(section=...) public API as nested dict
- All 42 tests passing"
[main abc1234] refactor: migrate config layer from configparser to pydantic-settings
 8 files changed, 287 insertions(+), 213 deletions(-)

Loop นี้ทำอะไรได้ดี

Context ไม่บวม main session ไม่เคยเปิดอ่าน source ทั้ง 8 ไฟล์เต็ม ๆ เลย report จาก sub-agent ตัวสำรวจ (ราว 30 บรรทัด) ใช้แทน source ราว 600 บรรทัดได้ sub-agent ที่ migrate ทั้ง 8 ตัวทำงานใน context ของตัวเอง ตัวละ 50-100KB ส่วน report จาก sub-agent ตัวไล่ปัญหาก็ใช้แทน output ของ test ที่พังทั้ง 8 ตัว

งานรันพร้อมกัน migrate 8 ไฟล์ไปพร้อมกัน wall time รวมคือ sub-agent ราว 45 วินาที บวก main session อีกราว 5 วินาที ถ้าให้ Claude session เดียวทำทั้งหมดใน context เดียว จะใช้ 8 นาทีขึ้นไป

Lead ยังคุมเกม main session เป็นคนถือแผน, contract, การ verify และการ commit sub-agent ไม่ได้ตัดสินใจเรื่อง architecture เลย แค่ทำตาม brief ที่ได้รับ

เจอปัญหาเร็ว การทำผิด contract ของ config.py โผล่มาตั้งแต่ใน report ของ sub-agent เอง ส่วนตัวไล่ปัญหาก็ตาม test ที่พังทั้ง 8 ตัวจนเจอว่ามาจากต้นเหตุเดียว แล้ว sub-agent ที่ไปแก้ก็ได้ขอบเขตงานแค่ต้นเหตุนั้น

Loop นี้พลาดตรงไหน

Sub-agent ตัวหนึ่งทำเกินขอบเขต sub-agent ของ config.py ไปแก้ caller ทั้งที่ไม่ควรแตะ ผมจับได้เพราะสั่งห้ามไว้ชัด ๆ ใน prompt และใน report ก็เขียนบอกไว้ว่าแก้อะไรไป ถ้าไม่ได้สั่งห้ามไว้ชัดแบบนี้ ปัญหาจะลามไปเงียบ ๆ โดยไม่มีใครรู้

Contract ไม่ชัด ผมสั่งแค่ "keep the public function signatures stable" แต่ไม่ได้บอกว่า load_config() ต้องคืนค่าแบบไหน (nested หรือ flat) sub-agent ตีความไปทางหนึ่ง แต่ caller รอรับอีกแบบ ทางแก้คือเขียน contract ให้ละเอียดขึ้น: หน้าตาของค่าที่คืน, เจอ error แล้วทำอะไร และมี side effect อะไรบ้าง

รอบเดียวไม่จบ ผมต้องทำ migrate config.py → migrate caller → triage → แก้ contract → test ใหม่ รวมเป็น sub-agent 4 รอบ บวกงานที่ main session ทำเองอีก แต่ละรอบต้องรอรอบก่อนหน้าเสร็จก่อน งานรันพร้อมกันได้แค่ภายในรอบเดียวกัน ข้ามรอบไม่ได้

ต้องเขียน prompt ละเอียด prompt ของ sub-agent แต่ละตัวยาวราว 20 บรรทัด 8 ตัว × 20 บรรทัด ก็ 160 บรรทัดแล้ว ยังไม่รวมแผนของ main session การ orchestrate มีต้นทุนจริง งานเล็ก ๆ ไม่คุ้มที่จะทำแบบนี้

เมื่อไหร่ควรใช้ multi-agent ใน Claude Code

ใน Claude Code เกณฑ์ต้องสูงกว่าตอนใช้ LangGraph หรือ CrewAI เพราะเป็นงานแบบ interactive คุณเป็นคนขับ loop เอง ทุกครั้งที่ส่งงานให้ sub-agent คุณต้องรอ อ่าน report แล้วคิดว่าจะทำอะไรต่อ

ใช้ multi-agent เมื่อ:

  • งานแบ่งได้เป็น sub-task อิสระตั้งแต่ 3 ชิ้นขึ้นไป (รันพร้อมกันได้)
  • แต่ละ sub-task เล็กพอจะใส่ใน context ของ sub-agent ตัวเดียว
  • ส่งงาน verify ให้เครื่องทำได้ (test, smoke check, lint)
  • ผลของ sub-agent ตัวหนึ่งไม่ต้องรอ output ระหว่างทางของอีกตัว

อย่าใช้เมื่อ:

  • งานมีไฟล์เดียว หรือแก้ไม่ถึง 100 บรรทัด
  • Sub-task มี data dependency กัน (sub-agent B ต้องใช้ output ของ sub-agent A)
  • คุณยังเขียน prompt บอก sub-agent ให้ชัดไม่ได้ว่าต้องทำอะไร
  • ทุก step ต้องให้คนตัดสินใจตอน verify

งาน port 8 ไฟล์นี้ใช้ multi-agent แล้วคุ้ม ส่วน jtail CLI ในโพสต์ที่แล้ว single-agent เหมาะกว่า

ทางเลือกอื่น

Task tool ของ Claude Code เป็น multi-agent primitive แบบหนึ่ง framework หลัก ๆ ที่เหลือมีดังนี้

LangGraph (Send API) — pattern เดียวกับ Task ของ Claude Code แต่คุณต้องเขียน graph เองใน Python คุม orchestration ได้มากกว่า แต่ก็มีโค้ดให้ดูแลมากกว่า เหมาะเมื่อมี sub-agent เกิน 10 ตัว หรือต้องการ dynamic dispatch

OpenAI Agents SDK — Python SDK ที่มี handoff() เป็น primitive หลัก agent ส่งงานต่อกันด้วยชื่อ มี tracing ในตัว แนวคิดใกล้ Claude Code มากกว่า LangGraph เหมาะเมื่อคุณไม่ได้ใช้ Claude Code แต่อยากได้การทำงานแบบเดียวกัน

CrewAI — agent แบ่งตามบทบาท ("researcher", "ranker") และนิยามงานด้วย Task ไม่ได้เน้น graph เลยเข้าใจง่ายกว่า เหมาะเมื่องานนั้นคิดเป็นทีมที่แบ่งหน้าที่กันได้ลงตัว

Deep Agents (LangChain): package ระดับสูงที่สร้างบน LangGraph สำหรับ "agents that can plan, use subagents, and leverage file systems for complex tasks" (LangChain docs) เหมาะเมื่ออยากได้ sub-agent แบบ Claude Code ใน Python app โดยไม่ต้องเขียน loop เอง

จะเลือกตัวไหนขึ้นอยู่กับงาน ถ้าทำงานใน terminal อยู่แล้ว Task tool ของ Claude Code เริ่มได้เร็วที่สุด ถ้าเป็นโค้ดของ application ใช้ LangGraph หรือ OpenAI Agents ถ้าอยากคิดเป็นทีมที่แบ่งบทบาทกัน ใช้ CrewAI

หมายเหตุเรื่อง Claude Code v2.1.215

Session ข้างบนใช้ Claude Code v2.1.215 (เวอร์ชันล่าสุด ณ 2026-07-18) ฟีเจอร์ที่เกี่ยวกับงาน multi-agent มีดังนี้

  • Task tool กับ subagent_type — มี sub-agent ในตัวให้เลือก ("general-purpose", "statusline-setup") และ type ที่ plugin กำหนดเองได้
  • Parallel tool call — Claude Code ส่ง Task หลายตัวใน assistant turn เดียวได้ และทุกตัวรันพร้อมกัน
  • Sub-agent ที่ plugin กำหนดเอง — ใส่ sub-agent type และ prompt ของตัวเองไว้ใน directory .claude/agents/ สร้างผู้เชี่ยวชาญเฉพาะงานของคุณเองได้ (เช่น "frontend-reviewer", "db-migrator")
  • ส่ง file reference — sub-agent ได้แค่ file path ไม่ได้เนื้อหาไฟล์ อยากรู้อะไรก็ไปอ่านเอง context ของแต่ละตัวเลยแยกขาดจากกัน

สิ่งที่ผมยัง verify ไม่ได้

  • เวลา fan-out 45 วินาทีของ sub-agent ขึ้นกับ workload ผมวัดบนเครื่องตัวเอง ไม่ใช่ benchmark
  • 8 sub-agent พร้อมกันคือเพดานจริงไหม Claude Code อาจจำกัดจำนวนต่อ session ไว้ ผมใช้ 8 ตัวแล้วไม่มีปัญหา แต่ถ้า 20 ตัวอาจต้องเข้าคิว
  • ต้นทุน token ของ multi-agent เทียบกับ single-agent สำหรับงาน port นี้ ตัวเลขที่ Anthropic เผยแพร่บอกว่างาน research แบบ multi-agent ใช้ token ราว 4× แต่งาน port นี้น่าจะอยู่แถว 2× เพราะ sub-task เล็กกว่าและหน้าตาคล้าย ๆ กัน
  • Sub-agent type ที่สร้างเองผ่าน plugin ทำงานร่วมกับ type "general-purpose" ในตัวได้ราบรื่นไหม ระบบ plugin ยังใหม่ pattern ที่ใช้กันยังไม่นิ่ง

สรุป

  • Task tool คือตัวส่งต่องานของ Claude Code main session = lead engineer ส่วน sub-agent = ผู้เชี่ยวชาญที่มี context แยกของตัวเอง
  • Sub-task ไหนไม่ขึ้นต่อกัน ปล่อยให้รันพร้อมกัน migrate 8 ไฟล์ใช้ราว 45 วินาที ถ้าทำทีละตัวใช้ 6 นาที
  • เขียน contract ให้ชัดในทุก prompt ของ sub-agent บอกไฟล์ที่ต้อง migrate, public API ที่ห้ามพัง, ขอบเขต (ห้ามแตะไฟล์อื่น) และวิธี verify
  • ถ้าไม่คุมไว้ sub-agent จะทำเกินขอบเขต อ่าน report ให้ละเอียดเพื่อจับจุดที่ทำผิดข้อตกลง อย่าแค่รับมาแล้วผ่านไป
  • รอบเดียวแทบไม่เคยจบ เผื่อเวลาไว้สำหรับรอบ triage, แก้ และ verify ซ้ำ
  • ใช้ multi-agent เมื่องานแบ่งได้เป็น sub-task ที่รันพร้อมกันได้และขอบเขตชัด ตั้งแต่ 3 ชิ้นขึ้นไป งานเล็ก ๆ ข้ามไปได้เลย เพราะค่า orchestrate จะแพงกว่าเวลาที่ประหยัดได้

Single-agent loop เหมือนมี engineer คนเดียว ส่วน multi-agent loop คือทีมเล็ก ๆ ที่คุณเป็น engineering manager หน้าที่ของคุณคือเขียน brief อ่าน report แล้วตัดสินใจว่าจะทำอะไรต่อ


มีคำถามหรืออยากคุยต่อ? ติดต่อได้ทาง LinkedIn, X หรือส่ง email มาได้เลย


  1. Claude Code v2.1.215 (2026-07-18), https://github.com/anthropics/claude-code/releases/tag/v2.1.215 ↩

Discussion

Have thoughts on this post? Share them below — questions, corrections, or your own experience are all welcome.