Tools
程式碼執行
code_execution 會在 xAI 的 Responses API 上執行沙盒化的遠端 Python 分析
(https://api.x.ai/v1/responses,與 x_search 使用相同端點)。它由內建的 xai 外掛在 tools 合約下註冊。
| 屬性 | 值 |
|---|---|
| 工具名稱 | code_execution |
| 提供者外掛 | xai(內建,enabledByDefault: true) |
| 驗證 | xAI 驗證設定檔、XAI_API_KEY,或 plugins.entries.xai.config.webSearch.apiKey |
| 預設模型 | grok-4-1-fast |
| 預設逾時 | 30 秒 |
預設 maxTurns |
未設定(xAI 會套用自己的內部限制) |
可用於計算、製表、快速統計與圖表式分析,包括由 x_search 或 web_search 傳回的資料。它無法存取本機檔案、你的 shell、你的 repo 或配對裝置,且不會在呼叫之間保留狀態,因此請將每次呼叫視為暫時性分析,而不是筆記本工作階段。若要取得最新的 X 資料,請先執行 x_search,再將結果傳入。
若要在本機執行,請改用 exec。
設定
Provide xAI credentials
OAuth 需要符合資格的 SuperGrok 或 X Premium 訂閱 (裝置碼驗證,因此可從沒有 localhost 回呼的遠端主機使用):
openclaw models auth login --provider xai --method oauth在全新安裝期間,也可以在 onboarding 中選擇相同選項:
openclaw onboard --install-daemon --auth-choice xai-oauth或使用 API 金鑰:
openclaw models auth login --provider xai --method api-keyexport XAI_API_KEY=xai-...或透過設定:
{ plugins: { entries: { xai: { config: { webSearch: { apiKey: "xai-...", }, }, }, }, },}這三種方式也都會供 x_search 和 Grok web_search 使用。
Enable and tune code_execution
只要能解析 xAI 憑證,就能使用 code_execution。將 plugins.entries.xai.config.codeExecution.enabled 設為 false 可停用它,或使用相同區塊覆寫模型、回合上限或逾時:
{ plugins: { entries: { xai: { config: { codeExecution: { enabled: true, model: "grok-4-1-fast", // override the default xAI code-execution model maxTurns: 2, // optional cap on internal tool turns timeoutSeconds: 30, // request timeout (default: 30) }, }, }, }, },}Restart the Gateway
openclaw gateway restart一旦 xAI 外掛以 enabled: true 重新註冊,code_execution 就會出現在代理程式的工具清單中。
如何使用
明確說明分析意圖;此工具接受單一 task 參數,因此請在同一個提示中送出完整請求與任何內嵌資料:
Use code_execution to calculate the 7-day moving average for these numbers: ...Use x_search to find posts mentioning OpenClaw this week, then use code_execution to count them by day.Use web_search to gather the latest AI benchmark numbers, then use code_execution to compare percent changes.錯誤
如果沒有驗證,工具會傳回結構化 JSON 錯誤(而不是拋出的例外),因此代理程式可以自行修正:
{ "error": "missing_xai_api_key", "message": "code_execution needs xAI credentials. Run `openclaw onboard --auth-choice xai-oauth` to sign in with Grok, run `openclaw onboard --auth-choice xai-api-key`, set `XAI_API_KEY` in the Gateway environment, or configure `plugins.entries.xai.config.webSearch.apiKey`.", "docs": "https://docs.openclaw.ai/tools/code-execution"}