Skip to main content

Minimal Python API for running the Codex CLI.

Project description

CodexAPI

Use OpenAI's codex from python as easily as calling a function with your codex credits instead of the API.

Note: this project is not affiliated with OpenAI in any way. Thanks for the awesome tools and models though!

Requirements

  • Codex CLI installed and authenticated (codex must be on your PATH).
  • Python 3.8+.

Install

pip install codexapi

Quickstart

from codexapi import agent, Agent, Task

# Run one-shot tasks as a function call
print(agent("Say hello"))

# Run a multi-turn conversation as a session
session = Agent(cwd="/path/to/project")
print(session("Summarize this repo."))
print(session("Now list any risks."))

# Save and resume a session later
thread_id = session.thread_id
session2 = Agent(cwd="/path/to/project", thread_id=thread_id)
print(session2("Continue from where we left off."))

# Define a task with a checker
class RepoTask(Task):
    def check(self):
        # Return an error string if something is wrong, or None/"" if OK
        return None

task = RepoTask("Summarize this repo.", cwd="/path/to/project")
result = task()
print(result.success, result.summary)

CLI

After installing, use the codexapi command:

codexapi "Summarize this repo."
codexapi --cwd /path/to/project "Fix the failing tests."
echo "Say hello." | codexapi

Task mode exits with code 0 on success and 1 on failure, printing the summary.

Show running sessions and their latest activity:

codexapi top

Press h for keys.

Resume a session and print the thread id to stderr:

codexapi --thread-id THREAD_ID --print-thread-id "Continue where we left off."

API

agent(prompt, cwd=None, yolo=False, flags=None) -> str

Runs a single Codex turn and returns only the agent's message. Any reasoning items are filtered out.

  • prompt (str): prompt to send to Codex.
  • cwd (str | PathLike | None): working directory for the Codex session.
  • yolo (bool): pass --yolo to Codex when true.
  • flags (str | None): extra CLI flags to pass to Codex.

Agent(cwd=None, yolo=False, thread_id=None, flags=None)

Creates a stateful session wrapper. Calling the instance sends the prompt into the same conversation and returns only the agent's message.

  • __call__(prompt) -> str: send a prompt to Codex and return the message.
  • thread_id -> str | None: expose the underlying session id once created.
  • yolo (bool): pass --yolo to Codex when true.
  • flags (str | None): extra CLI flags to pass to Codex.

task(prompt, check=None, n=10, cwd=None, yolo=False, flags=None) -> str

Runs a task with checker-driven retries and returns the success summary. Raises TaskFailed when the maximum attempts are reached.

  • check (str | None | False): custom check prompt, default checker, or False to skip.
  • n (int): maximum number of retries after a failed check.

task_result(prompt, check=None, n=10, cwd=None, yolo=False, flags=None) -> TaskResult

Runs a task with checker-driven retries and returns a TaskResult without raising TaskFailed.

Task(prompt, max_attempts=10, cwd=None, yolo=False, thread_id=None, flags=None)

Runs a Codex task with checker-driven retries. Subclass it and implement check() to return an error string when the task is incomplete, or return None/"" when the task passes.

  • __call__() -> TaskResult: run the task.
  • set_up(): optional setup hook.
  • tear_down(): optional cleanup hook.
  • check() -> str | None: return an error description or None/"".
  • on_success(result): optional success hook.
  • on_failure(result): optional failure hook.

TaskResult(success, summary, attempts, errors, thread_id)

Simple result object returned by Task.__call__.

  • success (bool): whether the task completed successfully.
  • summary (str): agent summary of what happened.
  • attempts (int): how many attempts were used.
  • errors (str | None): last checker error, if any.
  • thread_id (str | None): Codex thread id for the session.

TaskFailed

Exception raised by task() when retries are exhausted.

  • summary (str): failure summary text.
  • attempts (int | None): attempts made when the task failed.
  • errors (str | None): last checker error, if any.

Behavior notes

  • Uses codex exec --json and parses JSONL events for agent_message items.
  • Automatically passes --skip-git-repo-check so it can run outside a git repo.
  • Passes --full-auto unless --yolo is enabled.
  • Passes --yolo when enabled (use with care).
  • Raises RuntimeError if Codex exits non-zero or returns no agent message.

Configuration

Set CODEX_BIN to point at a non-default Codex binary:

export CODEX_BIN=/path/to/codex

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

codexapi-0.1.7.tar.gz (16.6 kB view details)

Uploaded Source

Built Distribution

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

codexapi-0.1.7-py3-none-any.whl (16.5 kB view details)

Uploaded Python 3

File details

Details for the file codexapi-0.1.7.tar.gz.

File metadata

  • Download URL: codexapi-0.1.7.tar.gz
  • Upload date:
  • Size: 16.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for codexapi-0.1.7.tar.gz
Algorithm Hash digest
SHA256 cdd7319f7bfcfe02c3452556f526939b2876f254c9d16039aaba0fa663f73fa9
MD5 d380a9f6b02dbb20ba6c6a38940ac37b
BLAKE2b-256 5c61fddcf211c7f84b7adce98fe7d5e544f5ce13a5e3ae05ea5f134c9eaf3b10

See more details on using hashes here.

Provenance

The following attestation bundles were made for codexapi-0.1.7.tar.gz:

Publisher: publish.yml on yieldthought/codexapi

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file codexapi-0.1.7-py3-none-any.whl.

File metadata

  • Download URL: codexapi-0.1.7-py3-none-any.whl
  • Upload date:
  • Size: 16.5 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for codexapi-0.1.7-py3-none-any.whl
Algorithm Hash digest
SHA256 565a27ebda1b6fe0245ce6d5c68565b40c6889b8455e525efecdf0329c558eff
MD5 b3c060349df06a81a08a43a9be575c69
BLAKE2b-256 0f261c7fbf1166f8a1f42d4ada18d1e37fd1d990b77a4329616f307a966c7848

See more details on using hashes here.

Provenance

The following attestation bundles were made for codexapi-0.1.7-py3-none-any.whl:

Publisher: publish.yml on yieldthought/codexapi

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

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