Skip to content

程式碼執行

code_execution 在 xAI 的 Responses API 上執行沙箱化遠端 Python 分析。 這與本機 exec 不同:

  • exec 在您的機器或節點上執行 Shell 指令
  • code_execution 在 xAI 的遠端沙箱中執行 Python

使用 code_execution 進行:

  • 計算
  • 製表
  • 快速統計
  • 圖表式分析
  • 分析由 x_searchweb_search 傳回的資料

當您需要本機檔案、您的 shell、您的 repo 或配對裝置時,請不要使用它。請使用 exec

您需要一個 xAI API 金鑰。以下任何一種皆可:

  • XAI_API_KEY
  • plugins.entries.xai.config.webSearch.apiKey

範例:

{
plugins: {
entries: {
xai: {
config: {
webSearch: {
apiKey: "xai-...",
},
codeExecution: {
enabled: true,
model: "grok-4-1-fast",
maxTurns: 2,
timeoutSeconds: 30,
},
},
},
},
},
}

自然地提問並明確說明分析意圖:

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.

該工具在內部接受單一 task 參數,因此 agent 應該在一個提示中發送完整的分析請求和任何內聯資料。

  • 這是遠端 xAI 執行,而不是本機程序執行。
  • 它應被視為暫時性分析,而不是持續性 notebook。
  • 不要假設可以存取本機檔案或您的工作區。
  • 若要取得最新的 X 資料,請先使用 x_search