2025-10-01 08:29:59 -07:00
# Codex SDK
2025-10-01 13:12:59 -07:00
Bring the power of the best coding agent to your application.
## Installation
```bash
npm install @openai/codex -sdk
```
## Usage
2025-10-04 19:55:33 -07:00
Call `startThread()` and `run()` to start a thread with Codex.
2025-10-01 13:12:59 -07:00
```typescript
import { Codex } from "@openai/codex -sdk";
const codex = new Codex();
const thread = codex.startThread();
const result = await thread.run("Diagnose the test failure and propose a fix");
console.log(result);
```
You can call `run()` again to continue the same thread.
```typescript
const result = await thread.run("Implement the fix");
console.log(result);
```
### Streaming
2025-10-04 19:55:33 -07:00
The `run()` method completes when a thread turn is complete and the agent has produced the final response.
2025-10-01 13:12:59 -07:00
2025-10-04 19:55:33 -07:00
You can stream events while they are being produced by calling `runStreamed()` and iterating the returned generator.
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) {
console.log(event);
}
2025-10-01 13:12:59 -07:00
```
### Resuming a thread
2025-10-04 19:55:33 -07:00
If you don't have the original `Thread` instance to continue the thread, you can resume by calling `resumeThread()` and providing the thread identifier.
2025-10-01 13:12:59 -07:00
```typescript
const threadId = "...";
const thread = codex.resumeThread(threadId);
const result = await thread.run("Implement the fix");
console.log(result);
```
2025-10-01 17:31:13 -07:00
### Working directory
2025-10-04 19:55:33 -07:00
By default, Codex will run in the current working directory. You can change the working directory by passing the `workingDirectory` option when creating a thread.
2025-10-01 17:31:13 -07:00
```typescript
const thread = codex.startThread({
workingDirectory: "/path/to/working/directory",
});
```
2025-10-04 19:55:33 -07:00
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({
skipGitRepoCheck: true,
});
```