xAI Code Execution tool (type: "code_interpreter", alias "code_execution")
Status: DOCUMENTED · LIVE_VERIFIED (2026-09-19, grok-4.3: print(2+2) → code_interpreter_call with code, outputs[0].logs stdout 4, code_interpreter_calls: 1; streamed run emitted the 5 response.code_interpreter_call* events; {type:"code_execution"} accepted and echoed as code_interpreter)
Sources: https://docs.x.ai/developers/tools/code-execution · https://docs.x.ai/developers/tools/streaming-and-sync (include values) · https://docs.x.ai/developers/model-capabilities/files/chat-with-files (files + code) · OpenAPI CodeInterpreterCall, CodeInterpreterOutput
Last verified: 2026-09-19
Parameters
{"type":"code_interpreter"} (OpenAI SDK name) or {"type":"code_execution"} (xAI name; normalised). container accepted for compatibility (not needed). Responses only (gRPC/xai-sdk: code_execution(); code_interpreter string not supported there).
Output
{"type":"code_interpreter_call","id":"ci_<resp>_call-…-0","status":"completed","code":"print(2+2)",
"outputs":[{"type":"logs","logs":"{\"stdout\":\"4\\n\",\"stderr\":\"\",\"exit_code\":0,\"command_timed_out\":false}"}]}outputs[] only with include:["code_interpreter_call.outputs"] (logs is a JSON string with stdout/stderr/exit_code/command_timed_out; image outputs {type:"image", url}). Chat Completions: generated files via output_files[] with include: code_execution_files_output (docs; chat has no tools[] support live). Stream: response.code_interpreter_call.in_progress → …call_code.delta → …call_code.done → …call.interpreting → …call.completed.
Sandbox
Python with NumPy/Pandas/Matplotlib/SciPy; no network, no persistent filesystem, per-request stateless; time/memory limits. Attach data files (input_file) to analyse them. Usage live: 2 324 tokens (368 reasoning) + 1 call = ticks 81 834 000 ≈ $0.0082.
Billing
$5 per 1k calls + tokens. Docs recommend low temperature for math.
Examples: examples/xai/tools/code-execution/. Test: tests/xai/test_tools.py::test_code_interpreter_tool (RUN_EXPENSIVE_TESTS).