#Codex Commands: คู่มือใช้งาน OpenAI Codex CLI และ Slash Commands

OpenAI Codex CLI คือ AI coding agent ที่ทำงานจาก Terminal โดยตรง สามารถอ่านโค้ด แก้ไขไฟล์ รันคำสั่ง ทดสอบระบบ ตรวจสอบ Git diff ทำ code review เชื่อมต่อ MCP และนำไปใช้ในงาน automation หรือ CI/CD ได้

จุดสำคัญคือคำว่า Codex Commands มีอยู่ 2 กลุ่มหลัก

  1. CLI / Shell Commands — รันจาก Terminal เช่น codex exec, codex review, codex resume
  2. Slash Commands — ใช้หลังจากเข้า Interactive TUI แล้ว เช่น /model, /permissions, /status, /review

บทความนี้อ้างอิงสถานะของ Codex CLI ณ วันที่ 22 กันยายน 2026 เนื่องจาก Codex พัฒนาเร็ว รายการคำสั่งและ option บางรายการอาจเปลี่ยนได้ ควรตรวจสอบด้วย codex --help และพิมพ์ / ภายใน TUI ของเวอร์ชันที่ติดตั้งอยู่เสมอ


#1. ติดตั้ง Codex CLI

#macOS / Linux

curl -fsSL https://chatgpt.com/codex/install.sh | sh

#Windows PowerShell

powershell -ExecutionPolicy ByPass -c "irm https://chatgpt.com/codex/install.ps1 | iex"

#ติดตั้งด้วย npm

npm install -g @openai/codex

#ติดตั้งด้วย Homebrew

brew install --cask codex

ตรวจสอบเวอร์ชัน

codex --version

ดู help

codex --help

#2. เริ่มใช้งาน Codex

เข้าไปยัง project ที่ต้องการ

cd my-project

แล้วรัน

codex

Codex จะเปิด Interactive Terminal UI (TUI)

สามารถส่ง prompt ตั้งแต่เริ่มต้นได้เช่น

codex "Explain this project architecture"

หรือ

codex "Find bugs in this repository"

ตัวอย่างงานที่เหมาะกับ Codex

Explain this project structure.
Find the cause of the failing tests and fix it.
Refactor this REST API to use a service layer.
Add unit tests for the authentication module.
Review the current changes for bugs and security issues.

#3. คำสั่ง CLI สำคัญ

#codex

เปิด Interactive TUI

codex

พร้อม prompt

codex "Explain this repository"

กำหนด working directory

codex -C ./backend

เปิด live web search

codex --search

#codex exec

ใช้ Codex แบบ Non-interactive

เหมาะกับ

  • Shell script
  • CI/CD
  • GitHub Actions
  • automation
  • scheduled jobs
  • pipeline

ตัวอย่าง

codex exec "Explain the architecture of this project"

มี alias เป็น

codex e "Explain the architecture of this project"

ตัวอย่างให้ตรวจสอบ project

codex exec "Run the tests, identify the failures, and summarize the likely root causes"

อ่าน prompt จาก stdin ได้

echo "Review this project for security issues" | codex exec -

ตัวอย่างใน shell script

#!/bin/bash

codex exec \
  "Review the current codebase and report high-risk issues."

#4. codex review

ใช้สำหรับทำ Code Review แบบ non-interactive

#Review uncommitted changes

codex review --uncommitted

ตรวจสอบทั้ง

  • staged changes
  • unstaged changes
  • untracked changes

เหมาะมากก่อน commit

git status
codex review --uncommitted

#Review เทียบกับ branch

codex review --base main

ตัวอย่าง

codex review --base develop

เหมาะกับการตรวจ branch feature เทียบกับ branch หลัก


#Review commit

codex review --commit <SHA>

ตัวอย่าง

codex review --commit a1b2c3d

#5. codex resume

Codex เก็บ session ไว้ ทำให้สามารถกลับมาทำงานต่อได้

เปิดตัวเลือก session

codex resume

กลับไป session ล่าสุดของ current directory

codex resume --last

ค้นหา session จาก directory อื่นด้วย

codex resume --all

หรือระบุ session โดยตรง

codex resume <SESSION_ID>

เหมาะกับ workflow เช่น

Day 1
codex
  -> วิเคราะห์ architecture
  -> แก้ API
  -> เพิ่ม test

วันต่อมา

codex resume --last

แล้วทำงานต่อจาก context เดิม


#6. codex fork

สร้าง conversation ใหม่จาก session เดิม โดยไม่ทำให้ session ต้นฉบับหาย

codex fork

fork จาก session ล่าสุด

codex fork --last

กรณีใช้งาน

Session A
 ├─ แนวทางเดิม
 └─ Fork → ทดลอง architecture แบบใหม่

