กลับไปหน้าสูตร
#codex#openai#cli#cheatsheet#ai#o1#o4-mini

Codex CLI Cheatsheet

คู่มือฉบับสมบูรณ์ Codex CLI (OpenAI): พลังแห่ง O1/O4-mini ใน Terminal การใช้งาน Sandbox, Image Input และ Automation Workflow

11 มีนาคม 2569อ่านประมาณ 4 นาที

🚀 Core CLI Commands (จาก Terminal)

Commandการใช้งานเจาะลึก
codexเริ่มต้นใช้งาน Terminal UI (TUI) แบบเต็มตัว
codex "prompt"สั่งงานครั้งเดียว (Single action) แล้วเปิด Interactive mode
codex resume --lastกลับไปคุยต่อในเซสชันล่าสุดที่เคยรันไว้
codex resume <ID>โหลดเซสชันเฉพาะเจาะจงด้วย ID
codex appเปิด Desktop App (macOS) เพื่อใช้งานแบบ GUI ควบคู่กัน
codex --image <path>ส่งรูปภาพ (Screenshot/Design) ไปให้ AI วิเคราะห์โค้ด
codex --versionดูเวอร์ชันและข้อมูลระบบ

🛠 Interactive Slash Commands

🧠 Model & Plan

Commandการใช้งาน
/modelสลับโมเดลระหว่าง o1, o4-mini, gpt-4o
/planเข้าสู่ Plan Mode ให้ AI อธิบายขั้นตอนทั้งหมดก่อนลงมือทำ
/permissionsปรับระดับการขออนุญาต (Suggest, Auto Edit, Full Auto)
/diffดูความแตกต่างของโค้ด (Git Diff) ที่ AI แก้ไขไปในเซสชันนี้
/reviewให้ AI ตรวจสอบโค้ดที่เพิ่งแก้ไขไป เพื่อหาบั๊กแฝง

⚙️ Session & System

Commandการใช้งาน
/compactบีบอัดประวัติการคุยเพื่อล้างหน่วยความจำ Token (ดีสำหรับเซสชันยาว)
/mcpจัดการการเชื่อมต่อกับเครื่องมือภายนอกผ่าน Model Context Protocol
/clearล้างหน้าจอและรีเซ็ตประวัติการคุยเริ่มต้นใหม่
/exit หรือ /quitออกจากการทำงานของ Codex

🛡 Approval & Sandbox Modes

หัวใจสำคัญของ Codex คือความปลอดภัยในการรันคำสั่ง:

1) Approval Modes (--approval-mode)

  • suggest (Default): AI จะต้องขออนุญาต (Y/N) ก่อนแก้ไขไฟล์หรือรัน shell
  • auto-edit: แก้ไขไฟล์ได้เลยอัตโนมัติ แต่ต้องขออนุญาตก่อนรัน shell
  • full-auto: รันงานอัตโนมัติทั้งหมด (ต้องระวังมาก!)

2) Sandbox Policy (--sandbox)

  • read-only: AI อ่านไฟล์ได้อย่างเดียว ปลอดภัยสูงสุด
  • workspace-write: AI แก้ไขไฟล์ได้เฉพาะในโฟลเดอร์ปัจจุบันเท่านั้น
  • danger-full-access: ปลดล็อกการเข้าถึงไฟล์ระบบทั้งหมด (ใช้ในระบบปิดเท่านั้น)

🖼 Image & Multi-modal Integration

Codex รองรับการทำงานกับรูปภาพโดยตรง:

# ส่งภาพ UI Design แล้วให้สร้าง Tailwind CSS
codex --image ./design/mockup.png "ช่วยสร้าง Component ใน @src/components/ ตามรูปนี้"

# ส่งรูป Error จากหน้าจอแล้วให้ AI แก้ไข
codex --image ./logs/browser-error.png "วิเคราะห์สาเหตุของ error นี้ใน @src/app.js"

📄 Configuration Files

