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 -e .
# or, if you use uv
uv pip install -e .

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

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.1.tar.gz (14.9 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.1-py3-none-any.whl (18.0 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: codex_client-0.0.1.tar.gz
  • Upload date:
  • Size: 14.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.11

File hashes

Hashes for codex_client-0.0.1.tar.gz
Algorithm Hash digest
SHA256 2287e3a04e4fc2d66a423a8a3393c67db14624c72798508f2348d122c7b75d8f
MD5 da6801eb1ba4bf7c9b153deadb9c61ed
BLAKE2b-256 756b643a7052a868158c820b712c638cbfbd4dae0e9267024aaeb2878e67276f

See more details on using hashes here.

File details

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

File metadata

  • Download URL: codex_client-0.0.1-py3-none-any.whl
  • Upload date:
  • Size: 18.0 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.11

File hashes

Hashes for codex_client-0.0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 316c595215e05c8eb991f17eb0d74bd5449046112c6a826b4796d192cf5f0907
MD5 559cb08438cebb26cee21c06e41603d3
BLAKE2b-256 fa1490b145379fa07384207b0ebfd6fbbeb1599fb64c38a4f832095d92c6b75d

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