เหมาะกับการทดลองหลาย solution


#7. codex login

ใช้เข้าสู่ระบบ

codex login

สามารถใช้ ChatGPT sign-in หรือ authentication method ที่ระบบรองรับ

หากต้องการใช้ API key จาก environment

printenv OPENAI_API_KEY | codex login --with-api-key

Windows PowerShell ตัวอย่าง

$env:OPENAI_API_KEY | codex login --with-api-key

#8. codex logout

ออกจากระบบและลบ authentication credentials ที่เก็บไว้

codex logout

#9. codex doctor

ตรวจสอบปัญหาของ Codex installation

codex doctor

ใช้ตรวจสอบเรื่องต่าง ๆ เช่น

  • installation
  • configuration
  • authentication
  • runtime
  • Git
  • terminal
  • app server
  • session/thread inventory

เมื่อ Codex ทำงานผิดปกติ คำสั่งนี้ควรเป็นหนึ่งในคำสั่งแรกที่ลองใช้


#10. codex update

อัปเดต Codex CLI หาก installation รองรับ self-update

codex update

ตรวจสอบ version หลัง update

codex --version

#11. codex completion

สร้าง shell completion

ตัวอย่าง Bash

codex completion bash

Zsh

codex completion zsh

Fish

codex completion fish

PowerShell

codex completion powershell

ช่วยให้กด Tab เพื่อ autocomplete command และ option ได้สะดวกขึ้น


#12. codex mcp

Codex รองรับ Model Context Protocol (MCP) เพื่อเชื่อม AI agent กับ tools และ external systems

ดู MCP servers

codex mcp list

ดูแบบ JSON

codex mcp list --json

ดู server รายตัว

codex mcp get <name>

เพิ่ม MCP แบบ stdio

codex mcp add my-server -- node server.js

เพิ่ม MCP แบบ HTTP

codex mcp add my-server --url https://example.com/mcp

ตัวอย่างเพิ่ม OpenAI Developers Docs MCP

codex mcp add openaiDeveloperDocs \
  --url https://developers.openai.com/mcp

จากนั้น Codex สามารถใช้ MCP tools ที่ server นั้น expose ให้ได้


#13. codex plugin

Codex รองรับ plugin สำหรับเพิ่ม workflow และ integration

ดู plugin

codex plugin list

ติดตั้ง plugin ตามคำสั่งของ marketplace/plugin ที่ใช้งาน

Codex ยังรองรับ plugin marketplace ผ่าน

codex plugin marketplace

ใช้สำหรับจัดการแหล่ง plugin เช่น

  • add marketplace
  • list marketplace
  • upgrade marketplace
  • remove marketplace

#14. codex sandbox

ใช้รันคำสั่งภายใน sandbox ที่ Codex จัดเตรียม

codex sandbox

แนวคิดคือจำกัดขอบเขตการเข้าถึงของ command เพื่อลดความเสี่ยงจากการรันคำสั่งบนเครื่องโดยตรง

Codex รองรับ sandbox policy หลัก เช่น

read-only
workspace-write
danger-full-access

ตัวอย่างกำหนด sandbox ตอนเปิด Codex

codex --sandbox read-only

หรือ

codex --sandbox workspace-write

ควรหลีกเลี่ยง danger-full-access หากงานไม่ได้จำเป็นต้องใช้สิทธิ์ระดับนั้น


#15. เปิด Web Search

หาก task ต้องใช้ข้อมูลปัจจุบัน

codex --search

เช่น

codex --search \
  "Check the latest Laravel documentation and update this project accordingly"

เหมาะกับ

  • library version ล่าสุด
  • API documentation
  • security advisory
  • framework migration
  • cloud service documentation

#16. Slash Commands ใน Codex TUI

หลังจากรัน

codex

สามารถพิมพ์

/

เพื่อเปิด command picker

นี่เป็นวิธีที่แม่นยำที่สุดในการดูคำสั่งที่รองรับใน Codex CLI version ที่ติดตั้งอยู่จริง


#/model

เลือก model และ reasoning effort

/model

เหมาะเมื่อต้องการเปลี่ยน model ตามลักษณะงาน

งานทั่วไป
→ reasoning ปกติ

debug architecture ซับซ้อน
→ reasoning สูงขึ้น

#/permissions

กำหนดขอบเขตที่ Codex สามารถทำได้

/permissions

ใช้ควบคุมเรื่อง

  • file access
  • command execution
  • sandbox
  • approval behavior

ควรตรวจ permissions ก่อนให้ agent ทำงานที่สามารถแก้ไฟล์หรือรัน command ได้จำนวนมาก


#/status

ดูสถานะ session

/status

โดยทั่วไปใช้ดูข้อมูล เช่น

  • model
  • reasoning
  • permission/approval configuration
  • token/context usage
  • session information