Fileบทบาท
GEMINI.mdใช้เก็บกฎเกณฑ์และมาตรฐานของโปรเจกต์ (Codex อ่านไฟล์นี้ด้วย)
AGENTS.mdกำหนดพฤติกรรมเฉพาะของ Agent ในโปรเจกต์นั้นๆ

📦 Official Install & Sign-in

วิธีคำสั่ง / วิธีใช้
npmnpm install -g @openai/codex
Homebrewbrew install --cask codex
เริ่มใช้งานรัน codex แล้วเลือก Sign in with ChatGPT
Desktop experiencecodex app

จาก official repo ตอนนี้แนะนำให้ใช้ Codex ผ่านบัญชี ChatGPT plan ได้โดยตรง และถ้าจะใช้ API key ต้องไปตั้งค่าตาม auth docs เพิ่ม


🧾 Official Quickstart Notes

ความสามารถรายละเอียดจาก official repo
codexเปิด Terminal agent หลัก
codex appเปิด Codex App สำหรับประสบการณ์แบบ desktop
Binary releaseดาวน์โหลดจาก GitHub Releases ได้โดยตรง
IDE usageมี flow แยกสำหรับ VS Code / Cursor / Windsurf

✅ Doc-backed Usage Additions

  • Codex CLI เป็น lightweight coding agent ที่รัน local บนเครื่องเรา
  • ใช้ได้ทั้งผ่าน ChatGPT sign-in และ API key setup
  • มีเส้นทางใช้งานแยกสำหรับ terminal, IDE และ app
  • official repo ระบุชัดว่ามี binary releases ให้ติดตั้งตรงได้โดยไม่ต้องผ่าน npm หรือ brew

🌟 จุดเด่นเฉพาะตัวของ Codex

  • เด่นเรื่องการใช้ ChatGPT sign-in กับ coding agent ได้ตรงๆ โดยไม่จำเป็นต้องเริ่มจาก API key workflow
  • docs แยกประสบการณ์ใช้งานเป็น CLI, App, IDE, และ Web ค่อนข้างชัด ทำให้ต่อ workflow ข้าม surfaces ได้ดี
  • custom agents ใช้ไฟล์ .toml และมี schema ที่เน้น operation จริง เช่น developer_instructions, sandbox_mode, model_reasoning_effort, nickname_candidates
  • มี subagent fan-out workflow และ spawn_agents_on_csv สำหรับ batch jobs หลายแถว ซึ่งเป็น pattern ที่เด่นมากสำหรับงาน audit/review จำนวนมาก
  • ecosystem ของ Codex ผูกกับ Hooks, Rules, Skills, MCP, AGENTS.md, และ security approvals ค่อนข้างชัด เหมาะกับงาน production ที่ต้องคุม sandbox กับ approval behavior มาก

🤖 Subagents & Custom Agents

Built-in Agents จาก docs

Agentใช้ทำอะไร
defaultagent ทั่วไปสำหรับงานทั่วไป
workerเน้น implementation และ fixes
explorerเน้นอ่าน codebase และสำรวจข้อมูล

Subagent Workflow

  • Codex จะ spawn subagents เมื่อเรา สั่งอย่างชัดเจน
  • เหมาะกับงาน review หลายมุม, codebase exploration, หรือ feature plan หลายส่วน
  • ใช้ /agent ใน CLI เพื่อสลับดู active agent threads

วิธีเรียก Subagent / Custom Agent

วิธีตัวอย่าง
สั่งเป็นภาษาธรรมชาติSpawn one agent per point, wait for all of them, and summarize the result for each point.
ระบุชื่อ agent ที่อยากใช้Have pr_explorer map the affected code paths, reviewer find real risks, and docs_researcher verify the framework APIs.
สั่งให้ Codex แตกงานหลายมุมUse explorer to inspect the codebase, then have worker implement the smallest safe fix.
ดูหรือสลับ thread ที่กำลังรันใช้ /agent ใน CLI

