Tools

การเรียกใช้โค้ด

code_execution รันการวิเคราะห์ Python ระยะไกลใน sandbox บน Responses API ของ xAI โดยลงทะเบียนผ่าน Plugin xai ที่มาพร้อมชุด (ภายใต้สัญญา tools) และส่งคำขอไปยัง endpoint เดียวกันคือ https://api.x.ai/v1/responses ที่ x_search ใช้

คุณสมบัติ ค่า
ชื่อเครื่องมือ code_execution
Plugin ผู้ให้บริการ xai (มาพร้อมชุด, enabledByDefault: true)
การยืนยันตัวตน 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 API

    ตั้งค่า XAI_API_KEY ในสภาพแวดล้อมของ Gateway หรือกำหนดค่าคีย์ภายใต้ Plugin xAI เพื่อให้ credential เดียวกันครอบคลุม code_execution, x_search, การค้นหาเว็บ และเครื่องมือ xAI อื่นๆ:

    export XAI_API_KEY=xai-...
    

    หรือผ่าน config:

    {
      plugins: {
        entries: {
          xai: {
            config: {
              webSearch: {
                apiKey: "xai-...",
              },
            },
          },
        },
      },
    }
    
  • เปิดใช้งานและปรับแต่ง code_execution

    เครื่องมือนี้ถูกควบคุมด้วย plugins.entries.xai.config.codeExecution.enabled ค่าเริ่มต้นคือปิด

    {
      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 restart
    

    code_execution จะแสดงในรายการเครื่องมือของ agent เมื่อ Plugin 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 แบบมีโครงสร้างที่ชี้ไปยัง env var และเส้นทาง config ข้อผิดพลาดนี้เป็น JSON ไม่ใช่ exception ที่ถูก throw ดังนั้น agent จึงแก้ไขได้เอง:

    {
      "error": "missing_xai_api_key",
      "message": "code_execution needs an 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 ไม่ใช่การประมวลผลโปรเซส local
    • ให้ถือว่าผลลัพธ์เป็นการวิเคราะห์ชั่วคราว ไม่ใช่เซสชัน notebook แบบถาวร
    • อย่าสันนิษฐานว่ามีสิทธิ์เข้าถึงไฟล์ local หรือ workspace ของคุณ
    • สำหรับข้อมูล X ที่สดใหม่ ให้ใช้ x_search ก่อน แล้วส่งผลลัพธ์เข้า code_execution

    ที่เกี่ยวข้อง