Codex SDK: install it, run a thread and resume it in TypeScript or Python
Codex SDK guide: install the TypeScript or Python package, run your first thread, set the model and sandbox, resume past threads, and know when to use codex exec.
By the Kipt team · Updated 24 November 2026 · 7 min read
What is the Codex SDK?
The Codex SDK lets your own code start, run, continue and resume Codex conversations without opening the terminal or the app. OpenAI's Codex SDK page lists two libraries: a TypeScript library installed with npm install @openai/codex-sdk, and a Python library installed with pip install openai-codex. Both control Codex running on your own machine.
The docs give four reasons to use it:
- Control Codex as part of your CI/CD pipeline.
- Create your own agent that can engage with Codex to perform complex engineering tasks.
- Build Codex into your own internal tools and workflows.
- Integrate Codex within your own application.
If you only want to run Codex by hand, you don't need the SDK. The Codex CLI is the everyday tool, and the SDK is for when a program, not a person, is the one asking Codex to work.
How do I install the Codex SDK?
Install the package for the language you use. As checked on 24 November 2026:
| TypeScript | Python | |
|---|---|---|
| Install | npm install @openai/codex-sdk |
pip install openai-codex |
| Needs | Node.js 18 or later | Python 3.10 or later |
| Where to run it | Server-side only | Not stated |
| Prereleases | Not mentioned | pip install --pre openai-codex |
| Source | github.com/openai/codex/tree/main/sdk/typescript | github.com/openai/codex/tree/main/sdk/python |
The Python page says the SDK is available as a stable release, so a plain pip install openai-codex installs the latest stable version. It also says published builds include a pinned Codex CLI runtime, so you don't have to install the CLI separately. Pass CodexConfig(codex_bin=...) only when you deliberately want to run against a specific local Codex executable.
How do I run my first Codex thread?
A thread is one Codex conversation. You create a thread, call run() with a prompt, and read the final answer. This is the TypeScript example from the official page:
import { Codex } from "@openai/codex-sdk";
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);
The Python version uses a with block so the Codex process is closed for you. Here the model and sandbox are set when the thread starts:
from openai_codex import Codex, Sandbox
with Codex() as codex:
thread = codex.thread_start(
model="gpt-6.1-sol",
sandbox=Sandbox.workspace_write,
)
result = thread.run("Make a plan to diagnose and fix the CI failures")
print(result.final_response)
Two things to notice. The result exposes the final reply as finalResponse in TypeScript and final_response in Python. And the docs say the example model, GPT-6.1 Sol, "must be available to your signed-in account". If it isn't, leave out model and Codex uses your configured default. OpenAI points to its model availability by plan table for what each plan includes.
The SDK page does not describe an API key flow, and this guide doesn't claim one. The examples assume the account you are signed in with already has access.
If your code is already asynchronous, the TypeScript SDK is asynchronous throughout, and Python offers AsyncCodex:
import asyncio
from openai_codex import AsyncCodex
async def main() -> None:
async with AsyncCodex() as codex:
thread = await codex.thread_start(model="gpt-6.1-sol")
result = await thread.run("Implement the plan")
print(result.final_response)
asyncio.run(main())
How do I continue or resume a thread?
Call run() again on the same thread object to continue the conversation. To pick up a thread from an earlier process, save its id and resume it. This TypeScript example is from the docs:
// 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, {
sandboxMode: "workspace-write",
});
const result2 = await thread2.run("Pick up where you left off");
console.log(result2.finalResponse);
A useful detail for pipelines: set threadSource when you create a thread to label where it came from. codex.startThread({ threadSource: "ci" }) labels the thread ci, and resuming a thread keeps its original source. That makes it easier to tell automated threads from ones a person started.
In Python, codex.thread_resume() and codex.thread_fork() accept include_turns=False. That skips loading old turns into the response, although the model still has the thread's context. To read the history later, call thread.read(include_turns=True). With Codex CLI 0.166.0 or later, a resumed thread uses its saved model provider. If you switch providers, call codex.thread_fork(thread_id) to continue from the same history on the new provider.
If you want to browse the threads Codex has already saved, see Codex sessions.
How do I control the model, sandbox and configuration?
Set them when you create the thread. In TypeScript the options include model, modelReasoningEffort and sandboxMode:
const thread = codex.startThread({
model: "gpt-6.1-sol",
modelReasoningEffort: "low",
sandboxMode: "workspace-write",
});
Python has three sandbox presets, described on the official page:
| Preset | What it allows |
|---|---|
Sandbox.read_only |
Read files without allowing writes |
Sandbox.workspace_write |
Read files and write inside the workspace and configured writable roots |
Sandbox.full_access |
Run without filesystem access restrictions |
If you leave out sandbox=, the app server uses its configured default. A sandbox passed to run(...) or turn(...) applies to that turn and later turns on the thread. That lets one thread make a change and then review it read-only:
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)
For anything automated, pick the narrowest preset that works. The approvals and sandbox guide explains what each level means in Codex itself.
For settings beyond those options, the TypeScript constructor takes config for structured Codex configuration and configOverrides for raw TOML strings that are passed as separate --config arguments. Raw overrides win over config, and the SDK's own managed settings, such as baseUrl and thread options, win over both. These overrides don't change your Codex configuration files. The usual files are covered in the Codex config guide.
What else does the Python SDK add?
The Python page documents three extras. Each needs Codex CLI 0.151.0 or later if you supply your own Codex executable.
ExternalMessagepasses content from a tool, application or another agent. For example,ExternalMessage(tool_name="issue_tracker", content=issue_body)marks an issue description as coming from a tracker. Codex treats it as tool output, at a lower priority than user and developer instructions. Pass it as the entire input tothread.run()orthread.turn().include_turns=Falseon resume or fork, covered above.turn_service_tieronrun()orturn()chooses the service tier for one new turn.turn_service_tier="default"uses standard speed for that turn. To change the tier for this and later turns, passservice_tier.
The Python SDK works by controlling the local Codex app server over JSON-RPC. You don't need to know that to use it, but it explains the version notes above. If you want to talk to the app server directly, read the Codex app server guide.
Should I use the SDK, codex exec, the app server or the GitHub Action?
Pick by who is in control and how much you need back from Codex. OpenAI's docs draw these lines: the SDK is for automating coding tasks including CI jobs, and the app server is for custom clients that handle authentication, conversation history, approvals and streamed agent events.
| You want to | Use | Why |
|---|---|---|
| Run one prompt in a shell script or CI step | codex exec |
Prints the final message to stdout, no code to write |
| Call Codex from your own TypeScript or Python program | Codex SDK | Thread objects you can continue, resume and configure |
| Build a client with its own sign-in, approvals UI and live events | App server | The interface Codex uses to power rich clients |
| Trigger Codex from GitHub events | Codex GitHub Action | Wired to pull requests and workflows |
| Work by hand in a terminal | Codex CLI | Interactive, with approvals in the terminal |
One removal matters if you have older code. The docs state: "The codex mcp-server command and standalone codex-mcp-server binary have been removed." They tell existing integrations to use the app server instead. If you want Codex to use MCP tools rather than be controlled as one, that is a different topic, covered in Codex MCP.
How does the Codex SDK compare with the Claude Agent SDK?
They solve a similar problem for different agents. Anthropic's Agent SDK overview describes a library that runs the Claude Code binary, programmable in Python and TypeScript, with Claude Code's built-in tools, permissions, sessions and hooks. Its quickstart has you set an API key. The Codex SDK page, as checked on 24 November 2026, documents TypeScript and Python libraries for starting, continuing and resuming Codex threads, plus sandbox presets and configuration overrides, and says nothing about API keys.
Both give you a programmable version of a coding agent you may already use by hand, so the practical rule is to use the SDK that matches the agent your team trusts. For the Claude side, see the Claude Agent SDK guide. This guide does not compare them on speed, price or quality, and it doesn't claim either is better.
Where do prompts and skills fit in?
An SDK call is only as good as the prompt you pass to run(). Pipelines tend to reuse a handful of prompts, such as "diagnose the CI failure" or "review this diff read-only", and it helps to keep them in one place instead of in scattered scripts. Kipt stores your prompts and skills as Markdown in a private GitHub repo you own, so your team keeps one copy of each prompt. Codex can also load skills from .agents/skills, which keeps repeatable procedures out of your application code.
Questions
For TypeScript run npm install @openai/codex-sdk, which needs Node.js 18 or later and should be used server-side. For Python run pip install openai-codex, which needs Python 3.10 or later. Add --pre to the pip command to get newer prerelease builds.
Yes. OpenAI's Codex SDK page documents a Python library installed with pip install openai-codex, and says it is available as a stable release. It controls the local Codex app server over JSON-RPC, and published builds include a pinned Codex CLI runtime.
codex exec is a command you run from a shell or CI script, and it prints the final message to stdout. The SDK is a library you call from your own TypeScript or Python code, where you get a thread object you can run again, resume and configure. Use exec for one-off pipeline steps and the SDK when Codex is part of an application.
Yes. In TypeScript, call codex.resumeThread(threadId) with the saved thread id, then run the thread again. In Python, codex.thread_resume() reopens a thread, and passing include_turns=False skips loading its old turns into the response.
The official Codex SDK page documents only a TypeScript library and a Python library. It does not mention Go or Rust. For other languages, the Codex app server speaks JSON-RPC over stdio, and OpenAI's docs include Go and Ruby client examples for it.