Building with the Codex SDK
Your team wants an internal dashboard where product managers can click “Fix this bug” on a Jira ticket and have Codex automatically analyze the issue, propose a fix, and open a PR. Or maybe you need a nightly cron job that reviews all open PRs and posts summaries. The Codex CLI handles one-off tasks; the SDK lets you build Codex into your own applications.
What You’ll Walk Away With
Section titled “What You’ll Walk Away With”- A working TypeScript integration that starts Codex threads, sends prompts, and processes results
- Patterns for multi-turn conversations, session resumption, and error handling
- Architecture guidance for building internal tools, dashboards, and automated pipelines with the SDK
- Security best practices for API key management in server-side applications
Getting Started
Section titled “Getting Started”The SDK requires Node.js 18+ and runs server-side only.
npm install @openai/codex-sdkBasic Usage
Section titled “Basic Usage”import { Codex } from "@openai/codex-sdk";
const codex = new Codex();const thread = codex.startThread();const result = await thread.run( "Analyze the test failures in the CI logs and propose fixes");
console.log(result);The run() method sends a prompt to Codex, waits for the agent to complete its work (including file reads, edits, and command execution), and returns the final result.
Multi-Turn Conversations
Section titled “Multi-Turn Conversations”Call run() again on the same thread to continue the conversation:
// First turn: analyzeconst analysis = await thread.run( "Review the authentication module for security issues");
// Second turn: fix what was foundconst fixes = await thread.run( "Implement the fixes you identified. Run the test suite after each change.");Resuming Past Threads
Section titled “Resuming Past Threads”If you need to continue a previous session (e.g., a two-stage pipeline), provide the thread ID:
const threadId = "01912345-abcd-7890-ef01-234567890abc";const thread = codex.resumeThread(threadId);const result = await thread.run("Pick up where you left off and finish the migration");Architecture Patterns
Section titled “Architecture Patterns”Internal Dashboard
Section titled “Internal Dashboard”Build a web API that exposes Codex operations to internal tools:
// POST /api/codex/taskapp.post("/api/codex/task", async (req, res) => { const { prompt, projectPath } = req.body;
const codex = new Codex(); const thread = codex.startThread();
try { const result = await thread.run(prompt); res.json({ threadId: thread.id, result, status: "completed" }); } catch (error) { res.status(500).json({ error: error.message }); }});Scheduled Pipeline
Section titled “Scheduled Pipeline”Combine the SDK with a job scheduler for recurring tasks:
import { CronJob } from "cron";
const nightlyReview = new CronJob("0 2 * * *", async () => { const codex = new Codex(); const thread = codex.startThread();
const result = await thread.run( "Review all PRs opened in the last 24 hours. " + "For each PR, summarize the changes and flag any security concerns. " + "Output a JSON array with pr_number, summary, and risk_level fields." );
await postToSlack("#engineering", result);});
nightlyReview.start();SDK vs codex exec vs Cloud Tasks
Section titled “SDK vs codex exec vs Cloud Tasks”| Feature | SDK | codex exec | Cloud Tasks |
|---|---|---|---|
| Programmatic control | Full | CLI flags + stdin/stdout | Web UI + integrations |
| Multi-turn conversations | Native | Resume with --last | Follow-up in thread |
| Environment | Your server | Your machine / CI runner | OpenAI’s containers |
| Best for | Custom apps, dashboards | CI/CD scripts, one-off automation | Delegated work, mobile triage |
When This Breaks
Section titled “When This Breaks”- Authentication errors: The SDK uses the same credentials as the CLI. Ensure
CODEX_API_KEYis set or runcodex loginon the machine running the SDK. - Thread timeouts: Long-running tasks may hit API timeouts. Break complex work into smaller
run()calls rather than one massive prompt. - Rate limits: The SDK is subject to the same rate limits as your plan. Monitor usage with
/statusin the CLI or the usage dashboard. - Stale thread state: If you resume a thread from days ago, the codebase may have changed. Include context about recent changes in your follow-up prompt.
What’s Next
Section titled “What’s Next”- Non-Interactive Mode — For simpler scripting needs,
codex execmay be sufficient - GitHub Action — Pre-built CI/CD integration without writing SDK code
- Cost Management — SDK usage counts against your plan limits