Tools
การเรียกใช้โค้ด
code_execution เรียกใช้การวิเคราะห์ Python ระยะไกลแบบ sandbox บน Responses API ของ xAI เครื่องมือนี้ลงทะเบียนโดยปลั๊กอิน xai ที่บันเดิลมาให้ (ภายใต้สัญญา tools) และส่งคำขอไปยัง endpoint เดียวกันคือ https://api.x.ai/v1/responses ที่ x_search ใช้
| คุณสมบัติ | ค่า |
|---|---|
| ชื่อเครื่องมือ | code_execution |
| ปลั๊กอินผู้ให้บริการ | xai (บันเดิลมาให้, enabledByDefault: true) |
| การยืนยันตัวตน | โปรไฟล์ยืนยันตัวตน xAI, XAI_API_KEY, หรือ plugins.entries.xai.config.webSearch.apiKey |
| โมเดลเริ่มต้น | grok-4-1-fast |
| timeout เริ่มต้น | 30 วินาที |
maxTurns เริ่มต้น |
ไม่ได้ตั้งค่า (xAI ใช้ขีดจำกัดภายในของตัวเอง) |
สิ่งนี้แตกต่างจาก exec แบบ local:
execเรียกใช้คำสั่ง shell บนเครื่องของคุณหรือโหนดที่จับคู่ไว้code_executionเรียกใช้ Python ใน sandbox ระยะไกลของ xAI
ใช้ code_execution สำหรับ:
- การคำนวณ
- การจัดทำตาราง
- สถิติด่วน
- การวิเคราะห์แบบแผนภูมิ
- การวิเคราะห์ข้อมูลที่ได้จาก
x_searchหรือweb_search
อย่าใช้เครื่องมือนี้เมื่อคุณต้องการไฟล์ local, shell ของคุณ, repo ของคุณ หรืออุปกรณ์ที่จับคู่ไว้ ให้ใช้ exec สำหรับกรณีนั้น
การตั้งค่า
ระบุข้อมูลรับรอง xAI
ลงชื่อเข้าใช้ด้วย Grok OAuth โดยใช้การสมัครใช้งาน SuperGrok หรือ X Premium ที่มีสิทธิ์
หรือจัดเก็บ API key xAI OAuth ใช้การตรวจสอบยืนยันแบบ device-code จึงทำงานได้
จากโฮสต์ระยะไกลโดยไม่ต้องมี localhost callback OAuth ใช้ได้กับ
code_execution และ x_search; XAI_API_KEY หรือ config web-search ของปลั๊กอิน
ยังสามารถขับเคลื่อน Grok web_search ได้ด้วย
openclaw models auth login --provider xai --method oauthระหว่างการติดตั้งใหม่ ตัวเลือกการยืนยันตัวตนเดียวกันจะมีให้ใช้งานภายใน onboarding:
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
code_execution จะพร้อมใช้งานเมื่อมีข้อมูลรับรอง xAI ตั้งค่า
plugins.entries.xai.config.codeExecution.enabled เป็น false เพื่อปิดใช้
หรือใช้บล็อกเดียวกันเพื่อปรับแต่งโมเดลและ 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 restartcode_execution จะแสดงในรายการเครื่องมือของ agent เมื่อปลั๊กอิน xAI ลงทะเบียนใหม่ด้วย enabled: true
วิธีใช้งาน
ถามอย่างเป็นธรรมชาติและระบุเจตนาการวิเคราะห์ให้ชัดเจน:
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.เครื่องมือนี้รับพารามิเตอร์ task เพียงตัวเดียวภายใน ดังนั้น agent ควรส่งคำขอวิเคราะห์แบบเต็มและข้อมูล inline ใด ๆ ใน prompt เดียว
ข้อผิดพลาด
เมื่อเครื่องมือทำงานโดยไม่มีการยืนยันตัวตน เครื่องมือจะคืนข้อผิดพลาด missing_xai_api_key แบบมีโครงสร้าง ซึ่งชี้ไปที่ตัวเลือก auth-profile, env var และ config ข้อผิดพลาดเป็น JSON ไม่ใช่ exception ที่ถูก 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"}ขีดจำกัด
- นี่คือการประมวลผลระยะไกลของ xAI ไม่ใช่การประมวลผล process แบบ local
- ให้ถือผลลัพธ์เป็นการวิเคราะห์ชั่วคราว ไม่ใช่ session notebook แบบถาวร
- อย่าถือว่ามีสิทธิ์เข้าถึงไฟล์ local หรือ workspace ของคุณ
- สำหรับข้อมูล X สดใหม่ ให้ใช้
x_searchก่อน แล้วส่งผลลัพธ์เข้าไปยังcode_execution