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 (
codexmust 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 run "Summarize this repo."
codexapi run --cwd /path/to/project "Fix the failing tests."
echo "Say hello." | codexapi run
codexapi task exits with code 0 on success and 1 on failure, printing the summary.
codexapi task "Fix the failing tests." --max-iterations 5
Show running sessions and their latest activity:
codexapi top
Press h for keys.
Resume a session and print the thread id to stderr:
codexapi run --thread-id THREAD_ID --print-thread-id "Continue where we left off."
Use --no-yolo to run Codex with --full-auto instead.
Ralph loop mode repeats the same prompt until a completion promise or a max
iteration cap is hit (0 means unlimited). Cancel by deleting
.codexapi/ralph-loop.local.md or running codexapi ralph --cancel.
codexapi ralph "Fix the bug." --completion-promise DONE --max-iterations 5
codexapi ralph --ralph-fresh "Try again from scratch." --max-iterations 3
codexapi ralph --cancel --cwd /path/to/project
API
agent(prompt, cwd=None, yolo=True, 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--yoloto Codex when true (defaults to true).flags(str | None): extra CLI flags to pass to Codex.
Agent(cwd=None, yolo=True, 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--yoloto Codex when true (defaults to true).flags(str | None): extra CLI flags to pass to Codex.
task(prompt, check=None, n=10, cwd=None, yolo=True, 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, orFalseto skip.n(int): maximum number of retries after a failed check.
task_result(prompt, check=None, n=10, cwd=None, yolo=True, 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=True, 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 orNone/"".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 --jsonand parses JSONL events foragent_messageitems. - Automatically passes
--skip-git-repo-checkso it can run outside a git repo. - Passes
--yoloby default (use--no-yolooryolo=Falsefor--full-auto). - Raises
RuntimeErrorif 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
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file codexapi-0.4.0.tar.gz.
File metadata
- Download URL: codexapi-0.4.0.tar.gz
- Upload date:
- Size: 21.7 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
02d001c77078c48d808fea978623d60cb7200e3f49073b0f721a5fd082d9b80c
|
|
| MD5 |
9bda659ea6e600b2cbca86cd26c95ed7
|
|
| BLAKE2b-256 |
a2c0c31a34ec81076300c66cbfb120e4dbba13134b347cd0a842969efaa35548
|
Provenance
The following attestation bundles were made for codexapi-0.4.0.tar.gz:
Publisher:
publish.yml on yieldthought/codexapi
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
codexapi-0.4.0.tar.gz -
Subject digest:
02d001c77078c48d808fea978623d60cb7200e3f49073b0f721a5fd082d9b80c - Sigstore transparency entry: 815909608
- Sigstore integration time:
-
Permalink:
yieldthought/codexapi@54c441a8bf5dfc197cc6b8f6b615114e75f2901e -
Branch / Tag:
refs/tags/v0.4.0 - Owner: https://github.com/yieldthought
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yml@54c441a8bf5dfc197cc6b8f6b615114e75f2901e -
Trigger Event:
push
-
Statement type:
File details
Details for the file codexapi-0.4.0-py3-none-any.whl.
File metadata
- Download URL: codexapi-0.4.0-py3-none-any.whl
- Upload date:
- Size: 22.1 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
69df56aa26d68bec340fdb4da37c48c2e7312608c3892dc953ffe2fd34b6a4f3
|
|
| MD5 |
410e5a331b08c58cb7beb31974bbfb34
|
|
| BLAKE2b-256 |
8d7d081a2f4898f6869476ac32cb76fcf45d1cd19e6168bcffca62fc6d279190
|
Provenance
The following attestation bundles were made for codexapi-0.4.0-py3-none-any.whl:
Publisher:
publish.yml on yieldthought/codexapi
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
codexapi-0.4.0-py3-none-any.whl -
Subject digest:
69df56aa26d68bec340fdb4da37c48c2e7312608c3892dc953ffe2fd34b6a4f3 - Sigstore transparency entry: 815909620
- Sigstore integration time:
-
Permalink:
yieldthought/codexapi@54c441a8bf5dfc197cc6b8f6b615114e75f2901e -
Branch / Tag:
refs/tags/v0.4.0 - Owner: https://github.com/yieldthought
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yml@54c441a8bf5dfc197cc6b8f6b615114e75f2901e -
Trigger Event:
push
-
Statement type: