Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

Codex Core Concepts

Codex Core Concepts

Before using Codex, it is important to understand a few core concepts. These concepts form the foundation of how Codex works.


Prompt

You interact with Codex by sending a Prompt describing the task you want it to complete.

The Prompt work loop

After you submit a Prompt, Codex works in the following loop:

  1. Call the language model to understand the task
  2. Execute the actions indicated by the model's output (read files, edit code, run commands)
  3. Feed the results of the actions back to the model
  4. Repeat until the task is complete or you cancel

Codex uses a Agent Loopmode of operation—the model outputs instructions for actions, the results are fed back to the model, and the cycle repeats until the task is complete.

Principles of an effective Prompt

Principle Description Example
Include verification steps Codex produces higher-quality output when it can verify its work "Write a function, including test cases that verify it handles an empty list"
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 "Following the style of src/auth.py, implement a similar feature"

Codex performs best on small, focused tasks. Large tasks should be broken into small steps and executed incrementally.


Thread

A Thread is a single task session: your Prompt plus the subsequent model outputs and tool calls.

Thread types

Type Runtime environment Feature
Local threads Your machine (inside the sandbox) Can read/write files, use existing tools, and execute commands
Cloud threads Isolated cloud environment Clone a repo to run, suitable for parallel tasks, cross-device delegation

Thread usage rules

Running threads can be concurrent, but note the following:


Context

When you submit a Prompt, include context that Codex can use—references to relevant files and images.

Context sources

Context window

All information in a thread must fit within the model's context window.

Codex monitors and reports remaining space. When you approach the limit, you will be notified.

Automatic Compact

For longer tasks, Codex may automatically compact the context.

Compaction summarizes relevant information, discards less important details, and frees up space to continue.

Manage context

Start a new session to free up context

/

new

View current context usage

/

status

When Codex reports high context usage, consider starting a new session or reducing history messages.


Sandbox

Sandbox is Codex's security isolation mechanism that prevents accidental modification of files outside the workspace.

Sandbox mode

Mode File modification Network access Applicable scenarios
Read-only Forbid Forbid Read-only analysis, code review
Workspace-write Workspace only Forbid Daily development (default)
Full-access Allowed Allowed Fully trusted environments (use with caution)

Approval mechanism

Certain operations require your confirmation before they can execute:

Set the sandbox mode

Switch sandbox mode in the CLI

codex

--sandbox

workspace-write

or specify it in the Prompt

"Analyze this code, do not modify any files"

Sandbox is the first line of defense in Codex's security policy, ensuring AI operations stay within expected bounds.


Approval Policy

The approval policy controls whether Codex needs confirmation before executing actions.

Strategy type

Strategy Description Behavior
ask Ask every time Request confirmation before sensitive operations (default)
approve Auto-approve Execute automatically without confirmation (use with caution)
deny Automatically reject Reject all operations that may have side effects

Configure the approval policy

~/.codex/config.toml

approval_policy = "ask"


Summary

After understanding these core concepts, you can use Codex more effectively:


Common questions

Q: Should Prompts be written in Chinese or English?

Codex supports multiple languages, but English usually works best. When using Chinese, make sure the description is detailed and clear.

Q: How do I view the current thread status?

Use /status The command shows the thread ID, context usage, and configuration information.

Q: What if the context window is full?

Start a new session (/new), or have Codex automatically compact the history.

Q: Can sandbox mode be switched dynamically?

You can specify it at CLI startup or set a default in the configuration file.