Codex SDK
ローカルの Codex エージェントをプログラムから制御します
Codex CLI、IDE 拡張機能、または Codex cloud から Codex を使用している場合は、プログラムから制御することもできます。
次のような場合に SDK を使用します。
- CI/CD パイプラインの一部として Codex を制御する
- Codex と連携して複雑なエンジニアリングタスクを実行できる独自のエージェントを作成する
- Codex を独自の社内ツールやワークフローに組み込む
- Codex を独自のアプリケーションに統合する
コーディングに重点を置いた Codex thread には Codex SDK を使用します。Codex が、より広範なオーケストレーション済みワークフロー内の 1 つの専門エージェントである場合は、Codex CLI を MCP server として実行し、Agents SDK でオーケストレーションしてください。
ベータアクセスがあり、構造化されたセキュリティ検出結果とカバレッジを含むリポジトリまたは変更のスキャンが必要な場合は、Codex Security TypeScript SDK を使用してください。
TypeScript ライブラリ
TypeScript ライブラリを使用すると、アプリケーションからローカルの Codex thread を開始、継続、再開できます。
このライブラリはサーバー側で使用してください。Node.js 18 以降が必要です。
インストール
まず、npm を使用して Codex SDK をインストールします。
npm install @openai/codex-sdk使用方法
Codex で thread を開始し、プロンプトを指定して実行します。
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);同じ thread で続けるには run() をもう一度呼び出します。過去の thread を再開するには、thread 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 により公開済みの最新ベータビルドが選択されます。安定版 SDK のリリース後に、新しいプレリリースビルドを使用するには、
pip install --pre openai-codex を使用してください。
使用方法
Codex を起動し、thread を作成して、プロンプトを実行します。
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 を使用します。
import asyncio
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())サンドボックスプリセット
thread の作成時、または後続の turn でファイルシステムへのアクセス権を変更するときは、同じ 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(...) に渡したサンドボックスは、その turn と、同じ thread 上の後続の turn に適用されます。
詳細については、Python リポジトリをご覧ください。