Codex CLI、IDE 拡張機能、Codex クラウドのいずれかを介して Codex を使用している場合は、プログラムから制御することもできます。
次のような場合は SDK を使用します:
- CI/CD パイプラインの一部として Codex を制御する
- Codex と連携して複雑なエンジニアリングタスクを実行できる独自のエージェントを作成する
- 独自の社内ツールやワークフローに Codex を組み込む
- 独自のアプリケーションに Codex を統合する
コーディングに特化した Codex スレッドには、Codex SDK を使用します。より広範なオーケストレーションワークフローの中で Codex が専門エージェントの 1 つとして機能する場合は、Codex CLI を MCP サーバーとして実行し、Agents SDK でオーケストレーションします。
ベータ版へのアクセス権があり、リポジトリまたは変更内容をスキャンして構造化された セキュリティ検出結果とカバレッジを取得する必要がある場合は、Codex Security TypeScript SDK を使用します。
TypeScript ライブラリ
TypeScript ライブラリを使うと、アプリケーションからローカルの Codex スレッドを開始、継続、再開できます。
このライブラリはサーバー側で使用してください。Node.js 18 以降が必要です。
インストール
はじめに、npm を使用して Codex SDK をインストールします:
npm install @openai/codex-sdk
使用方法
Codex でスレッドを開始し、プロンプトを指定して実行します。
const codex = new Codex();
const thread = codex.startThread();
const result = await thread.run(
"Make a plan to diagnose and fix the CI failures"
);
console.log(result.finalResponse);
同じスレッドで処理を続けるには、もう一度 run() を呼び出します。以前のスレッドを再開するには、スレッド ID を指定します。
// running the same thread
const result = await thread.run("Implement the plan");
console.log(result.finalResponse);
// resuming past thread
const threadId = "<thread-id>";
const thread2 = codex.resumeThread(threadId);
const result2 = await thread2.run("Pick up where you left off");
console.log(result2.finalResponse);
詳しくは、TypeScript リポジトリ をご覧ください。
Python ライブラリ
Python SDK は、JSON-RPC 経由でローカルの Codex app-server を制御します。Python 3.10 以降が必要です。公開済みの SDK ビルドには、固定バージョンの Codex CLI ランタイムが依存関係として含まれています。
インストール
SDK をインストールするには、次を実行します:
pip install openai-codex
公開済みの SDK ビルドは、固定バージョンのランタイムを自動的に使用します。特定のローカル Codex 実行ファイルを意図的に使用する場合にのみ、CodexConfig(codex_bin=...) を渡してください。
Python SDK は安定版として提供されています。pip install openai-codex
を実行すると、最新の安定版がインストールされます。pip install --pre openai-codex を使用すると、より新しいプレリリースビルドを
利用できます。
使用方法
Codex を起動してスレッドを作成し、プロンプトを実行します:
from openai_codex import Codex, Sandbox
with Codex() as codex:
thread = codex.thread_start(
model="gpt-5.6-terra",
sandbox=Sandbox.workspace_write,
)
result = thread.run("Make a plan to diagnose and fix the CI failures")
print(result.final_response)
アプリケーションですでに非同期処理を使用している場合は、AsyncCodex を使用します:
from openai_codex import AsyncCodex
async def main() -> None:
async with AsyncCodex() as codex:
thread = await codex.thread_start(model="gpt-5.6-terra")
result = await thread.run("Implement the plan")
print(result.final_response)
asyncio.run(main())
サンドボックスのプリセット
同じ Sandbox プリセットは、スレッドの作成時や、後続のターンでファイルシステム
へのアクセス権を変更するときに使用します:
from openai_codex import Codex, Sandbox
with Codex() as codex:
thread = codex.thread_start(sandbox=Sandbox.workspace_write)
thread.run("Make the requested change.")
review = thread.run("Review the diff only.", sandbox=Sandbox.read_only)
利用可能なプリセット:
Sandbox.read_only:書き込みを許可せずにファイルを読み取ります。Sandbox.workspace_write:ファイルを読み取り、ワークスペース内および設定済みの書き込み可能なルート内に書き込みます。Sandbox.full_access:ファイルシステムへのアクセスを制限せずに実行します。
sandbox= を省略すると、app-server は構成済みのデフォルト設定を使用します。サンドボックスを
run(...) または turn(...) に渡した場合、そのサンドボックスは該当するターンと、同じスレッド内の後続のターン
に適用されます。
詳しくは、Python リポジトリ をご覧ください。