# 代码执行

> codeexecution：使用 xAI 运行沙箱隔离的远程 Python 分析

- 网址：https://funcoding.ai/agents/openclaw/tools/code-execution/
- 来源：OpenClaw 官方文档原文（中文），MIT 许可，同步于 2026-10-11
- 官方原文：https://docs.openclaw.ai/zh-CN/tools/code-execution

---
`code_execution` 在 xAI 的 Responses API 上运行沙箱隔离的远程 Python 分析
（`https://api.x.ai/v1/responses` 使用的同一端点 `x_search`）。它由内置的 `xai` 插件根据 `tools` 契约注册。

<div class="callout callout-warning">

`code_execution` 在 xAI 的服务器上运行。xAI 对每 1,000 次工具调用收取 $5，
另加模型的输入和输出 token 费用。

</div>

| 属性               | 值                                                                                |
| ------------------ | --------------------------------------------------------------------------------- |
| 工具名称           | `code_execution`                                                                  |
| 提供商插件         | `xai`（内置，`enabledByDefault: true`）                                         |
| 身份验证           | xAI 身份验证配置文件、`XAI_API_KEY` 或 `plugins.entries.xai.config.webSearch.apiKey` |
| 默认模型           | `grok-4.3`                                                                        |
| 默认超时时间       | 30 秒                                                                             |
| 默认 `maxTurns` | 未设置（xAI 应用其自身的内部限制）                                                |

可将其用于计算、制表、快速统计和图表式分析，包括分析由 `x_search` 或 `web_search` 返回的数据。它无法访问本地文件、你的 shell、你的仓库或已配对设备，并且不会在调用之间持久保存状态，因此应将每次调用视为临时分析，而不是笔记本会话。对于最新的 X 数据，请先运行 [`x_search`](https://funcoding.ai/agents/openclaw/tools/web/#x_search)，然后将结果传入。

如需本地执行，请改用 [`exec`](https://funcoding.ai/agents/openclaw/tools/exec/)。

## 设置

**提供 xAI 凭据**

OAuth 需要符合条件的 SuperGrok 或 X Premium 订阅
（使用设备代码验证，因此可从没有 localhost 回调的远程主机运行）：

```bash
openclaw models auth login --provider xai --method oauth
```

在全新安装期间，新手引导中也提供相同选项：

```bash
openclaw onboard --install-daemon --auth-choice xai-oauth
```

或使用 API 密钥：

```bash
openclaw models auth login --provider xai --method api-key
export XAI_API_KEY=xai-...
```

或通过配置：

```json5
{
  plugins: {
    entries: {
      xai: {
        config: {
          webSearch: {
            apiKey: "xai-...",
          },
        },
      },
    },
  },
}
```

这三种方式中的任意一种也能为 `x_search` 和 Grok `web_search` 提供支持。

**启用并调优 code_execution**

省略 `enabled` 时，仅当当前模型的提供商为 `xai` 且能够解析 xAI 凭据时，才会公开 `code_execution`。对于提供商已知且不是 xAI 的当前模型，请将 `plugins.entries.xai.config.codeExecution.enabled` 设置为 `true`，以选择启用跨提供商使用。如果当前模型提供商缺失或无法解析，该工具将保持隐藏。将 `enabled` 设置为 `false` 可对所有提供商禁用它。始终需要 xAI 凭据。

使用同一配置块可覆盖模型、轮次上限或超时时间：

```json5
{
  plugins: {
    entries: {
      xai: {
        config: {
          codeExecution: {
            enabled: true, // 对于提供商已知且不是 xAI 的模型，此项为必需
            model: "grok-4.3", // 覆盖默认的 xAI 代码执行模型
            maxTurns: 2,            // 内部工具轮次的可选上限
            timeoutSeconds: 30,     // 请求超时时间（默认：30）
          },
        },
      },
    },
  },
}
```

**重启 Gateway 网关**

```bash
openclaw gateway restart
```

xAI 插件重新注册，并且上述提供商、启用状态和身份验证检查均通过后，`code_execution` 将出现在智能体的工具列表中。

## 使用方法

明确说明分析意图；该工具只接受一个 `task` 参数，因此请在一个提示词中发送完整请求和所有内联数据：

```text
使用 code_execution 计算这些数字的 7 天移动平均值：...
```

```text
使用 x_search 查找本周提及 OpenClaw 的帖子，然后使用 code_execution 按天统计数量。
```

```text
使用 web_search 收集最新的 AI 基准测试数据，然后使用 code_execution 比较百分比变化。
```

## 错误

没有身份验证时，该工具会返回结构化 JSON 错误（而不是抛出的异常），以便智能体自行纠正：

```json
{
  "error": "missing_xai_api_key",
  "message": "code_execution 需要 xAI 凭据。运行 `openclaw onboard --auth-choice xai-oauth` 以使用 Grok 登录，运行 `openclaw onboard --auth-choice xai-api-key`，在 Gateway 网关环境中设置 `XAI_API_KEY`，或配置 `plugins.entries.xai.config.webSearch.apiKey`。",
  "docs": "https://docs.openclaw.ai/tools/code-execution"
}
```

## 相关内容

- [Exec 工具](https://funcoding.ai/agents/openclaw/tools/exec/)：在你的计算机或已配对节点上执行本地 shell。
- [Exec 审批](https://funcoding.ai/agents/openclaw/tools/exec-approvals/)：shell 执行的允许/拒绝策略。
- [Web 工具](https://funcoding.ai/agents/openclaw/tools/web/)：`web_search`、`x_search` 和 `web_fetch`。
- [xAI 提供商](https://funcoding.ai/agents/openclaw/providers/xai/)：Grok 模型、Web/X 搜索和代码执行配置。