หลักสำคัญจาก docs:

  • Codex จะไม่ spawn subagents เองมั่วๆ ถ้าเราไม่ได้ขอชัด
  • ถ้าอยากใช้ custom agent ตัวไหน ให้เรียกชื่อมันใน prompt ตรงๆ
  • เมื่อมีหลาย agent พร้อมกัน Codex จะรอผลทั้งหมดแล้วค่อยสรุปรวมให้

ตัวอย่าง prompt จาก docs:

I would like to review the following points on the current PR (this branch vs main).
Spawn one agent per point, wait for all of them, and summarize the result for each point.

Custom Agent Files

ScopePath
Personal~/.codex/agents/
Project.codex/agents/

ตัวอย่าง .toml:

name = "reviewer"
description = "PR reviewer focused on correctness, security, and missing tests."
developer_instructions = """
Review code like an owner.
Prioritize correctness, security, behavior regressions, and missing test coverage.
"""
nickname_candidates = ["Atlas", "Delta", "Echo"]

Fields สำคัญ

Fieldความหมาย
nameชื่อ agent
descriptionคำอธิบายว่า agent นี้ควรถูกใช้เมื่อไร
developer_instructionscore instructions ของ agent
nickname_candidatesชื่อเล่นสำหรับ UI
modelmodel override
model_reasoning_effortreasoning effort
sandbox_modeเช่น read-only, workspace-write
mcp_serversMCP เฉพาะ agent
skills.configskills ที่ผูกกับ agent

Global Agent Settings

Keyใช้ทำอะไร
agents.max_threadsจำนวน agent threads พร้อมกัน
agents.max_depthความลึกของ nested spawning
agents.job_max_runtime_secondstimeout ต่อ worker

🧩 Config, Skills, Hooks, MCP

จาก docs navigation ของ Codex ตอนนี้มีหัวข้อ config/customization ชัดเจน:

  • AGENTS.md สำหรับคำสั่งและกติกาในโปรเจกต์
  • Hooks สำหรับ workflow automation
  • MCP สำหรับต่อ external tools/data
  • Skills สำหรับ reusable capability
  • Rules และ config file สำหรับกำหนด behavior เพิ่ม

ถ้าจะออกแบบ workflow จริงใน Codex ปกติมักจับคู่กันแบบนี้:

  • custom agent สำหรับ persona + sandbox + model
  • skills สำหรับ reusable expertise
  • MCP สำหรับเครื่องมือภายนอก
  • hooks/rules สำหรับ guardrail และ automation

⌨️ Advanced Tips

  • Reference Scope: ใช้ @src/ เพื่อระบุขอบเขตไฟล์ที่ต้องการให้ AI ดู
  • Shell Commands: Codex สามารถรัน !npm test หรือ !git status ได้โดยตรง
  • Multi-line Input: ใช้ Alt + Enter เพื่อพิมพ์ Prompt ยาวๆ หลายบรรทัด
  • Session ID: ทุกเซสชันจะมี ID เฉพาะตัว สามารถกู้คืนได้ทุกเมื่อ

⚡ Automation Recipes (Real-world)

1) UI Transformation (Image to Code)

codex --image ./figma-export.png "เปลี่ยนรูปนี้เป็น React Component โดยใช้ shadcn/ui"

2) Debugging with Context

codex "รัน !npm test ดู @test-results/ พังตรงไหนแล้วแก้ใน @src/ ให้หน่อย"

3) Bulk Refactoring (Auto Mode)

codex --approval-mode auto-edit "ย้าย Logic ใน @src/pages/ ไปไว้ใน @src/hooks/ ทั้งหมด"

📅 Daily Pro Workflow

  1. Morning Prep: codex resume --last เพื่อเช็กงานที่ค้างไว้
  2. Strategy: ใช้ /plan วางแผนงานใหญ่ประจำวัน
  3. Develop: เขียนโค้ดโดยใช้ @ ระบุไฟล์ และให้ AI รันเทสให้
  4. Safety Check: ใช้ /diff ตรวจสอบความเรียบร้อยก่อน commit
  5. Git Commit: สั่ง !git add . && !git commit -m "..." ผ่าน Codex ได้เลย