Claude Code vs OpenAI Codex: Complete Guide — Installation, Commands & Real-World Examples
A side-by-side comparison of the two leading terminal-based AI coding agents in 2026 — covering real commands, how they work, and which tool fits which situation.
AI-assisted draft · Editorially reviewedThis blog content may use AI tools for drafting and structuring, and is published after editorial review by the Trensee Editorial Team.
Goal of this article: Not "which one is better?" — but showing you, with real commands and examples, how to install, run, and use both tools effectively.
Why Should You Learn Both Tools Right Now?
Claude Code (Anthropic) and OpenAI Codex CLI (OpenAI) are terminal-based AI coding agents that emerged in 2025–2026. Unlike IDE autocomplete tools such as GitHub Copilot, both are autonomous agents that read your entire codebase, directly edit files, and run tests.
They belong to the same category but operate with different philosophies. Understanding those differences at the command level lets you make the right choice for each situation.
Claude Code: Core Features and Commands
How Do You Install Claude Code?
# Install (requires Node.js 18+)
npm install -g @anthropic-ai/claude-code
# Set your API key
export ANTHROPIC_API_KEY="sk-ant-..."
# Check version
claude --version
# Start an interactive session
claude
# Start with current directory as context
cd my-project && claude
What Execution Modes Are Available?
| Mode | Command | Description |
|---|---|---|
| Interactive | claude |
Interactive session (exit with /exit) |
| One-shot | claude "prompt" |
Runs once and exits |
| Pipe mode | cat file | claude -p "..." |
Reads from stdin, writes to stdout |
| Resume last | claude --continue |
Resumes the most recent session |
| Resume by ID | claude --resume <id> |
Returns to a specific session |
# One-shot — complete a task in a single line
claude "Find and fix the JWT expiry bug in app/api/auth/route.ts"
# Pipe mode — use git diff for code review
git diff main | claude -p "Review these changes: security, performance, then readability"
# Analyze error logs
cat error.log | claude -p "Identify the root cause and suggest a fix"
Which Slash Commands Can You Use Inside a Session?
These commands are available within an interactive session (claude).
| Command | Purpose |
|---|---|
/init |
Analyze the current project and auto-generate CLAUDE.md |
/clear |
Reset context (use when starting a new task) |
/compact |
Compress the conversation — saves tokens in long sessions |
/cost |
Show token usage and estimated cost for the current session |
/model |
Switch model (Haiku ↔ Sonnet ↔ Opus) |
/memory |
Manage Auto-Memory — view and toggle saved context (v2.1.59+) |
/permissions |
View and change allowed tool list |
/help |
Show all available commands |
/btw [question] |
Ask a quick side question without adding it to conversation history (v2.1.72+) |
/vim |
Toggle Vim key bindings |
# Example usage inside a session
$ claude
> /init
✓ CLAUDE.md created — project structure analysis applied
> /cost
Session tokens: 12,400 input / 3,200 output | Estimated cost: $0.08
> /compact
✓ Conversation compressed — 40% context saved
> /btw What JWT library is being used in auth.ts right now?
↳ [Side question — not saved to history] Using jsonwebtoken 0.9.1
Key characteristics of /btw:
- Answers using the current context — cannot read new files or run commands
- The response is not added to conversation history, so it does not pollute context
- Can run in parallel even while Claude is working on a task
- Reuses the parent conversation's prompt cache, minimizing extra cost
Why Does the CLAUDE.md Configuration File Matter?
CLAUDE.md is a project-specific instruction file placed at the project root. Claude Code reads it automatically at session start and applies your coding rules, structure notes, and restrictions.
# CLAUDE.md example
## Stack
Next.js 16 App Router, TypeScript strict, Tailwind CSS, Drizzle ORM + Neon Postgres
## Coding Rules
- Functional components only (no class components)
- No `any` types — use unknown + type guards instead
- Component files: PascalCase, util files: camelCase
- All new API routes must apply rate-limit middleware
## Testing
- Test runner: `node --experimental-strip-types --test`
- Write unit test files alongside any new utility functions
## Restrictions
- Never modify `.env` files
- Do not commit with leftover `console.log` statements
- Do not cast `any` to bypass type errors
Without CLAUDE.md: You must explain project rules at the start of every session. With CLAUDE.md: Define project rules once — they apply automatically to every session.
How Do Skills Package Reusable Workflows? (v2.1+)
Skills let you package frequently used tasks into SKILL.md files for reuse. Place them under .claude/skills/ or ~/.claude/skills/ and they hot-reload immediately. From Claude Code 2.1 onward, they appear integrated with slash commands when you type /.
# .claude/skills/code-review.md
---
name: code-review
description: Review PR diff in order of security, performance, readability
user-invocable: true
---
Read `git diff HEAD~1` and review in this order:
1. Security vulnerabilities (injection, missing auth, exposed secrets)
2. Performance issues (N+1 queries, unnecessary re-renders)
3. Readability and maintainability
$ claude
> /code-review # Direct invocation inside a session (user-invocable: true)
> /commit # Analyze staged changes and generate a commit message
Two invocation modes for Skills:
| Mode | Frontmatter | Trigger |
|---|---|---|
| User-invocable | user-invocable: true |
Call directly with /skill-name |
| Auto-invocation | user-invocable: false |
Claude reads description and triggers automatically |
Adding context: fork runs the skill in a sub-agent isolated from the current session — protecting session tokens during long analysis tasks.
How Does Auto-Memory Preserve Context Across Sessions? (v2.1.59+)
Auto-Memory automatically saves what Claude learns during a session — build commands, debugging patterns, architecture notes, coding style preferences — and reuses that knowledge in future sessions. It is enabled by default.
$ claude
> /memory
📁 ~/.claude/projects/my-project/memory/MEMORY.md
- Build: pnpm build (Next.js 16)
- Test: node --experimental-strip-types --test
- Note: Never modify .env files (recorded: 2026-03-15)
[auto-memory toggle: ON]
CLAUDE.md vs Auto-Memory — how they differ:
| CLAUDE.md | Auto-Memory | |
|---|---|---|
| Written by | Developer (manual) | Claude (automatic) |
| Content | Coding rules, restrictions | Build commands, debugging insights |
| Sharing scope | Entire team (git-trackable) | Personal session context |
| Control | Manual editing | Toggle with /memory command |
What Are Hooks and How Do They Automate Your Workflow? (2026)
Hooks connect shell commands to Claude Code events. Configure them in .claude/settings.json.
// .claude/settings.json
{
"hooks": {
"PostToolUse": [{
"matcher": "Edit|Write",
"hooks": [{ "type": "shell", "command": "pnpm run lint --fix" }]
}],
"Stop": [{
"hooks": [{ "type": "shell", "command": "echo 'Session ended' >> .claude/session.log" }]
}]
}
}
Key hook events: PreToolUse (before tool runs) | PostToolUse (after tool runs) | PreCompact (before compression) | Stop (session end)
→ This enables CI/CD-level automation: run the linter automatically every time AI edits a file, trigger type checks before commits, and more. Claude Code supports 12 hook events in total.
Real-World Command Examples
1. Autonomous bug-fix loop
claude "Find the JWT expiry bug in app/api/auth/route.ts, fix it,
then add 3 failing-case tests and keep running until all pass"
Claude Code autonomously cycles through: read files → identify bug → fix → run tests → re-fix on failure — until done, without human intervention.
2. AI-generated commit message
git add -p # Stage your changes
claude commit # Analyze staged changes and generate a commit message
3. Large-scale refactoring
claude "Convert all Promise chains (.then/.catch) in the lib/ directory
to async/await. Verify TypeScript types remain intact throughout."
4. Codebase onboarding
claude "Explain the authentication flow in this project step-by-step
for a developer seeing it for the first time, with file paths."
What Are the Key Flags?
# Restrict to read-only tools — analysis with no file modifications
claude --allowedTools "Read,Grep,Glob" "Scan for security vulnerabilities only"
# Autonomous execution without confirmations (use with caution)
claude --dangerouslySkipPermissions "Generate all test files in bulk"
# Specify a model
claude --model claude-opus-4-6 "Help me design a complex system"
claude --model claude-haiku-4-5-20251001 "Write a simple utility function"
OpenAI Codex: Core Features and Commands
How Do You Install OpenAI Codex CLI?
# Install (MIT open source, Node.js 22+ recommended)
npm install -g @openai/codex
# Set your API key
export OPENAI_API_KEY="sk-..."
# Check version
codex --version
# Start an interactive session
codex
# One-shot execution
codex "Add accessibility aria attributes to components/Button.tsx"
What Is Approval Mode?
This is Codex's key differentiator. You can set the user approval level for execution in 3 stages.
# suggest mode (default): confirms before every change
codex --approval-mode suggest "Refactor this module"
# auto-edit mode: file edits are auto-approved, shell commands require confirmation
codex --approval-mode auto-edit "Fix all TypeScript errors"
# full mode: file edits and shell commands both run automatically (use carefully)
codex --approval-mode full "Run tests automatically and fix failures"
| Mode | File edits | Shell commands | Recommended for |
|---|---|---|---|
suggest |
Confirm first | Confirm first | Unfamiliar codebases, production |
auto-edit |
Automatic | Confirm first | Everyday development (recommended default) |
full |
Automatic | Automatic | Test environments, CI/CD |
Which Workflows Does Codex Suit Best?
Codex uses a sandboxed execution environment by default. Shell commands run with restricted network access and isolated disk writes. This makes it a strong fit for security-sensitive enterprise environments.
# Multimodal — pass a screenshot
codex "Write a test that reproduces the UI bug in this screenshot" \
--image ./screenshots/bug-report.png
# Quiet mode — use in scripts and pipes
cat legacy-code.js | codex -q "Convert this code to TypeScript"
Real-World Command Examples
1. Function refactoring
codex --approval-mode auto-edit \
"Consolidate the 3 useState hooks in components/Header.tsx into a single useReducer"
2. Automated test generation
codex "Write Vitest unit tests for all public functions in lib/geo-probe.ts.
Include edge cases."
3. Generate a GitHub Actions workflow
codex "Write a CI workflow .github/workflows/ci.yml for this project
(Next.js, pnpm, Node 22). Order: lint → typecheck → test → build"
4. Code explanation (pipe mode)
cat lib/geo-check-scoring.ts | codex -q \
"Explain this scoring logic in plain English without inline comments"
Codex Project Configuration (AGENTS.md)
AGENTS.md is Codex's equivalent of Claude Code's CLAUDE.md — a project instruction file for the agent.
# AGENTS.md
## Project
Next.js 16, TypeScript strict, pnpm
## Rules
- Do not change existing code style
- Use Vitest for all tests
- Write PR descriptions in English
How Do Agent Skills Package Workflows?
Codex also packages repetitive tasks using SKILL.md. They work identically across the CLI, IDE extension, and the Codex app. A community skills index is publicly available at github.com/openai/skills.
# .codex/skills/test-coverage.md
---
name: test-coverage
description: Auto-generate Vitest tests for files with low coverage
---
Find files in lib/ that have no tests or coverage below 70%,
then write Vitest unit tests including edge cases.
codex /test-coverage # Invoke the skill directly
Are Multi-Agent Workflows Supported?
Multi-agent workflows are well-suited for large tasks that can be parallelized. Enable them with /experimental.
# Enable multi-agent inside a session
> /experimental
# CSV-based parallel fan-out — process multiple modules simultaneously via sub-agents
codex "Refactor each module in tasks.csv using parallel agents"
Using spawn_agents_on_csv, you can fan out tasks from a CSV list and track progress and ETA on a single screen. Sub-agents can be given nicknames so you can tell which agent is handling which task.
SWE-bench Verified (codex-1): 72.1% (1 attempt) / 83.8% (8 attempts)
What New Features Did 2026 Bring to Codex?
| Feature | Details |
|---|---|
| GPT-5.4 support | Specify --model gpt-5.4 or use it as the default model |
| Codex-Spark | Research preview (ChatGPT Pro only), 128K context window |
| Fast mode on by default | Live Fast/Standard mode indicator in TUI header |
| Voice input | Hold spacebar → record and transcribe (experimental: features.voice_transcription = true) |
| Windows native | PowerShell + Windows Sandbox (no WSL required) |
| /theme picker | Live preview of light/dark theme inside a session |
| TUI code highlighting | Syntax highlighting for fenced code blocks and diffs |
How Do Both Tools Handle the Same Task Differently?
Scenario 1: Bug Fix + Autonomous Test Loop
Claude Code:
claude "Fix the session expiry bug in app/api/auth/route.ts,
add 3 failing-case tests, and keep iterating until all pass"
→ Autonomous loop: reads files → identifies bug → fixes → runs tests → re-fixes on failure. Runs to completion with no human input.
OpenAI Codex:
# Step 1: Fix the bug (you can review the diff for each file change)
codex --approval-mode auto-edit \
"Fix session expiry bug in app/api/auth/route.ts"
# Step 2: Add tests
codex --approval-mode auto-edit \
"Add 3 failing-case tests to auth.test.ts"
# Step 3: Run tests (shell commands require confirmation)
codex --approval-mode full "pnpm test auth"
→ Step-by-step control: review and approve/reject each file diff. More control at each stage.
Scenario 2: Code Review
Claude Code:
git diff main | claude -p "Review this PR.
List issues in order: security vulnerabilities, performance, readability."
OpenAI Codex:
git diff main | codex -q "Review this PR.
List issues in order: security vulnerabilities, performance, readability."
Both tools work equally well in pipe mode for this workflow.
How Do the Commands Compare Side by Side?
| Task | Claude Code | OpenAI Codex |
|---|---|---|
| Autonomous bug fix | claude "find and fix, run until tests pass" |
codex --approval-mode full "..." |
| Read-only exploration | claude --allowedTools "Read,Grep,Glob" |
codex --approval-mode suggest |
| Pipe input | cat f | claude -p "..." |
cat f | codex -q "..." |
| AI commit message | claude commit |
Not supported (manual) |
| Resume session | claude --resume <id> |
Session history (codex --history) |
| Project instructions | CLAUDE.md |
AGENTS.md |
| Skills | /skill-name (user-invocable + auto-invocation) |
/skill-name (Agent Skills) |
| Session memory | Auto-Memory (auto-save) + /memory |
None (AGENTS.md manual only) |
| Hook automation | PostToolUse → auto lint/test | Not supported |
| Multi-agent | context: fork sub-agent fork |
/experimental (experimental) |
| Voice input | Not supported | Experimental (hold spacebar) |
| Model selection | --model claude-sonnet-4-6 |
--model gpt-5.4 or codex-spark |
Which Tool Should You Choose and When?
| Situation | Recommendation | Reason |
|---|---|---|
| Understanding a large, unfamiliar codebase | Claude Code | 1M context window, autonomous multi-file editing |
| Autonomous bug-fix loop | Claude Code | Runs to completion without interruption |
| Reviewing diffs before every change | Codex | Step-by-step approval with --approval-mode |
| Security-isolated execution environment | Codex | Sandboxed by default, network restricted |
| Open-source tool or self-modification needed | Codex | MIT license |
| Automated AI commit messages | Claude Code | claude commit built in |
| Image / screenshot input | Codex | Multimodal --image support |
| Team already using Claude API | Claude Code | Same API key, unified cost management |
| Accumulating context across sessions | Claude Code | Auto-Memory enabled by default |
| CI/CD automation pipeline | Claude Code | Hooks connect lint/test automatically |
| Large parallel code tasks | Codex | Multi-agent spawn_agents_on_csv |
| Windows native environment | Codex | PowerShell sandbox (no WSL needed) |
Quick Reference Summary
| Claude Code | OpenAI Codex CLI | |
|---|---|---|
| Install | npm i -g @anthropic-ai/claude-code |
npm i -g @openai/codex |
| API key | ANTHROPIC_API_KEY |
OPENAI_API_KEY |
| Default model | Claude Sonnet 4.6 | GPT-5.4 / Codex-Spark |
| Execution control | --allowedTools / --dangerouslySkipPermissions |
--approval-mode suggest/auto-edit/full |
| Project instructions | CLAUDE.md |
AGENTS.md |
| Skills | SKILL.md (user-invocable + auto-invocation) | SKILL.md (Agent Skills) |
| Auto-Memory | Enabled by default (v2.1.59+) | Not available |
| Hooks | 12 events (PostToolUse, etc.) | Not available |
| Multi-agent | context: fork |
/experimental |
| Commit support | claude commit |
Not supported |
| Sandbox | Optional | Default |
| Open source | No | Yes (MIT) |
| Voice input | Not supported | Experimental |
| Pricing model | Anthropic API usage | OpenAI API usage |
FAQ
Q1. What Are the Most Common Errors When Installing Claude Code?▾
A: Two issues account for most cases. ① Node.js version — installation fails on Node below 18. Run node --version and upgrade to 18+. ② Missing API key — if the ANTHROPIC_API_KEY environment variable is not set, running claude produces an authentication error. Run export ANTHROPIC_API_KEY="sk-ant-..." and add it to your .bashrc or .zshrc.
Q2. When Should You Use the --dangerouslySkipPermissions Flag?▾
A: This flag allows Claude to modify files and run shell commands without any confirmation. Use it only in CI/CD pipelines or isolated test environments. Using it directly on a production codebase risks unintended file deletions and edits.
Q3. How Should You Write a Good CLAUDE.md File?▾
A: Use the /init command — Claude Code analyzes your current project and generates one automatically. Add or adjust content from there. The three essentials are ① tech stack ② restrictions ③ how to run tests. Keep it concise; an overly long CLAUDE.md wastes context.
Q4. Which Codex Approval Mode Should You Use by Default?▾
A: auto-edit mode is recommended. File edits proceed automatically, but shell commands (test runs, package installs) still require confirmation. It provides the best balance of speed and safety for most development work. suggest is too slow; full runs shell commands without confirmation, which can be risky.
Q5. Does It Make Sense to Use Both Tools Together?▾
A: Yes. In practice, many teams use Claude Code for large-scale refactoring, codebase comprehension, and autonomous bug fixing, while using Codex in CI as a sandboxed environment for security-sensitive tasks. Running both means paying for both Anthropic and OpenAI APIs, so it is worth running a pilot to measure the actual productivity gains before committing.
Q6. How Can You Reduce Token Costs?▾
A: In Claude Code: ① Use /compact to compress long sessions mid-way ② Use /clear to reset context when starting a new task ③ Use --model claude-haiku-4-5-20251001 for simpler tasks ④ Use --allowedTools "Read,Grep" to limit Claude to only the tools you need. Monitor costs in real time with the /cost command.
Q7. How Should You Verify AI-Modified Code?▾
A: Three steps are recommended. ① git diff — review changes directly before and after modification ② Run your test suite — confirm existing tests still pass ③ Code review — AI-modified code should still go through human review. Cross-checking AI code with AI is also effective: git diff | claude -p "Review these changes from a security perspective".
Q8. Is It Safe to Use These Tools in Security-Sensitive Environments?▾
A: Both tools send code to cloud APIs. Always check each service's data handling policy — specifically whether your code is used for training and how long it is retained. Never include files with API keys or secrets (.env, credentials.json) in prompts. Codex offers a sandboxed execution environment by default, making it relatively safer in terms of command execution isolation. For the highest-security environments, consider on-premises open-source models (Code Llama, Codestral).
Q9. What Is the Difference Between Skills and CLAUDE.md?▾
A: They serve different purposes. CLAUDE.md contains always-on project instructions — coding rules and restrictions that apply to everything, every session. Skills are on-demand task units — things like code review, commit message generation, or test writing that you only invoke when needed. You can call them manually with /skill-name, or set user-invocable: false with a clear description so Claude invokes them automatically based on context.
Q10. Does Auto-Memory Store Sensitive Information?▾
A: Auto-Memory stores workflow context — build commands, test procedures, architecture notes. Sensitive data like API keys and passwords is only stored in Auto-Memory if you explicitly include it in CLAUDE.md or your prompts. Use /memory to view or delete saved content at any time. You can also disable Auto-Memory entirely by setting autoMemoryEnabled: false in .claude/settings.json.
Q11. When Should You Use the /btw Command?▾
A: /btw [question] is a "By The Way" side question command added in v2.1.72. It lets you ask a quick question while keeping the current context intact — the answer is not added to conversation history, so it does not affect Claude's ongoing work. For example, while Claude is in the middle of a refactoring task, you can type /btw Which file contains the function being changed right now? without disrupting the flow. Note that /btw cannot access tools like file reading or command execution — it can only answer based on what is already in context. If you need Claude to look something up, use a regular prompt instead.
Related Terms (Glossary)
Further Reading
- This Week in AI Signals: The Fallout from the "90% AI-Written Code" Prediction
- Developer Survival Strategies in the AI Era: 5 Transitions to Start Right Now
- When 90% of Code Is Written by AI: How Will Developers Survive?
- The Evolution of AI Coding Assistants: From Autocomplete to Autonomous Agents
About This Article
This article was written in March 2026 based on the Anthropic Claude Code official documentation and the OpenAI Codex CLI GitHub README. Both tools update rapidly — new features will be reflected as they are released.
References
Execution Summary
| Item | Practical guideline |
|---|---|
| Core topic | Claude Code vs OpenAI Codex: Complete Guide — Installation, Commands & Real-World Examples |
| Best fit | Prioritize for tools workflows |
| Primary action | Standardize an input contract (objective, audience, sources, output format) |
| Risk check | Validate unsupported claims, policy violations, and format compliance |
| Next step | Store failures as reusable patterns to reduce repeat issues |
Data Basis
- Sources: Anthropic Claude Code official documentation and OpenAI Codex CLI GitHub (github.com/openai/codex) official README and release notes
- Verification principle: Only verified, executable commands, flags, and examples are included. Features with uncertainty have their scope and limitations noted
Key Claims and Sources
Claim:Claude Code is installed via npm install -g @anthropic-ai/claude-code and requires the ANTHROPIC_API_KEY environment variable
Source:Anthropic: Claude Code Official DocumentationClaim:OpenAI Codex CLI is an MIT-licensed open-source terminal agent that controls autonomous execution level via the --approval-mode flag
Source:OpenAI Codex CLI GitHub README
External References
Have a question about this post?
Sign in to ask anonymously in our Ask section.