メインコンテンツへスキップ
code_execution は、xAI の Responses API (https://api.x.ai/v1/responsesx_search が使うものと同じエンドポイント) で、サンドボックス化されたリモート Python 解析を実行します。これは バンドル済みの xai plugin により、tools contract の下で登録されます。
プロパティ
ツール名code_execution
Provider pluginxai (バンドル済み、enabledByDefault: true)
認証xAI 認証プロファイル、XAI_API_KEY、または plugins.entries.xai.config.webSearch.apiKey
デフォルトモデルgrok-4-1-fast
デフォルトタイムアウト30 秒
デフォルト maxTurns未設定 (xAI が独自の内部制限を適用)
計算、表作成、簡単な統計、チャート形式の解析に使用します。これには x_searchweb_search から返されたデータも含まれます。ローカルファイル、シェル、リポジトリ、ペアリング済みデバイスには アクセスできず、呼び出し間で状態を永続化しません。そのため、各呼び出しはノートブックセッションではなく、一時的な解析として扱います。新しい X データには、まず x_search を実行し、その結果を渡してください。 ローカル実行には、代わりに exec を使用します。

セットアップ

1

xAI 認証情報を提供する

OAuth には対象となる SuperGrok または X Premium サブスクリプションが必要です (device-code 検証のため、localhost callback なしでリモートホストから動作します):
openclaw models auth login --provider xai --method oauth
新規インストール時には、オンボーディングでも同じ選択肢を利用できます:
openclaw onboard --install-daemon --auth-choice xai-oauth
または API キー:
openclaw models auth login --provider xai --method api-key
export XAI_API_KEY=xai-...
または config 経由:
{
  plugins: {
    entries: {
      xai: {
        config: {
          webSearch: {
            apiKey: "xai-...",
          },
        },
      },
    },
  },
}
これら 3 つはいずれも x_search と Grok web_search も動作させます。
2

code_execution を有効化して調整する

code_execution は、xAI 認証情報が解決できる場合に利用できます。無効化するには plugins.entries.xai.config.codeExecution.enabledfalse に設定します。 または同じブロックを使って、モデル、turn 上限、タイムアウトを上書きします:
{
  plugins: {
    entries: {
      xai: {
        config: {
          codeExecution: {
            enabled: true,
            model: "grok-4-1-fast", // override the default xAI code-execution model
            maxTurns: 2,            // optional cap on internal tool turns
            timeoutSeconds: 30,     // request timeout (default: 30)
          },
        },
      },
    },
  },
}
3

Gateway を再起動する

openclaw gateway restart
xAI plugin が enabled: true で再登録されると、code_execution が agent のツール一覧に表示されます。

使用方法

解析の意図を明示します。このツールは単一の task パラメータを取るため、 リクエスト全体とインラインデータを 1 つのプロンプトで送信します:
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.

エラー

認証がない場合、このツールは thrown exception ではなく構造化された JSON エラーを返すため、 agent は自己修正できます:
{
  "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"
}

関連

Exec ツール

自分のマシンまたはペアリング済み node でのローカルシェル実行。

Exec 承認

シェル実行の許可/拒否ポリシー。

Web ツール

web_searchx_searchweb_fetch

xAI provider

Grok モデル、web/x search、code execution config。