本页内容

本页内容

工具

代码执行

code_execution 对 xAI 的响应 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.7
默认超时 30 秒
默认 maxTurns 未设置(xAI 会应用其自身的内部限制)

省略工具模型设置的现有安装也使用 Grok 4.7. 仍选择显式模型设置。

可用于计算、制表、快速统计以及图表式分析,包括来自 x_search 或 web_search 的数据。它无法访问本地文件、你的 shell、你的仓库或配对设备,并且不会在调用之间持久保存状态,因此请将每次调用视为一次性的分析,而不是笔记本会话。若要获取最新的 X 数据,请先运行 x_search,然后将结果传入。

对于本地执行,请改用 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-keyexport 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, // required for a known non-xAI model provider            model: "grok-4.3", // override the default xAI code-execution model            maxTurns: 2,            // optional cap on internal tool turns            timeoutSeconds: 30,     // request timeout (default: 30)          },        },      },    },  },}
  • 验证工具可用性

    使用默认的混合重新加载模式,插件配置更改会自动应用。如果网关处于离线状态,请在配置后启动网关。参见配置热重载。

    在目标对话中发送 /tools,并在上述提供者、启用和身份验证检查通过后检查 code_execution。如果更改了网关服务的进程环境,请重新启动该服务以加载它。

  • 如何使用

    明确表达分析意图;该工具只接受一个 task 参数, 因此请在一个提示中发送完整请求以及任何内联数据:

    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.

    错误

    如果没有认证,工具会返回一个结构化的 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"}

    相关内容

    本页有帮助吗?