#/review

เข้าสู่ code review flow

/review

ใช้ตรวจ

  • uncommitted changes
  • branch diff
  • code changes
  • bug risks

#/init

สร้างไฟล์

AGENTS.md

ใช้

/init

Codex จะสร้างโครงสำหรับ project instruction

ตัวอย่าง AGENTS.md

# AGENTS.md

## Project rules

- Use TypeScript.
- Run npm test after modifying code.
- Run npm run lint before finishing.
- Do not modify database migrations without confirmation.
- Follow existing architecture.

AGENTS.md เป็นไฟล์สำคัญมากสำหรับกำหนด coding convention และ workflow ให้ Codex


#17. Slash Commands ที่มีประโยชน์เพิ่มเติม

Codex CLI รุ่นใหม่มีคำสั่ง TUI จำนวนมาก โดย availability อาจแตกต่างตาม release และ configuration

Command หน้าที่
/new เริ่ม chat ใหม่
/compact ย่อ context ของ conversation เพื่อประหยัด context window
/review ตรวจ current changes
/rename เปลี่ยนชื่อ thread
/resume กลับไป session ที่บันทึกไว้
/fork แตก session เป็น branch ใหม่
/worktree เปิดงานใน Git worktree
/diff แสดง Git diff
/status ดู session configuration
/model เลือก model/reasoning
/permissions ตั้ง permissions
/plan เข้า Plan mode
/skills ดูหรือใช้ skills
/mcp ดู MCP tools
/plugins จัดการ/browse plugins
/copy copy คำตอบล่าสุด
/export export conversation เป็น Markdown
/cd เปลี่ยน working directory
/pwd แสดง working directory
/usage ดู usage
/feedback ส่ง feedback
/quit ออกจาก Codex
/exit ออกจาก Codex

พิมพ์ / ใน TUI เพื่อดูรายการจริงของ version ที่ติดตั้ง


#18. รัน Shell Command จาก Codex TUI

Codex CLI สามารถรัน shell command ด้วย !

ตัวอย่าง

!ls
!git status
!npm test
!docker compose ps

จึงสามารถสลับระหว่างการสั่ง AI กับ command ปกติได้ใน session เดียว

ตัวอย่าง workflow

!git status

Review the current implementation.

!npm test

Fix the failing tests.

!git diff

#19. AGENTS.md — หัวใจของ Project Instructions

Codex อ่าน AGENTS.md ก่อนเริ่มงานเพื่อเข้าใจข้อกำหนดของ project

ตัวอย่าง

# AGENTS.md

## Stack

- Laravel 13
- PHP 8.4
- PostgreSQL
- React
- TypeScript

## Development Rules

- Follow PSR-12.
- Use Form Request for input validation.
- Use Service classes for business logic.
- Write Feature Tests for every API endpoint.
- Do not edit production credentials.

## Testing

Before completing a task run:

```bash
php artisan test
npm test

จากนั้นเปิด Codex

```bash
codex

Codex จะนำ instruction เหล่านี้ไปประกอบ context ก่อนทำงาน


#20. ตัวอย่าง Workflow: Fix Bug

เริ่ม Codex

codex

ตรวจสถานะ

/status

ดู permission

/permissions

สั่งงาน

Investigate why the login API returns HTTP 500.
Reproduce the issue first, identify the root cause,
make the smallest safe fix, and run the relevant tests.

ดู diff

/diff

ขอ review

/review

ตรวจ test

!php artisan test

#21. ตัวอย่าง Workflow: React Project

cd react-app
codex

สร้าง project instructions

/init

จากนั้นปรับ AGENTS.md

## React Rules

- Use TypeScript.
- Prefer functional components.
- Use React Testing Library.
- Do not introduce dependencies unless necessary.
- Run npm test and npm run lint before finishing.

สั่ง Codex

Create a reusable LoginForm component.

Requirements:
- React Hook Form
- Zod validation
- accessible labels
- unit tests

ตรวจ diff

/diff

ทดสอบ

!npm test

review

/review

#22. ตัวอย่าง Workflow: Laravel API

cd laravel-api
codex

Prompt

Create CRUD endpoints for products.

Requirements:
- Laravel 13
- Form Request validation
- API Resource
- Service layer
- Feature Tests

จากนั้น

!php artisan test

หาก test fail

Analyze the failing tests and fix the root cause.
Do not weaken the assertions.

สุดท้าย

/review

#23. ใช้ Codex กับ Git

ตรวจ repository

!git status

ดู diff

/diff

หรือ

!git diff

ให้ Codex review

/review

จาก shell ภายนอก TUI ก็ทำได้

codex review --uncommitted

workflow ที่แนะนำ

Edit
   ↓
Test
   ↓
