2025-10-01 08:29:59 -07:00
# Codex SDK
2025-10-01 13:12:59 -07:00
2025-10-05 14:43:34 -07:00
Embed the Codex agent in your workflows and apps.
The TypeScript SDK wraps the bundled `codex` binary. It spawns the CLI and exchanges JSONL events over stdin/stdout.
2025-10-01 13:12:59 -07:00
## Installation
```bash
npm install @openai/codex -sdk
```
2025-10-05 14:43:34 -07:00
Requires Node.js 18+.
2025-10-01 13:12:59 -07:00
2025-10-05 14:43:34 -07:00
## Quickstart
2025-10-01 13:12:59 -07:00
```typescript
import { Codex } from "@openai/codex -sdk";
const codex = new Codex();
const thread = codex.startThread();
2025-10-05 14:43:34 -07:00
const turn = await thread.run("Diagnose the test failure and propose a fix");
2025-10-01 13:12:59 -07:00
2025-10-05 14:43:34 -07:00
console.log(turn.finalResponse);
console.log(turn.items);
2025-10-01 13:12:59 -07:00
```
2025-10-05 14:43:34 -07:00
Call `run()` repeatedly on the same `Thread` instance to continue that conversation.
2025-10-01 13:12:59 -07:00
```typescript
2025-10-05 14:43:34 -07:00
const nextTurn = await thread.run("Implement the fix");
2025-10-01 13:12:59 -07:00
```
2025-10-05 14:43:34 -07:00
### Streaming responses
2025-10-01 13:12:59 -07:00
2025-10-05 14:43:34 -07:00
`run()` buffers events until the turn finishes. To react to intermediate progress—tool calls, streaming responses, and file diffs—use `runStreamed()` instead, which returns an async generator of structured events.
2025-10-01 13:12:59 -07:00
```typescript
2025-10-04 19:55:33 -07:00
const { events } = await thread.runStreamed("Diagnose the test failure and propose a fix");
for await (const event of events) {
2025-10-05 14:43:34 -07:00
switch (event.type) {
case "item.completed":
console.log("item", event.item);
break;
case "turn.completed":
console.log("usage", event.usage);
break;
}
2025-10-04 19:55:33 -07:00
}
2025-10-01 13:12:59 -07:00
```
2025-10-05 18:17:50 -07:00
### Structured output
Provide a JSON schema per turn to have Codex respond with structured JSON. Pass schemas as
plain JavaScript objects.
```typescript
const schema = {
type: "object",
properties: {
summary: { type: "string" },
status: { type: "string", enum: ["ok", "action_required"] },
},
required: ["summary", "status"],
additionalProperties: false,
} as const;
const turn = await thread.run("Summarize repository status", { outputSchema: schema });
console.log(turn.finalResponse);
```
You can also create JSON schemas for Zod types using the `zod-to-json-schema` package and setting the `target` to `"openAi"` .
```typescript
const schema = z.object({
summary: z.string(),
status: z.enum(["ok", "action_required"]),
});
const turn = await thread.run("Summarize repository status", {
outputSchema: zodToJsonSchema(schema, { target: "openAi" }),
});
console.log(turn.finalResponse);
```
2025-10-05 14:43:34 -07:00
### Resuming an existing thread
2025-10-01 13:12:59 -07:00
2025-10-05 14:43:34 -07:00
Threads are persisted in `~/.codex/sessions` . If you lose the in-memory `Thread` object, reconstruct it with `resumeThread()` and keep going.
2025-10-01 13:12:59 -07:00
```typescript
2025-10-05 14:43:34 -07:00
const savedThreadId = process.env.CODEX_THREAD_ID!;
const thread = codex.resumeThread(savedThreadId);
await thread.run("Implement the fix");
2025-10-01 13:12:59 -07:00
```
2025-10-01 17:31:13 -07:00
2025-10-05 14:43:34 -07:00
### Working directory controls
2025-10-01 17:31:13 -07:00
2025-10-05 14:43:34 -07:00
Codex runs in the current working directory by default. To avoid unrecoverable errors, Codex requires the working directory to be a Git repository. You can skip the Git repository check by passing the `skipGitRepoCheck` option when creating a thread.
2025-10-01 17:31:13 -07:00
```typescript
const thread = codex.startThread({
2025-10-05 14:43:34 -07:00
workingDirectory: "/path/to/project",
2025-10-01 17:31:13 -07:00
skipGitRepoCheck: true,
});
```