{"title":"Điều phối Multi-Agent","slug":"multi-agent-coordination","category":"guides","summary":"Điều phối nhiều LLM agent sử dụng mind, tác vụ và chat Synapse chung.","audience":["human","llm"],"tags":["guide","multi-agent","coordination","patterns"],"difficulty":"advanced","updated":"2026-06-27","word_count":326,"read_minutes":2,"lang":"vi","translated":true,"requested_lang":"vi","content_markdown":"\n# Điều phối Multi-Agent\n\nKhi bạn có nhiều LLM agent làm việc trên các tác vụ liên quan, Synapse cung cấp\nlớp điều phối — bộ nhớ chung, gán tác vụ và chat bất đồng bộ.\n\n## Mẫu\n\n### Mẫu 1: Mind chung (Nguồn sự thật duy nhất)\n\nTất cả agent chia sẻ một Mind Key. Chúng đọc/ghi cùng kho bộ nhớ.\n\n```\n┌──────────┐  ┌──────────┐  ┌──────────┐\n│ Agent A  │  │ Agent B  │  │ Agent C  │\n└────┬─────┘  └────┬─────┘  └────┬─────┘\n     │             │             │\n     └─────────────┼─────────────┘\n                   ▼\n           ┌──────────────┐\n           │ Shared Mind  │\n           │  (one key)   │\n           └──────────────┘\n```\n\n**Trường hợp sử dụng:** Nhóm nhỏ agent làm việc trên một dự án.\n\n**Thiết lập:**\n\n```bash\n# All agents use the same Mind Key\nexport SYNAPSE_MIND_KEY=mk_shared_key...\n```\n\n**Điều phối qua tác vụ:**\n\n```python\n# Agent A creates a task\ncreate_task(\"Review PR #42\", priority=\"high\")\n\n# Agent B picks it up\ntasks = list_tasks(status=\"pending\")\nif tasks:\n    task = tasks[0]\n    update_task(task[\"id\"], status=\"in_progress\")\n    # ... do work ...\n    update_task(task[\"id\"], status=\"done\")\n```\n\n### Mẫu 2: Mind chuyên biệt (Ngữ cảnh tách biệt)\n\nMỗi agent có mind riêng. Chúng giao tiếp qua một mind \"điều phối\" chung.\n\n```\n┌──────────┐  ┌──────────┐  ┌──────────┐\n│ Coder    │  │ Reviewer │  │ Deployer │\n│ Agent    │  │ Agent    │  │ Agent    │\n└────┬─────┘  └────┬─────┘  └────┬─────┘\n     │             │             │\n     ▼             ▼             ▼\n┌─────────┐  ┌─────────┐  ┌─────────┐\n│ Mind C  │  │ Mind R  │  │ Mind D  │\n└─────────┘  └─────────┘  └─────────┘\n     │             │             │\n     └─────────────┼─────────────┘\n                   ▼\n           ┌──────────────────┐\n           │ Coordination Mind│\n           │ (shared)         │\n           └──────────────────┘\n```\n\n**Trường hợp sử dụng:** Agent với chuyên môn khác nhau (lập trình, review, triển khai).\n\n**Thiết lập:**\n\n```bash\n# Coder agent\nSYNAPSE_MIND_KEY=mk_coder... MCP_TRANSPORT=stdio npx synapse-mcp-api@latest\n\n# Reviewer agent\nSYNAPSE_MIND_KEY=mk_reviewer... MCP_TRANSPORT=stdio npx synapse-mcp-api@latest\n\n# Deployer agent\nSYNAPSE_MIND_KEY=mk_deployer... MCP_TRANSPORT=stdio npx synapse-mcp-api@latest\n```\n\n**Điều phối qua mind chung:**\n\n```python\n# Coder stores \"ready for review\"\nCOORDINATION_KEY = \"mk_coordination...\"\nrequests.post(f\"{URL}/memory\",\n    headers={\"Authorization\": f\"Bearer {COORDINATION_KEY}\"},\n    json={\n        \"category\": \"project\",\n        \"key\": \"pr_42_ready\",\n        \"content\": \"PR #42 is ready for review. Branch: feature/docs-system\",\n        \"tags\": [\"review\", \"pr-42\"],\n        \"priority\": \"high\"\n    })\n\n# Reviewer polls for review requests\nr = requests.get(f\"{URL}/memory/search?q=ready+for+review\",\n    headers={\"Authorization\": f\"Bearer {COORDINATION_KEY}\"})\n```\n\n### Mẫu 3: Hub-and-Spoke (Orchestrator)\n\nMột agent orchestrator trung tâm gán tác vụ cho agent worker.\n\n```\n        ┌──────────────┐\n        │ Orchestrator │\n        │    Agent     │\n        └──────┬───────┘\n               │\n    ┌──────────┼──────────┐\n    ▼          ▼          ▼\n┌──────┐  ┌──────┐  ┌──────┐\n│Worker│  │Worker│  │Worker│\n│  A   │  │  B   │  │  C   │\n└──────┘  └──────┘  └──────┘\n```\n\n**Trường hợp sử dụng:** Quy trình phức tạp với công việc song song.\n\n**Triển khai:**\n\n```python\n# Orchestrator\nclass Orchestrator:\n    def assign_task(self, worker_id, task_description):\n        # Store task in worker's mind (or shared coordination mind)\n        create_task(task_description, priority=\"high\")\n        # Notify worker via chat\n        reply(f\"@{worker_id}: New task — {task_description}\")\n    \n    def check_progress(self):\n        tasks = list_tasks(status=\"in_progress\")\n        for t in tasks:\n            print(f\"{t['title']}: {t['status']}\")\n\n# Workers poll for assigned tasks\nclass Worker:\n    def run(self):\n        while True:\n            tasks = list_tasks(status=\"pending\")\n            for t in tasks:\n                if assigned_to_me(t):\n                    update_task(t[\"id\"], status=\"in_progress\")\n                    result = do_work(t)\n                    update_task(t[\"id\"], status=\"done\")\n                    reply(f\"Completed: {t['title']}\")\n            time.sleep(60)\n```\n\n## Điều phối qua Chat\n\nAgent có thể giao tiếp qua hệ thống chat:\n\n```python\n# Agent A sends to Agent B\nreply(\"@agent-b: Can you review my PR?\")\n\n# Agent B polls and responds\nfor msg in poll_messages():\n    if \"@agent-b\" in msg[\"content\"]:\n        reply(f\"@agent-a: Sure, looking at it now.\")\n```\n\n> [!NOTE]\n> Tin nhắn chat được gắn vai trò. Đặt role=agent cho tin nhắn agent-đến-agent,\n> role=human cho human-đến-agent.\n\n## Điều phối qua Biến\n\nSử dụng biến cho điều phối nhẹ (lock, cờ):\n\n```python\n# Acquire a lock\ndef acquire_lock(name):\n    r = requests.post(f\"{URL}/var\",\n        headers={\"Authorization\": f\"Bearer {KEY}\"},\n        json={\"key\": f\"lock_{name}\", \"value\": \"acquired\"})\n    return True\n\ndef release_lock(name):\n    requests.delete(f\"{URL}/var/lock_{name}\",\n        headers={\"Authorization\": f\"Bearer {KEY}\"})\n\n# Use\nif acquire_lock(\"deploy\"):\n    try:\n        deploy_to_production()\n    finally:\n        release_lock(\"deploy\")\n```\n\n## Thực hành tốt nhất\n\n> [!TIP]\n> - **Sử dụng mind riêng cho mối quan tâm riêng** — không trộn bộ nhớ coder và reviewer\n> - **Gắn tag agent trong chat** — `@agent-name` cho địa chỉ rõ ràng\n> - **Sử dụng tác vụ cho gán công việc** — không chat (chat dành cho thảo luận)\n> - **Triển khai idempotency** — agent có thể thử lại thao tác thất bại\n> - **Ghi nhật ký mọi thứ** — lưu quyết định trong bộ nhớ cho khả năng kiểm toán\n\n## Bước tiếp theo\n\n- [LLM Agent cố định](/docs/guides/persistent-llm-agent)\n- [LLM Cookbook](/docs/llm-cookbook/session-start-pattern)\n- [Tự động hóa Webhook](/docs/guides/webhook-automation)\n","content_html":"<h1>Điều phối Multi-Agent</h1>\n<p>Khi bạn có nhiều LLM agent làm việc trên các tác vụ liên quan, Synapse cung cấp\nlớp điều phối — bộ nhớ chung, gán tác vụ và chat bất đồng bộ.</p>\n<h2>Mẫu</h2>\n<h3>Mẫu 1: Mind chung (Nguồn sự thật duy nhất)</h3>\n<p>Tất cả agent chia sẻ một Mind Key. Chúng đọc/ghi cùng kho bộ nhớ.</p>\n<pre><code class=\"hljs language-plaintext\">┌──────────┐  ┌──────────┐  ┌──────────┐\n│ Agent A  │  │ Agent B  │  │ Agent C  │\n└────┬─────┘  └────┬─────┘  └────┬─────┘\n     │             │             │\n     └─────────────┼─────────────┘\n                   ▼\n           ┌──────────────┐\n           │ Shared Mind  │\n           │  (one key)   │\n           └──────────────┘</code></pre><p><strong>Trường hợp sử dụng:</strong> Nhóm nhỏ agent làm việc trên một dự án.</p>\n<p><strong>Thiết lập:</strong></p>\n<pre><code class=\"hljs language-bash\"><span class=\"hljs-comment\"># All agents use the same Mind Key</span>\n<span class=\"hljs-built_in\">export</span> SYNAPSE_MIND_KEY=mk_shared_key...</code></pre><p><strong>Điều phối qua tác vụ:</strong></p>\n<pre><code class=\"hljs language-python\"><span class=\"hljs-comment\"># Agent A creates a task</span>\ncreate_task(<span class=\"hljs-string\">&quot;Review PR #42&quot;</span>, priority=<span class=\"hljs-string\">&quot;high&quot;</span>)\n\n<span class=\"hljs-comment\"># Agent B picks it up</span>\ntasks = list_tasks(status=<span class=\"hljs-string\">&quot;pending&quot;</span>)\n<span class=\"hljs-keyword\">if</span> tasks:\n    task = tasks[<span class=\"hljs-number\">0</span>]\n    update_task(task[<span class=\"hljs-string\">&quot;id&quot;</span>], status=<span class=\"hljs-string\">&quot;in_progress&quot;</span>)\n    <span class=\"hljs-comment\"># ... do work ...</span>\n    update_task(task[<span class=\"hljs-string\">&quot;id&quot;</span>], status=<span class=\"hljs-string\">&quot;done&quot;</span>)</code></pre><h3>Mẫu 2: Mind chuyên biệt (Ngữ cảnh tách biệt)</h3>\n<p>Mỗi agent có mind riêng. Chúng giao tiếp qua một mind &quot;điều phối&quot; chung.</p>\n<pre><code class=\"hljs language-plaintext\">┌──────────┐  ┌──────────┐  ┌──────────┐\n│ Coder    │  │ Reviewer │  │ Deployer │\n│ Agent    │  │ Agent    │  │ Agent    │\n└────┬─────┘  └────┬─────┘  └────┬─────┘\n     │             │             │\n     ▼             ▼             ▼\n┌─────────┐  ┌─────────┐  ┌─────────┐\n│ Mind C  │  │ Mind R  │  │ Mind D  │\n└─────────┘  └─────────┘  └─────────┘\n     │             │             │\n     └─────────────┼─────────────┘\n                   ▼\n           ┌──────────────────┐\n           │ Coordination Mind│\n           │ (shared)         │\n           └──────────────────┘</code></pre><p><strong>Trường hợp sử dụng:</strong> Agent với chuyên môn khác nhau (lập trình, review, triển khai).</p>\n<p><strong>Thiết lập:</strong></p>\n<pre><code class=\"hljs language-bash\"><span class=\"hljs-comment\"># Coder agent</span>\nSYNAPSE_MIND_KEY=mk_coder... MCP_TRANSPORT=stdio npx synapse-mcp-api@latest\n\n<span class=\"hljs-comment\"># Reviewer agent</span>\nSYNAPSE_MIND_KEY=mk_reviewer... MCP_TRANSPORT=stdio npx synapse-mcp-api@latest\n\n<span class=\"hljs-comment\"># Deployer agent</span>\nSYNAPSE_MIND_KEY=mk_deployer... MCP_TRANSPORT=stdio npx synapse-mcp-api@latest</code></pre><p><strong>Điều phối qua mind chung:</strong></p>\n<pre><code class=\"hljs language-python\"><span class=\"hljs-comment\"># Coder stores &quot;ready for review&quot;</span>\nCOORDINATION_KEY = <span class=\"hljs-string\">&quot;mk_coordination...&quot;</span>\nrequests.post(<span class=\"hljs-string\">f&quot;<span class=\"hljs-subst\">{URL}</span>/memory&quot;</span>,\n    headers={<span class=\"hljs-string\">&quot;Authorization&quot;</span>: <span class=\"hljs-string\">f&quot;Bearer <span class=\"hljs-subst\">{COORDINATION_KEY}</span>&quot;</span>},\n    json={\n        <span class=\"hljs-string\">&quot;category&quot;</span>: <span class=\"hljs-string\">&quot;project&quot;</span>,\n        <span class=\"hljs-string\">&quot;key&quot;</span>: <span class=\"hljs-string\">&quot;pr_42_ready&quot;</span>,\n        <span class=\"hljs-string\">&quot;content&quot;</span>: <span class=\"hljs-string\">&quot;PR #42 is ready for review. Branch: feature/docs-system&quot;</span>,\n        <span class=\"hljs-string\">&quot;tags&quot;</span>: [<span class=\"hljs-string\">&quot;review&quot;</span>, <span class=\"hljs-string\">&quot;pr-42&quot;</span>],\n        <span class=\"hljs-string\">&quot;priority&quot;</span>: <span class=\"hljs-string\">&quot;high&quot;</span>\n    })\n\n<span class=\"hljs-comment\"># Reviewer polls for review requests</span>\nr = requests.get(<span class=\"hljs-string\">f&quot;<span class=\"hljs-subst\">{URL}</span>/memory/search?q=ready+for+review&quot;</span>,\n    headers={<span class=\"hljs-string\">&quot;Authorization&quot;</span>: <span class=\"hljs-string\">f&quot;Bearer <span class=\"hljs-subst\">{COORDINATION_KEY}</span>&quot;</span>})</code></pre><h3>Mẫu 3: Hub-and-Spoke (Orchestrator)</h3>\n<p>Một agent orchestrator trung tâm gán tác vụ cho agent worker.</p>\n<pre><code class=\"hljs language-plaintext\">        ┌──────────────┐\n        │ Orchestrator │\n        │    Agent     │\n        └──────┬───────┘\n               │\n    ┌──────────┼──────────┐\n    ▼          ▼          ▼\n┌──────┐  ┌──────┐  ┌──────┐\n│Worker│  │Worker│  │Worker│\n│  A   │  │  B   │  │  C   │\n└──────┘  └──────┘  └──────┘</code></pre><p><strong>Trường hợp sử dụng:</strong> Quy trình phức tạp với công việc song song.</p>\n<p><strong>Triển khai:</strong></p>\n<pre><code class=\"hljs language-python\"><span class=\"hljs-comment\"># Orchestrator</span>\n<span class=\"hljs-keyword\">class</span> <span class=\"hljs-title class_\">Orchestrator</span>:\n    <span class=\"hljs-keyword\">def</span> <span class=\"hljs-title function_\">assign_task</span>(<span class=\"hljs-params\">self, worker_id, task_description</span>):\n        <span class=\"hljs-comment\"># Store task in worker&#x27;s mind (or shared coordination mind)</span>\n        create_task(task_description, priority=<span class=\"hljs-string\">&quot;high&quot;</span>)\n        <span class=\"hljs-comment\"># Notify worker via chat</span>\n        reply(<span class=\"hljs-string\">f&quot;@<span class=\"hljs-subst\">{worker_id}</span>: New task — <span class=\"hljs-subst\">{task_description}</span>&quot;</span>)\n    \n    <span class=\"hljs-keyword\">def</span> <span class=\"hljs-title function_\">check_progress</span>(<span class=\"hljs-params\">self</span>):\n        tasks = list_tasks(status=<span class=\"hljs-string\">&quot;in_progress&quot;</span>)\n        <span class=\"hljs-keyword\">for</span> t <span class=\"hljs-keyword\">in</span> tasks:\n            <span class=\"hljs-built_in\">print</span>(<span class=\"hljs-string\">f&quot;<span class=\"hljs-subst\">{t[<span class=\"hljs-string\">&#x27;title&#x27;</span>]}</span>: <span class=\"hljs-subst\">{t[<span class=\"hljs-string\">&#x27;status&#x27;</span>]}</span>&quot;</span>)\n\n<span class=\"hljs-comment\"># Workers poll for assigned tasks</span>\n<span class=\"hljs-keyword\">class</span> <span class=\"hljs-title class_\">Worker</span>:\n    <span class=\"hljs-keyword\">def</span> <span class=\"hljs-title function_\">run</span>(<span class=\"hljs-params\">self</span>):\n        <span class=\"hljs-keyword\">while</span> <span class=\"hljs-literal\">True</span>:\n            tasks = list_tasks(status=<span class=\"hljs-string\">&quot;pending&quot;</span>)\n            <span class=\"hljs-keyword\">for</span> t <span class=\"hljs-keyword\">in</span> tasks:\n                <span class=\"hljs-keyword\">if</span> assigned_to_me(t):\n                    update_task(t[<span class=\"hljs-string\">&quot;id&quot;</span>], status=<span class=\"hljs-string\">&quot;in_progress&quot;</span>)\n                    result = do_work(t)\n                    update_task(t[<span class=\"hljs-string\">&quot;id&quot;</span>], status=<span class=\"hljs-string\">&quot;done&quot;</span>)\n                    reply(<span class=\"hljs-string\">f&quot;Completed: <span class=\"hljs-subst\">{t[<span class=\"hljs-string\">&#x27;title&#x27;</span>]}</span>&quot;</span>)\n            time.sleep(<span class=\"hljs-number\">60</span>)</code></pre><h2>Điều phối qua Chat</h2>\n<p>Agent có thể giao tiếp qua hệ thống chat:</p>\n<pre><code class=\"hljs language-python\"><span class=\"hljs-comment\"># Agent A sends to Agent B</span>\nreply(<span class=\"hljs-string\">&quot;@agent-b: Can you review my PR?&quot;</span>)\n\n<span class=\"hljs-comment\"># Agent B polls and responds</span>\n<span class=\"hljs-keyword\">for</span> msg <span class=\"hljs-keyword\">in</span> poll_messages():\n    <span class=\"hljs-keyword\">if</span> <span class=\"hljs-string\">&quot;@agent-b&quot;</span> <span class=\"hljs-keyword\">in</span> msg[<span class=\"hljs-string\">&quot;content&quot;</span>]:\n        reply(<span class=\"hljs-string\">f&quot;@agent-a: Sure, looking at it now.&quot;</span>)</code></pre><div class=\"callout callout-note\">Tin nhắn chat được gắn vai trò. Đặt role=agent cho tin nhắn agent-đến-agent,\nrole=human cho human-đến-agent.</div><h2>Điều phối qua Biến</h2>\n<p>Sử dụng biến cho điều phối nhẹ (lock, cờ):</p>\n<pre><code class=\"hljs language-python\"><span class=\"hljs-comment\"># Acquire a lock</span>\n<span class=\"hljs-keyword\">def</span> <span class=\"hljs-title function_\">acquire_lock</span>(<span class=\"hljs-params\">name</span>):\n    r = requests.post(<span class=\"hljs-string\">f&quot;<span class=\"hljs-subst\">{URL}</span>/var&quot;</span>,\n        headers={<span class=\"hljs-string\">&quot;Authorization&quot;</span>: <span class=\"hljs-string\">f&quot;Bearer <span class=\"hljs-subst\">{KEY}</span>&quot;</span>},\n        json={<span class=\"hljs-string\">&quot;key&quot;</span>: <span class=\"hljs-string\">f&quot;lock_<span class=\"hljs-subst\">{name}</span>&quot;</span>, <span class=\"hljs-string\">&quot;value&quot;</span>: <span class=\"hljs-string\">&quot;acquired&quot;</span>})\n    <span class=\"hljs-keyword\">return</span> <span class=\"hljs-literal\">True</span>\n\n<span class=\"hljs-keyword\">def</span> <span class=\"hljs-title function_\">release_lock</span>(<span class=\"hljs-params\">name</span>):\n    requests.delete(<span class=\"hljs-string\">f&quot;<span class=\"hljs-subst\">{URL}</span>/var/lock_<span class=\"hljs-subst\">{name}</span>&quot;</span>,\n        headers={<span class=\"hljs-string\">&quot;Authorization&quot;</span>: <span class=\"hljs-string\">f&quot;Bearer <span class=\"hljs-subst\">{KEY}</span>&quot;</span>})\n\n<span class=\"hljs-comment\"># Use</span>\n<span class=\"hljs-keyword\">if</span> acquire_lock(<span class=\"hljs-string\">&quot;deploy&quot;</span>):\n    <span class=\"hljs-keyword\">try</span>:\n        deploy_to_production()\n    <span class=\"hljs-keyword\">finally</span>:\n        release_lock(<span class=\"hljs-string\">&quot;deploy&quot;</span>)</code></pre><h2>Thực hành tốt nhất</h2>\n<div class=\"callout callout-ok\"></div><h2>Bước tiếp theo</h2>\n<ul>\n<li><a href=\"/docs/guides/persistent-llm-agent\">LLM Agent cố định</a></li>\n<li><a href=\"/docs/llm-cookbook/session-start-pattern\">LLM Cookbook</a></li>\n<li><a href=\"/docs/guides/webhook-automation\">Tự động hóa Webhook</a></li>\n</ul>\n","urls":{"html":"/docs/guides/multi-agent-coordination","text":"/docs/guides/multi-agent-coordination?format=text","json":"/docs/guides/multi-agent-coordination?format=json","llm":"/docs/guides/multi-agent-coordination?format=llm"},"translations_available":["en","zh","hi","es","fr","ar","pt","ru","ja","de","it","ko","nl","pl","tr","sv","vi","th","id","uk"]}