Codex Core Concepts | Beginners Tutorial
Before using Codex, it's important to understand several core concepts. These concepts form the foundation of how Codex works.
Prompt
You interact with Codex by sending Prompts, describing the tasks you want it to complete.
Prompt Working Loop
After you submit a Prompt, Codex works in the following loop:
- Call the language model to understand the task
- Execute operations indicated by the model output (read files, edit code, run commands)
- Feed operation results back to the model
- Loop execution until the task is complete or you cancel
Codex adopts an Agent loop working modeβthe model outputs operation instructions, operation results are fed back to the model, and this cycle repeats until the task is complete.
Principles of Effective Prompts
| Principle | Description | Example |
|---|---|---|
| Include verification steps | Codex outputs higher quality when it can verify its work | "Write a function with test cases to verify it handles empty lists" |
| Break down complex tasks | Small tasks are easier to test and review | "Step 1: Create the model; tell me when done before continuing to Step 2" |
| Provide context | Reference relevant files and images | "Referencing the style of src/auth.py, implement similar functionality" |
Codex performs best when processing small, focused tasks. Large tasks should be broken down into small steps and executed gradually.
Thread
Thread is a single task session: your Prompt plus subsequent model outputs and tool calls.
Thread Types
| Type | Runtime Environment | Characteristics |
|---|---|---|
| Local thread | Your machine (within sandbox) | Can read/write files, use existing tools, execute commands |
| Cloud thread | Cloud isolated environment | Clone repository to run, suitable for parallel tasks, delegate across devices |
Thread Usage Rules
Running threads can be concurrent, but note:
- Avoid two threads modifying the same file simultaneously
- Threads can be resumed later by continuing with another Prompt
- Long-running tasks may automatically compress context
Context
When you submit a Prompt, it includes context that Codex can useβreferences to relevant files and images.
Context Sources
- IDE extension: Automatically includes list of open files and selected text ranges
- Manual specification: Reference file paths or attach images in the Prompt
- Conversation history: Previous conversation content in the thread
Context Window
All information in a thread must fit within the model's context window.
Codex monitors and reports remaining space. When approaching the limit, you will be prompted.
Automatic Compact
For longer tasks, Codex may automatically compress context.
The compression mechanism summarizes relevant information, discards less important details, and frees up space to continue processing.
Managing Context
# Start new session to release context
/new
# View current context usage
/status
When Codex reports high context usage, consider starting a new session or reducing historical messages.
Sandbox
Sandbox is Codex's security isolation mechanism, preventing accidental modification of files outside the workspace.
Sandbox Modes
| Mode | File Modification | Network Access | Use Case |
|---|---|---|---|
| Read-only | Prohibited | Prohibited | Read-only analysis, code review |
| Workspace-write | Workspace only | Prohibited | Daily development (default) |
| Full-access | Allowed | Allowed | Fully trusted environment (use with caution) |
Approval Mechanism
Certain operations require your confirmation before execution:
- Executing Shell commands (especially rm, kill, etc.)
- Modifying or deleting files
- Accessing sensitive directories (such as ~/.ssh/, /etc/)
- Network requests
Setting Sandbox Mode
# Switch sandbox mode in CLI
codex --sandbox workspace-write
# Or specify in Prompt
"Analyze this code, don't modify any files"
Sandbox is the first line of defense in Codex's security strategy, ensuring AI operations don't exceed expected scope.
Approval Policy
Approval policy controls whether Codex requires confirmation before executing operations.
Policy Types
| Policy | Description | Behavior |
|---|---|---|
ask |
Ask every time | Request confirmation before sensitive operations (default) |
approve |
Auto-approve | Execute automatically without confirmation (use with caution) |
deny |
Auto-deny | Reject all operations that may have side effects |
Configuring Approval Policy
# ~/.codex/config.toml
approval_policy = "ask"
Summary
After understanding these core concepts, you can use Codex more effectively:
- Prompt: Clearly describe tasks, include verification steps
- Thread: Manage task sessions, note concurrency rules
- Context: Provide relevant context, monitor window usage
- Sandbox: Understand security mechanisms, choose appropriate modes
FAQ
Q: Should Prompts be in Chinese or English?
Codex supports multiple languages, but English usually works best. When using Chinese, ensure descriptions are detailed and explicit.
Q: How to check current thread status?
Use the /status command to view thread ID, context usage, and configuration information.
Q: What to do when context window is full?
Start a new session (/new), or let Codex automatically compress historical records.
Q: Can sandbox mode be switched dynamically?
Can be specified when starting CLI, or set default values in configuration file.
YouTip