Tools
コード実行
code_execution は、xAI の Responses API でサンドボックス化されたリモート Python 分析を実行します。これはバンドル済みの xai plugin(tools contract 配下)によって登録され、x_search と同じ https://api.x.ai/v1/responses endpoint にディスパッチされます。
| プロパティ | 値 |
|---|---|
| ツール名 | code_execution |
| Provider plugin | xai(バンドル済み、enabledByDefault: true) |
| 認証 | xAI 認証 profile、XAI_API_KEY、または plugins.entries.xai.config.webSearch.apiKey |
| デフォルト model | grok-4-1-fast |
| デフォルト timeout | 30 秒 |
デフォルト maxTurns |
未設定(xAI が独自の内部制限を適用) |
これはローカルの exec とは異なります。
execは、あなたのマシンまたはペアリング済み node で shell コマンドを実行します。code_executionは、xAI のリモートサンドボックスで Python を実行します。
code_execution は次の場合に使用します。
- 計算。
- 表作成。
- 簡単な統計。
- グラフ形式の分析。
x_searchまたはweb_searchから返されたデータの分析。
ローカルファイル、shell、repo、またはペアリング済みデバイスが必要な場合は使用しないでください。その場合は exec を使用してください。
セットアップ
xAI 認証情報を提供する
対象の SuperGrok または X Premium サブスクリプションを使って Grok OAuth でサインインするか、
API key を保存します。xAI OAuth は device-code verification を使用するため、localhost callback がない
リモート host からでも動作します。OAuth は code_execution と x_search で動作します。
XAI_API_KEY または plugin web-search config でも Grok web_search を動かせます。
openclaw models auth login --provider xai --method oauth新規インストール時は、オンボーディング内でも同じ認証の選択肢を利用できます。
openclaw onboard --install-daemonopenclaw onboard --install-daemon --auth-choice xai-oauthまたは API key を使用します。
openclaw models auth login --provider xai --method api-keyexport XAI_API_KEY=xai-...または config 経由で設定します。
{ plugins: { entries: { xai: { config: { webSearch: { apiKey: "xai-...", }, }, }, }, },}code_execution を有効化して調整する
xAI 認証情報が利用可能な場合、code_execution を使用できます。無効化するには
plugins.entries.xai.config.codeExecution.enabled を false に設定します。
また、同じ block を使って model と timeout を調整できます。
{ 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) }, }, }, }, },}Gateway を再起動する
openclaw gateway restartxAI plugin が enabled: true で再登録されると、code_execution が agent のツール一覧に表示されます。
使い方
自然に依頼し、分析の意図を明示します。
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.この tool は内部的に単一の task parameter を取るため、agent は完全な分析リクエストと任意の inline data を 1 つの prompt で送る必要があります。
エラー
tool が認証なしで実行されると、auth-profile、env var、config options を示す構造化された missing_xai_api_key エラーを返します。このエラーは JSON であり、throw された例外ではないため、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"}制限
- これはローカル process 実行ではなく、リモート xAI 実行です。
- 結果は永続的な notebook session ではなく、一時的な分析として扱ってください。
- ローカルファイルや workspace へのアクセスを想定しないでください。
- 新しい X データについては、まず
x_searchを使用し、その結果をcode_executionに渡してください。