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_client.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_client.Client manages the background MCP session. Use it as an async context manager to guarantee clean startup and teardown.
  • Chatscodex_client.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_client.config) serialize options Codex expects: models, sandboxing, approval policy, working directory, environment overrides, MCP servers, and more.
  • Structured streaming – Helpers in codex_client.structured (structured, AssistantMessageStream, ReasoningStream, CommandStream) aggregate low-level deltas into convenient async streams.
  • Events & errors – All event dataclasses live in codex_client.event; exceptions (CodexError, ChatError, ToolError, etc.) live in codex_client.exceptions so you can handle failures precisely.

Usage Example

import asyncio
from codex_client import (
    Client,
    CodexChatConfig,
    CodexProfile,
    CodexMcpServer,
    ReasoningEffort,
    SandboxMode,
    Verbosity,
)
from codex_client.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.3.tar.gz (22.8 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.3-py3-none-any.whl (24.6 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: codex_client-0.0.3.tar.gz
  • Upload date:
  • Size: 22.8 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.3.tar.gz
Algorithm Hash digest
SHA256 67953ad3a2d7af239391a4bb38682134dbe1336e02d029121db2601794914b81
MD5 7cc13e5e41cf483eb6c36fad514f09be
BLAKE2b-256 ec71dc8da21f367aa541d8e7290bfe0d3fcb88f62c67abb64d8ca27b2517e5d5

See more details on using hashes here.

File details

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

File metadata

  • Download URL: codex_client-0.0.3-py3-none-any.whl
  • Upload date:
  • Size: 24.6 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.3-py3-none-any.whl
Algorithm Hash digest
SHA256 30231a5ab1964f2d2d08b0bfc7c2cc86d375d033ba0c65b9cafeedfd2b46236a
MD5 638bfddabb7e2f10b4ae8fc6e3d8cf40
BLAKE2b-256 f73213081b48892df620252b8aad891156cc2928b58101ff8508ccdb1888d02a

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