跳转到内容

代码执行

code_execution 在 xAI 的 Responses API 上运行沙箱隔离的远程 Python 分析。 这与本地的 exec 不同:

  • exec 在您的计算机或节点上运行 Shell 命令
  • code_execution 在 xAI 的远程沙箱中运行 Python

使用 code_execution 进行:

  • 计算
  • 制表
  • 快速统计
  • 图表风格分析
  • 分析由 x_searchweb_search 返回的数据

当您需要本地文件、您的 Shell、您的仓库或配对设备时,请不要使用它。为此请使用 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 远程执行,而非本地进程执行。
  • 应将其视为临时分析,而非持久化笔记本。
  • 不要假设可以访问本地文件或您的工作区。
  • 对于最新的 X 数据,请先使用 x_search