openclaw-agent.mdx 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. ---
  2. title: "OpenClawAgent"
  3. description: "Use an OpenClaw worker inside an Agency Swarm agency."
  4. icon: "lobster"
  5. ---
  6. `OpenClawAgent` gives you a clean way to plug an OpenClaw worker into an Agency Swarm agency.
  7. Use it when Agency Swarm should stay in charge of delegation while OpenClaw handles one specialized branch.
  8. ## Start With One Import
  9. ```python
  10. from agency_swarm.agents import OpenClawAgent
  11. ```
  12. ## Use It In An Agency
  13. ```python
  14. from agency_swarm import Agency
  15. from agency_swarm.agents import Agent, OpenClawAgent
  16. coordinator = Agent(
  17. name="Coordinator",
  18. description="Routes work to the right specialist.",
  19. instructions="Delegate OpenClaw-specific tasks to OpenClawWorker.",
  20. model="gpt-5.4-mini",
  21. )
  22. openclaw_worker = OpenClawAgent(
  23. name="OpenClawWorker",
  24. description="Handles OpenClaw-native work.",
  25. instructions="Act as an OpenClaw specialist worker.",
  26. )
  27. agency = Agency(
  28. coordinator,
  29. communication_flows=[(coordinator, openclaw_worker)],
  30. )
  31. ```
  32. This example assumes `OpenClawAgent` can already reach a running OpenClaw endpoint.
  33. By default it calls the local `/openclaw/v1` proxy, so either mount that proxy in the same FastAPI app or point the agent at a custom `base_url`.
  34. ## Choose How To Reach OpenClaw
  35. <Tabs>
  36. <Tab title="Same FastAPI app">
  37. ```python
  38. from agency_swarm.agents import OpenClawAgent
  39. openclaw_worker = OpenClawAgent(
  40. name="OpenClawWorker",
  41. description="OpenClaw specialist",
  42. instructions="Handle the delegated OpenClaw work and return the result.",
  43. )
  44. ```
  45. This uses the local `/openclaw/v1` proxy in the same app.
  46. Before you use this path, mount that proxy in the same app with `attach_openclaw_to_fastapi(app)`.
  47. Install the FastAPI extras for that path: `pip install "agency-swarm[fastapi]"`.
  48. That mounted runtime also needs a working `openclaw` gateway command and a compatible Node installation.
  49. If `OPENCLAW_PROXY_PORT` is set, `OpenClawAgent` uses that port.
  50. Otherwise it falls back to `PORT`, then `8000`.
  51. </Tab>
  52. <Tab title="Custom host and port">
  53. ```python
  54. from agency_swarm.agents import OpenClawAgent
  55. openclaw_worker = OpenClawAgent(
  56. name="OpenClawWorker",
  57. description="OpenClaw specialist",
  58. instructions="Handle the delegated OpenClaw work and return the result.",
  59. host="127.0.0.1",
  60. port=18080,
  61. api_path="/openclaw/v1",
  62. )
  63. ```
  64. Use this when you run more than one OpenClaw-backed worker.
  65. </Tab>
  66. <Tab title="External OpenClaw server">
  67. ```python
  68. from agency_swarm.agents import OpenClawAgent
  69. openclaw_worker = OpenClawAgent(
  70. name="OpenClawWorker",
  71. description="OpenClaw specialist",
  72. instructions="Handle the delegated OpenClaw work and return the result.",
  73. base_url="http://127.0.0.1:18789/v1",
  74. api_key="your-openclaw-token",
  75. )
  76. ```
  77. Use this when you are talking to a raw OpenClaw gateway that serves `/v1` directly.
  78. If the remote server is another Agency Swarm app using this integration, use its `/openclaw/v1` path instead of `/v1`.
  79. When that remote server is a delegated worker, start it with `OPENCLAW_TOOL_MODE=worker`.
  80. If it is plain OpenClaw outside Agency Swarm, disable its competing delegation paths in that runtime instead of copying this env var blindly.
  81. Raw `/v1` gateways default to the upstream provider model, not `openclaw:main`.
  82. If that remote server expects a different model id, pass it with `model="openclaw:custom"` so this agent matches the target runtime.
  83. If you point to another Agency Swarm worker at `/openclaw/v1`, pass that worker's `api_key=` explicitly unless you are using the same in-process proxy.
  84. </Tab>
  85. </Tabs>
  86. ## Collaboration Pattern
  87. Use `OpenClawAgent` as a worker.
  88. Do not use it as the top-level delegator.
  89. Why:
  90. - Agency Swarm owns the recursive collaboration loop
  91. - OpenClaw has its own native messaging and session tools
  92. - keeping Agency Swarm on top preserves clean run hierarchy in the frontend
  93. <Info>
  94. When a normal Agency Swarm agent delegates to `OpenClawAgent`, the streamed events already carry the same `parent_run_id`, `agent_run_id`, `agent`, and `callerAgent` fields the current frontend uses for nested runs.
  95. </Info>
  96. ## Tools And Limits
  97. `OpenClawAgent` is not the same thing as giving OpenClaw your normal Agency Swarm Python tools.
  98. Today, the clean extension paths for OpenClaw itself are:
  99. - OpenClaw plugin tools via `api.registerTool(...)`
  100. - MCP servers exposed to OpenClaw
  101. - OpenClaw tool policy config such as allowlists and deny lists
  102. Use plain Agency Swarm tools on your normal orchestrator agent.
  103. Use OpenClaw plugins or MCP when you need to extend OpenClaw itself.
  104. <Accordion title="Host the OpenClaw proxy in the same FastAPI app">
  105. If your Agency Swarm app should also start the OpenClaw runtime, attach the proxy to the same FastAPI app:
  106. ```python
  107. from agency_swarm import Agency
  108. from agency_swarm.agents import Agent, OpenClawAgent
  109. from agency_swarm.integrations.fastapi import run_fastapi
  110. from agency_swarm.integrations.openclaw import attach_openclaw_to_fastapi
  111. def create_agency(load_threads_callback=None):
  112. coordinator = Agent(
  113. name="Coordinator",
  114. description="Routes work to the OpenClaw worker.",
  115. instructions="Delegate OpenClaw-specific work to OpenClawWorker.",
  116. model="gpt-5.4-mini",
  117. )
  118. openclaw_worker = OpenClawAgent(
  119. name="OpenClawWorker",
  120. description="OpenClaw specialist",
  121. instructions="Handle the delegated OpenClaw work and return the result.",
  122. )
  123. return Agency(
  124. coordinator,
  125. communication_flows=[(coordinator, openclaw_worker)],
  126. load_threads_callback=load_threads_callback,
  127. )
  128. app = run_fastapi(
  129. agencies={"support": create_agency},
  130. app_token_env="APP_TOKEN",
  131. return_app=True,
  132. )
  133. if app is None:
  134. raise RuntimeError("FastAPI app failed to start")
  135. attach_openclaw_to_fastapi(app)
  136. ```
  137. When that runtime is meant to be a delegated worker, start the app with:
  138. ```bash
  139. OPENCLAW_TOOL_MODE=worker python main.py
  140. ```
  141. That worker mode disables the OpenClaw messaging paths that compete with Agency Swarm delegation.
  142. </Accordion>
  143. <Accordion title="Current limitation">
  144. `OpenClawAgent` can receive delegated work, but it should not be the sender in Agency Swarm communication flows.
  145. If you need deeper tool customization inside OpenClaw, use OpenClaw plugins or MCP instead of trying to pass ordinary Agency Swarm Python tools through the wrapper.
  146. </Accordion>