Appearance
xAI 远程沙箱 Python 分析工具,适合需要临时计算、统计、图表分析但无法访问本地环境或 Shell 的场景。配置前需准备 xAI 凭证(OAuth/设备码/API Key),启用后重启网关即可在工作区生效;首次运行缺少凭证时会返回 missing_xai_api_key 错误引导修复。
OpenClaw code_execution 配置:远程沙箱 Python 分析工具
code_execution 在 xAI 的 Responses API 远程沙箱中运行 Python 代码。它由内建 xai 插件注册(enabledByDefault: true),使用与 x_search 相同的 https://api.x.ai/v1/responses 端点。
| 属性 | 值 |
|---|---|
| 工具名称 | code_execution |
| 提供方插件 | xai(内建,enabledByDefault: true) |
| 认证方式 | xAI 认证配置文件、XAI_API_KEY 环境变量、或 plugins.entries.xai.config.webSearch.apiKey |
| 默认模型 | grok-4-1-fast |
| 默认超时 | 30 秒 |
默认 maxTurns | 未设置(xAI 内部有自身限制) |
与本地 exec 本质不同:
exec在本机或配对节点上执行 Shell 命令。code_execution在 xAI 远程沙箱中运行 Python。
适用场景:
- 数值计算
- 数据汇总
- 快速统计
- 图表型分析
- 对
x_search或web_search返回的数据做进一步分析
不要用于需要本地文件、本地 Shell、代码仓库或配对设备的情况——请用 exec。
配置步骤
提供 xAI 凭证
可使用 SuperGrok 或 X Premium 订阅通过 Grok OAuth 登录,也可使用设备码流程或 API Key。OAuth 同时支持 `code_execution` 和 `x_search`;`XAI_API_KEY` 或插件 web-search 配置也可用于 Grok `web_search`。
```bash
# 浏览器 OAuth 登录
openclaw models auth login --provider xai --method oauth
# 设备码登录(适合无浏览器环境)
openclaw models auth login --provider xai --device-code
```
首次安装时,可在引导命令中选择认证方式:
```bash
# 使用设备码认证
openclaw onboard --install-daemon --auth-choice xai-device-code
```
或使用 API Key:
```bash
openclaw models auth login --provider xai --method api-key
export XAI_API_KEY=xai-...
```
或写入配置:
```json5
{
plugins: {
entries: {
xai: {
config: {
webSearch: {
apiKey: "xai-...",
},
},
},
},
},
}
```
启用并调整 code_execution
当 xAI 凭证可用时,`code_execution` 即生效。若要禁用,设置 `plugins.entries.xai.config.codeExecution.enabled` 为 `false`,也可在同区域调整模型和超时。
```json5
{
plugins: {
entries: {
xai: {
config: {
codeExecution: {
enabled: true,
model: "grok-4-1-fast", // 覆盖默认的 code-execution 模型
maxTurns: 2, // 可选:限制内部工具循环次数
timeoutSeconds: 30, // 请求超时(默认 30 秒)
},
},
},
},
},
}
```
重启网关
```bash
openclaw gateway restart
```
xAI 插件重新注册后,`code_execution` 会出现在智能体的工具列表中(需 `enabled: true`)。
使用方法
自然语言描述任务并明确说明需要分析意图:
text
Use code_execution to calculate the 7-day moving average for these numbers: ...text
Use x_search to find posts mentioning OpenClaw this week, then use code_execution to count them by day.text
Use web_search to gather the latest AI benchmark numbers, then use code_execution to compare percent changes.工具内部只接受一个 task 参数,智能体应将完整分析请求和内联数据一次性发送。
错误处理
当工具缺少认证时,返回结构化 missing_xai_api_key 错误(JSON 而非异常),智能体可据此自我纠正:
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"
}限制
- 这是 xAI 远程执行,不是本地进程执行
- 结果应视为临时分析环境,而非持久化 Notebook
- 无法访问本地文件或工作区
- 如需最新 X 平台数据,先用
x_search获取后传给code_execution
相关文档
Exec 工具
在本机或配对节点上执行 Shell 命令。
Exec 审批
Shell 执行的允许/拒绝策略。
网络工具
`web_search`、`x_search`、`web_fetch`。
xAI 提供方
Grok 模型、网络/x 搜索、代码执行配置。
常见问题
code_execution 和 exec 有什么本质区别?
exec 在本机或配对节点上执行真实 Shell 命令,可读写本地文件;code_execution 在 xAI 远程沙箱中运行 Python,无状态且无法访问本地文件。选择取决于是否需要本地资源。
怎么配置 code_execution 的模型和超时?
在 plugins.entries.xai.config.codeExecution 中设置 model 和 timeoutSeconds,默认模型 grok-4-1-fast,默认超时 30 秒。修改后需重启网关生效。
遇到 missing_xai_api_key 错误怎么办?
返回的 JSON 错误已包含修复指引:运行 openclaw onboard --auth-choice xai-oauth 或 --auth-choice xai-api-key,或设置 XAI_API_KEY 环境变量,或在配置中填写 plugins.entries.xai.config.webSearch.apiKey。