# Referensi Cepat OpenAI Codex CLI

*Penggunaan CLI, mode, konfigurasi, perintah utama*

> Source: OpenAI Codex Documentation (github.com/openai/codex) · MIT

## Memulai

### Instalasi

```
npm install -g @openai/codex
```

### Autentikasi

```
export OPENAI_API_KEY="sk-..."
```

*Set di shell profile atau file .env*

### Pertama Kali Digunakan

```
codex "explain this project"
codex "add input validation to app.py"
```

## Perintah

### Interaktif (Default)

```
codex                       # start interactive session
codex "fix the login bug"   # with initial prompt
```

### codex exec

```
codex exec "write unit tests for utils.py"
codex exec "refactor db.py to use async"
```

*Non-interaktif — menjalankan task sampai selesai*

### codex review

```
codex review                # review staged changes
codex review --diff HEAD~3  # review last 3 commits
```

## Mode

### Mode Persetujuan

| Command | Description |
|---------|-------------|
| `suggest` | Tampilkan perubahan yang diusulkan, minta persetujuan untuk setiap edit file dan perintah |
| `auto-edit` | Terapkan edit file otomatis, minta persetujuan untuk perintah shell |
| `full-auto` | Terapkan edit dan jalankan perintah tanpa persetujuan |

### Mengatur Mode

```
codex --approval-mode suggest "add tests"
codex --approval-mode auto-edit "refactor"
codex --approval-mode full-auto "fix lint"
```

## Konfigurasi

### File Konfigurasi

```
# ~/.codex/config.yaml
model: o4-mini
approval_mode: suggest
providers:
  - name: openai
    api_key_env: OPENAI_API_KEY
```

*Override per-proyek: .codex/config.yaml di root proyek*

### Instruksi Proyek

```
# AGENTS.md (in project root)
- Run tests with: uv run pytest
- Use ruff for linting
- Never modify migration files
```

### Opsi Konfigurasi Berguna

| Command | Description |
|---------|-------------|
| `model` | Model yang digunakan (mis. `o4-mini`, `o3`) |
| `approval_mode` | Mode persetujuan default |
| `providers` | Konfigurasi provider API (key, base URL) |
| `history` | Simpan riwayat percakapan: `true` / `false` |

## Sandboxing

### Cara Kerjanya

> Codex menjalankan perintah di lingkungan sandbox
untuk mencegah efek samping yang tidak diinginkan.
Akses jaringan dinonaktifkan secara default. Penulisan
file dibatasi pada direktori proyek.

### Opsi Sandbox

| Command | Description |
|---------|-------------|
| `macOS` | Apple Seatbelt (default, bawaan) |
| `Linux` | Sandbox berbasis Docker |
| `--full-auto` | Memerlukan sandbox aktif |
| `--dangerously-auto-approve` | Lewati sandbox (tidak disarankan) |

## Tips

### Prompt yang Efektif

| Command | Description |
|---------|-------------|
| `Spesifik` | "add retry logic to fetch_data()" > "improve code" |
| `Referensikan file` | "fix the bug in src/auth.py" mempersempit cakupan |
| `Nyatakan batasan` | "don't change the public API" menetapkan batas |
| `Iterasi` | Lanjutkan dengan penyempurnaan dalam sesi yang sama |

### Pola Alur Kerja

```
# Explore → plan → execute
codex "explain the auth module"
codex "plan how to add OAuth support"
codex --approval-mode auto-edit "add OAuth"

# Review before commit
git add -A
codex review
```

### Flag Umum

| Command | Description |
|---------|-------------|
| `--model, -m` | Override model untuk sesi ini |
| `--approval-mode` | Set mode persetujuan |
| `--quiet, -q` | Output minimal |
| `--no-project-doc` | Abaikan AGENTS.md |
