Skip to main content

Claude Code SDK-style wrapper around the Codex CLI for programmatic automation.

Project description

Codex Client (Unofficial)

Codex Client is a lightweight, community-maintained Python wrapper around the Codex CLI. It lets you launch chats, stream reasoning and tool events, and drive MCP servers from your own applications—similar to how the claude-code-sdk works for Anthropics' Claude tools, but tailored for Codex.

This package is not an official OpenAI release. Expect the API surface to evolve as the Codex CLI changes.

Installation

# clone this repository, then install in a virtual environment
pip install codex-client
# or, if you use uv
uv pip install codex-client

Ensure the codex executable is on your PATH, since the client shells out to codex mcp serve under the hood.

Authentication quick start

The bundled CLI wraps the official Codex login helper so you can capture and reuse credentials safely.

# Launch the Codex login flow, witness the browser open, and wait for success
codex-client login

# Prefer to launch the browser yourself? Pass --no-browser and follow the prompt
codex-client login --no-browser

# See the stored payload (compressed base64) and copy it to another machine
codex-client read

# Import the copied payload into a fresh environment
codex-client set "<payload>"

# Clear local credentials when finished
codex-client logout

On the receiving machine you can also drop straight into Python:

from codex_sdk.auth import CodexAuth

auth = CodexAuth(codex_command="codex-client")
auth.set("<payload-from-read>")
# later, confirm or refresh as needed
token = auth.read()

Core Concepts

  • Client lifecyclecodex_sdk.Client manages the background MCP session. Use it as an async context manager to guarantee clean startup and teardown.
  • Chatscodex_sdk.Chat represents an ongoing conversation. Iterate over it for raw events, call await chat.get() for the final assistant reply, and await chat.resume(...) to continue the dialogue.
  • ConfigurationCodexChatConfig, CodexProfile, and CodexMcpServer (in codex_sdk.config) serialize options Codex expects: models, sandboxing, approval policy, working directory, environment overrides, MCP servers, and more.
  • Structured streaming – Helpers in codex_sdk.structured (structured, AssistantMessageStream, ReasoningStream, CommandStream) aggregate low-level deltas into convenient async streams.
  • Events & errors – All event dataclasses live in codex_sdk.event; exceptions (CodexError, ChatError, ToolError, etc.) live in codex_sdk.exceptions so you can handle failures precisely.

Usage Example

import asyncio
from codex_sdk import (
    Client,
    CodexChatConfig,
    CodexProfile,
    CodexMcpServer,
    ReasoningEffort,
    SandboxMode,
    Verbosity,
)
from codex_sdk.structured import structured, AssistantMessageStream, ReasoningStream, CommandStream

async def run(prompt: str) -> None:
    config = CodexChatConfig(
        profile=CodexProfile(
            model="gpt-5",
            reasoning_effort=ReasoningEffort.MINIMAL,
            verbosity=Verbosity.HIGH,
            sandbox=SandboxMode.DANGER_FULL_ACCESS,
        ),
        mcp_servers=[
            CodexMcpServer(
                name="context7",
                command="npx",
                args=["-y", "@upstash/context7-mcp", "--api-key", "<api_key>"]
            )
        ],
    )

    async with Client() as client:
        chat = await client.create_chat(prompt, config=config)

        async for event in structured(chat):
            if isinstance(event, AssistantMessageStream):
                async for chunk in event.stream():
                    print(chunk, end="", flush=True)
                print("\n[assistant message complete]")
            elif isinstance(event, ReasoningStream):
                async for chunk in event.stream():
                    print(f"[reasoning] {chunk}")
            elif isinstance(event, CommandStream):
                async for chunk in event.stream():
                    if chunk.text:
                        print(f"[command {event.command}] {chunk.text}")

        final_reply = await chat.get()
        print("Final reply:", final_reply)

        await chat.resume("Thanks! Any closing thoughts?")

asyncio.run(run("Introduce yourself."))

This pattern illustrates how to:

  • Bootstrap a Codex profile, sandbox policy, and optional MCP servers.
  • Open a chat, stream assistant output, reasoning traces, and command execution in real time.
  • Retrieve the final assistant response and continue the conversation with chat.resume().

Extending Codex Client

  • Inject your own MCP servers or tools by modifying the CodexChatConfig you pass to Client.create_chat.
  • Capture richer telemetry (token counts, command durations, event payloads) by iterating the raw chat events instead of the structured helper.
  • Integrate Codex Client into automation (FastAPI endpoints, Slack bots, GitHub Actions) so Codex handles the heavy lifting while you orchestrate workflows.

Bug reports and contributions are welcome—the codebase stays intentionally small so you can adapt it quickly.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

codex_client-0.0.2.tar.gz (20.3 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

codex_client-0.0.2-py3-none-any.whl (24.4 kB view details)

Uploaded Python 3

File details

Details for the file codex_client-0.0.2.tar.gz.

File metadata

  • Download URL: codex_client-0.0.2.tar.gz
  • Upload date:
  • Size: 20.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for codex_client-0.0.2.tar.gz
Algorithm Hash digest
SHA256 1fa8f0630effca6399f0691b152e1c8b48f1de7cbb39ee7db47c60b3cda88a0e
MD5 256cd6c6cc4843878e31ca0234cb1f39
BLAKE2b-256 adb09a70bca657be3ac33d4981ee801bdb870527f377c78bfd78ed0581e6ee57

See more details on using hashes here.

File details

Details for the file codex_client-0.0.2-py3-none-any.whl.

File metadata

  • Download URL: codex_client-0.0.2-py3-none-any.whl
  • Upload date:
  • Size: 24.4 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for codex_client-0.0.2-py3-none-any.whl
Algorithm Hash digest
SHA256 5c2559848348ca349b8b01ff1d038e79a8486990590e85fbcfa34c3f4d656dd9
MD5 24892bb282a8d7382c83da89af99ad1c
BLAKE2b-256 211d063f0613b3a09dd4dc76d656b4eedfd3b51a34769371d80523c0e6ec5cd5

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page