Appearance
代码执行(Code Execution)
code_execution 工具在 xAI 的远程沙箱中运行 Python 代码进行分析,与本地的 exec 有本质区别:
exec:在本机或节点上执行 Shell 命令code_execution:在 xAI 远程沙箱中运行 Python
适合用于:
- 数值计算
- 数据汇总与表格整理
- 快速统计分析
- 与
x_search或web_search联动的数据分析
不适合用于需要本地文件、本地 Shell、代码仓库或配对设备的场景,那些请用 exec。
配置
需要 xAI API Key,以下任意一种均可:
- 环境变量
XAI_API_KEY - 配置项
plugins.entries.xai.config.webSearch.apiKey
配置示例:
json5
{
plugins: {
entries: {
xai: {
config: {
webSearch: {
apiKey: "xai-...",
},
codeExecution: {
enabled: true,
model: "grok-4-1-fast",
maxTurns: 2,
timeoutSeconds: 30,
},
},
},
},
},
}使用方法
直接用自然语言告知 Agent 要做什么,并明确说明是数据分析任务:
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 参数,Agent 应将完整分析请求和内联数据一次性发送。
限制
- 这是 xAI 远程执行,不是本地进程
- 请视为临时分析环境,而非持久化 Notebook
- 无法访问本地文件或工作区
- 若需要最新 X 平台数据,请先用
x_search获取