# How to Generate Code Snippets Using the Codex Plugin for Claude Code

> Learn to generate code snippets with the Codex plugin for Claude Code. Use slash commands to send requests to Codex CLI and get instant code results without post-processing.

- Repository: [OpenAI/codex-plugin-cc](https://github.com/openai/codex-plugin-cc)
- Tags: how-to-guide
- Published: 2026-07-03

---

**The Codex plugin for Claude Code generates code snippets by delegating your natural-language requests to the local Codex CLI via slash-commands like `/codex:review` and `/codex:rescue`, returning raw output without post-processing.**

The `openai/codex-plugin-cc` repository implements a bridge between Claude Code and OpenAI’s Codex CLI, enabling you to generate code snippets directly within your editor workflow. When you invoke a Codex-specific slash-command, the plugin orchestrates a conversation with the local Codex binary, manages thread state, and streams the generated code back to your terminal. This integration leverages your existing Codex configuration and authentication, requiring no additional network setup beyond the locally installed CLI.

## How the Codex Plugin Generates Code

The plugin follows a strict five-phase pipeline to transform your natural-language prompt into executable code. Each phase is implemented in specific source files within the repository.

### Command Parsing and Invocation

When you type a slash-command such as `/codex:review` or `/codex:rescue`, Claude Code parses the input and invokes the companion script located at `plugins/codex/scripts/codex-companion.mjs`. Command definitions reside in `plugins/codex/commands/` (for example, [`review.md`](https://github.com/openai/codex-plugin-cc/blob/main/review.md) defines the review command), specifying how arguments are forwarded to the companion.

### Thread Initialization

The companion script calls `startThread` in `plugins/codex/scripts/lib/codex.mjs` (lines 332–349) to request a new conversation thread from the Codex app-server via the `thread/start` endpoint. This establishes a isolated context for your specific code-generation task.

### Prompt Construction and Submission

Your request is wrapped in a `<task>` XML block according to the prompting guide defined in [`plugins/codex/skills/gpt-5-4-prompting/SKILL.md`](https://github.com/openai/codex-plugin-cc/blob/main/plugins/codex/skills/gpt-5-4-prompting/SKILL.md). The function `runAppServerTurn` (lines 295–340 in `plugins/codex/scripts/lib/codex.mjs`) sends this payload to Codex via the `turn/start` endpoint. You can optionally include `--model` and `--effort` flags to control which model variant and reasoning depth Codex applies.

### Output Capture

While Codex executes, the plugin monitors events through `captureTurn` (lines 559–610 in `plugins/codex/scripts/lib/codex.mjs`). This helper captures file modifications, command executions, intermediate reasoning, and the final answer. The plugin buffers these events until the turn completes.

### Result Delivery

Once the turn finishes, the final message—containing the generated code snippet—is returned verbatim to Claude Code. The slash-command implementation streams the companion’s stdout directly to your terminal without transformation, ensuring you receive exactly what Codex produced.

## Practical Examples: Generating Code Snippets

You can generate code using three primary invocation patterns. Each delegates to the same underlying pipeline but offers different execution contexts.

Use the review command for simple, foreground generation where you want immediate results:

```bash
/codex:review "write a TypeScript function that flattens a nested object"

```

For long-running tasks that might take several minutes, use the rescue command with background execution. This runs the companion script with `run_in_background: true`, allowing you to continue working while the code generates:

```bash
/codex:rescue --background "write a Rust implementation of a fast heap queue"

```

When you need specific model capabilities or reasoning levels, pass the `--model` and `--effort` flags. These are forwarded to Codex but stripped from the natural-language prompt:

```bash
/codex:rescue --model gpt-5.4-mini --effort high "add a utility that converts ISO-8601 timestamps to Unix epoch"

```

All three methods return raw Codex output similar to this example:

```text
// Example snippet returned by Codex
export function flattenObject(obj: any): Record<string, any> {
  const result: Record<string, any> = {};
  function recurse(cur: any, prop: string) {
    if (Object(cur) !== cur) {
      result[prop] = cur;
    } else {
      for (const p in cur) recurse(cur[p], prop ? `${prop}.${p}` : p);
    }
  }
  recurse(obj, "");
  return result;
}

```

## Key Implementation Details

The plugin’s architecture relies on several critical components:

- **`plugins/codex/scripts/lib/codex.mjs`** – Contains the core library functions `startThread`, `runAppServerTurn`, and `captureTurn` that manage communication with the Codex app-server.
- **[`plugins/codex/commands/review.md`](https://github.com/openai/codex-plugin-cc/blob/main/plugins/codex/commands/review.md)** – Defines the `/codex:review` command for read-only code generation tasks.
- **[`plugins/codex/commands/rescue.md`](https://github.com/openai/codex-plugin-cc/blob/main/plugins/codex/commands/rescue.md)** – Defines the `/codex:rescue` command, the primary entry point for generating or fixing code via a sub-agent.
- **`plugins/codex/scripts/codex-companion.mjs`** – The CLI entry point that orchestrates the workflow between Claude Code and the Codex binary.
- **[`plugins/codex/skills/gpt-5-4-prompting/SKILL.md`](https://github.com/openai/codex-plugin-cc/blob/main/plugins/codex/skills/gpt-5-4-prompting/SKILL.md)** – Provides the XML-based prompt template that shapes how requests are presented to Codex.

Because the plugin reuses your local Codex installation (typically installed via `npm install -g @openai/codex`), it automatically respects configuration in `~/.codex/config.toml` and existing authentication tokens. No network requests are initiated by the plugin itself; all processing occurs within the Codex CLI binary.

## Summary

- **Slash-commands trigger delegation**: `/codex:review` and `/codex:rescue` parse your input and invoke `codex-companion.mjs` to handle the request.
- **Thread-based execution**: Each generation request creates a new Codex thread via `startThread` in `plugins/codex/scripts/lib/codex.mjs`.
- **XML-wrapped prompts**: Requests are wrapped in `<task>` blocks according to the prompting skill before submission via `runAppServerTurn`.
- **Raw output guarantee**: The plugin streams stdout directly from the companion script without modification, ensuring unaltered code delivery.
- **Local configuration**: The plugin inherits all settings from `~/.codex/config.toml` and uses your existing Codex CLI authentication.

## Frequently Asked Questions

### What is the difference between `/codex:review` and `/codex:rescue`?

The `/codex:review` command is optimized for read-only code generation and analysis tasks, while `/codex:rescue` serves as the primary entry point for generating new code or fixing existing implementations via a sub-agent. Both commands ultimately invoke the same companion script, but `rescue` supports additional flags like `--background` for asynchronous execution.

### How does the plugin handle authentication with the Codex API?

The plugin does not manage authentication directly. Instead, it relies on your local Codex CLI installation, which stores credentials and configuration in `~/.codex/config.toml`. When the companion script launches the Codex binary, it automatically inherits these settings, including any API tokens or organization preferences already configured.

### Can I customize the model used for code generation?

Yes. You can pass the `--model` flag followed by a model identifier (such as `gpt-5.4-mini`) to any generation command. Additionally, the `--effort` flag accepts values like `low`, `medium`, or `high` to control reasoning depth. These parameters are parsed by the command definitions and forwarded to the `runAppServerTurn` function, which includes them in the API request to Codex.

### Where does the plugin store configuration settings?

The plugin itself does not maintain separate configuration files. It defers entirely to the local Codex CLI configuration located at `~/.codex/config.toml`. This design ensures consistency between standalone Codex usage and Claude Code integration, preventing credential duplication or conflicting settings.