Diff
   ↓
Codex Review
   ↓
Human Review
   ↓
Commit

Codex ควรเป็น ผู้ช่วย review ไม่ใช่ตัวแทน human review ทั้งหมด โดยเฉพาะระบบ production หรือโค้ดที่เกี่ยวข้องกับ security, financial logic และข้อมูลสำคัญ


#24. ใช้ codex exec ใน CI/CD

แนวคิดพื้นฐาน

steps:
  - checkout
  - install dependencies
  - run tests
  - run Codex analysis
  - publish result

ตัวอย่างคำสั่ง

codex exec \
  "Review this repository for likely bugs introduced by the current changes.
   Return concise findings with file paths and reasoning."

หากต้องการนำ output ไปประมวลผลต่อ สามารถออกแบบ workflow ให้ stdout ของ codex exec ถูกส่งไปยังไฟล์หรือขั้นตอนถัดไป

codex exec "Summarize the current changes" > codex-review.txt

#25. กลุ่มคำสั่งที่ควรจำ

หากเพิ่งเริ่มใช้ Codex CLI ให้จำชุดนี้ก่อน

codex
codex --help
codex --version

codex exec "..."
codex review --uncommitted
codex resume --last
codex doctor
codex update

codex mcp list

และภายใน TUI

/model
/permissions
/status
/review
/init
/diff
/compact

#26. Cheat Sheet

┌──────────────────────────────────────────────┐
│              CODEX CLI CHEAT SHEET           │
├──────────────────────────────────────────────┤
│ codex                  Interactive TUI        │
│ codex "prompt"         Start with prompt     │
│ codex exec "..."       Non-interactive run   │
│ codex review           Code review           │
│ codex resume --last    Resume session        │
│ codex fork --last      Fork session          │
│ codex doctor           Diagnose Codex        │
│ codex update           Update Codex          │
│ codex mcp list         List MCP servers      │
│ codex --search         Live web search       │
├──────────────────────────────────────────────┤
│ /model                 Select model          │
│ /permissions           Permission settings   │
│ /status                Session status        │
│ /review                Review changes        │
│ /init                  Create AGENTS.md      │
│ /diff                  Show Git diff         │
│ /compact               Compact context       │
│ /skills                Skills                │
│ /mcp                   MCP tools             │
│ /plugins               Plugins               │
│ /quit                  Exit                  │
└──────────────────────────────────────────────┘

#27. Best Practices

#1. เริ่มจาก Git repository ที่ clean

git status

ก่อนให้ Codex แก้โค้ด ควร commit หรือ stash งานเดิมก่อน

git add .
git commit -m "checkpoint before codex"

ทำให้ rollback ง่าย


#2. ใช้ AGENTS.md

อย่าให้ Codex เดา project convention เองทั้งหมด

ระบุให้ชัด

architecture
coding standards
test commands
lint commands
dependency rules
security restrictions

#3. สั่งให้ Codex test หลังแก้โค้ด

Prompt ที่ดี

Implement the feature.
Run the relevant tests after making changes.
Fix failures caused by your changes.
Do not weaken existing tests.

#4. ตรวจ /permissions

ก่อนใช้ agent กับ project สำคัญ

/permissions

เลือกสิทธิ์เท่าที่งานจำเป็น

หลักการที่ดีคือ

Least Privilege

ให้ agent เข้าถึงเฉพาะสิ่งที่ต้องใช้


#5. Review ก่อน commit

/review

หรือ

codex review --uncommitted

แล้วตรวจด้วยคนอีกครั้งก่อน merge


#6. ใช้ codex exec สำหรับ Automation

Interactive

codex

เหมาะกับ developer ที่ต้องการคุยและปรับงานทีละขั้น

Automation

codex exec "..."

เหมาะกับ CI/CD และ script


#28. สรุป

Codex CLI ไม่ได้เป็นเพียง chatbot ใน Terminal แต่เป็น AI coding agent ที่สามารถทำงานร่วมกับ repository, shell, Git, tests, MCP, plugins และ automation pipeline ได้

คำสั่งที่ควรเริ่มใช้ก่อนคือ

codex
codex exec
codex review
codex resume
codex doctor
codex mcp

ส่วนใน Interactive TUI ควรจำ

/model
/permissions
/status
/review
/init
/diff
/compact

สำหรับ project จริง ควรใช้ร่วมกับ

Git checkpoints
      +
AGENTS.md
      +
Restricted permissions
      +
Automated tests
      +
Codex review
      +
Human review

แนวทางนี้ทำให้ Codex ช่วยเพิ่มความเร็วในการพัฒนาได้ โดยยังคงการควบคุม ความสามารถในการย้อนกลับ และกระบวนการตรวจสอบคุณภาพของซอฟต์แวร์ไว้


#References