docs/tools/code-execution.md
code_execution runs sandboxed remote Python analysis on xAI's Responses API.
This is different from local exec:
exec runs shell commands on your machine or nodecode_execution runs Python in xAI's remote sandboxUse code_execution for:
x_search or web_searchDo not use it when you need local files, your shell, your repo, or paired
devices. Use exec for that.
You need an xAI API key. Any of these work:
XAI_API_KEYplugins.entries.xai.config.webSearch.apiKeyExample:
{
plugins: {
entries: {
xai: {
config: {
webSearch: {
apiKey: "xai-...",
},
codeExecution: {
enabled: true,
model: "grok-4-1-fast",
maxTurns: 2,
timeoutSeconds: 30,
},
},
},
},
},
}
Ask naturally and make the analysis intent explicit:
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.
The tool takes a single task parameter internally, so the agent should send
the full analysis request and any inline data in one prompt.
x_search first.