Manage Codex SDK threads and external task input
Plan thread creation, resumption, trust marking of external content and handling of the final result.
Prerequisites and input · Engineering checks · 1. Fix the runtime location and build a thread map
At a glance
When an application drives Codex through the Codex SDK, there is more to manage than the run() call: who creates the thread, how it is continued, what identity the material from external systems carries when handed to Codex, and where the final result lands. This article shows how to write that plan: the input is the task the application passes in plus external-system material, and the deliverable is a plan covering thread creation, resumption, context trust and handling of the final result. The only basis is the official Codex SDK documentation, and none of the code below was run.
Runtime and applicability
- Article type
- Engineering practice
- Verified
- Applies to
- Codex Manage Codex SDK threads and external task input
Execution locations and responsibilities
- Codex host and calling client Use the workflow below to distinguish responsibilities of the caller, runtime, and external actions.
Arrows show the order of checks in the guide. Follow each step’s prerequisites, branches and stop conditions; unexecuted steps are not completed work.
When to use
- TypeScript creates a thread with
startThread()and runs it withrun(); callingrun()again continues the same thread, andresumeThread()takes a thread ID. Python's counterparts arethread_start()andthread_resume(). TypeScriptthreadSourcelabels the integration at creation, and resuming preserves the original source.
When not to use
ExternalMessageappears only in the Python SDK section of the documentation and has no TypeScript counterpart there; do not assume the same type exists in TypeScript.ExternalMessageonly lowers the instruction priority of the content, and the documentation does not promise it prevents external content from misleading the model; actions involving writes, network or credentials must still be limited by the application's permissions and sandbox.- The documentation does not state the exception type for a failed run or an interface for closing a thread, so the application must record terminal states itself; approval request handling is also out of scope here.
Prerequisites and input
- The application calls the SDK server-side: the TypeScript library needs
Node.js18 or later and the Python SDK needs Python 3.10 or later. - The application's own instructions are separated from external content that comes from an issue tracker, another tool or another agent.
- If supplying your own Codex executable, verify the minimum version for the Python features you use. The pinned runtime statement here applies to published Python SDK builds only; it does not describe TypeScript builds.
Engineering checks
Thread creation and resumption — TypeScript creates a thread with startThread() and runs it with run(); calling run() again continues the same thread, and resumeThread() takes a thread ID. Python's counterparts are thread_start() and thread_resume(). TypeScript threadSource labels the integration at creation, and resuming preserves the original source.
Trust in external content — In Python, content from another system is passed as ExternalMessage, which must be the entire input to run() or turn(); Codex treats it as tool output at a lower priority than user and developer instructions. The TypeScript usage in the documentation shows no such type.
Results and thread state — The result is read from finalResponse in TypeScript and final_response in Python. The following are Python documentation contracts: when resuming, include_turns=False skips loading old turns although the model still has the thread's context, and thread.read(include_turns=True) retrieves the history later; a sandbox passed to run() applies to that turn and later turns.
1. Fix the runtime location and build a thread map
Where: the application's server-side code. Input: the application's task identifier and the SDK language in use. Action: check the Node.js or Python version, create a map from task identifier to thread ID, and in TypeScript label threads with threadSource (the documentation's example is ci). Completion check: the map has a storage location and its fields include task identifier, thread ID and source. Stop condition: if the runtime version is below the documented requirement, upgrade first and do not call the SDK.
2. Create the thread with limited permissions
Where: the code path that creates the thread. Input: the task's permission needs. Action: in the Python branch choose Sandbox.read_only for review only and Sandbox.workspace_write when files must change; Sandbox.full_access means no filesystem restrictions and needs an explicit reason. TypeScript passes structured settings in config and uses configOverrides for keys that cannot be written as dotted paths; raw overrides take precedence over structured config, and baseUrl plus thread-specific options override both. Completion check: after the first run the thread ID is in the map (thread.id in TypeScript). Stop condition: if no thread ID was obtained, do not continue to later stages.
3. Classify the input and send external content as ExternalMessage
Where: the code that builds the input to run() or turn(). Input: the application's instructions and external material such as an issue body. Action: put instructions in calls the application originates; pass the external material separately as an ExternalMessage with a tool_name. It must be the entire input of that call and cannot be joined with instructions, and it can also join an active regular turn. Lower priority only describes ordering and does not make content trustworthy, so do not rely on it to authorize risky actions. Completion check: every external source in the code maps to a distinct tool_name. Stop condition: if you use TypeScript and need an equivalent marker, the documentation gives none, so leave it to be confirmed and do not assume.
4. Resume the thread and handle sandbox and provider
Where: later server-side calls. Input: the thread map and SDK language. Inside a process call run again. TypeScript resumes across processes with resumeThread() and explicit sandboxMode; its integration source follows threadSource. Python uses thread_resume(), with include_turns=False to avoid loading old turns. Turn-to-turn sandbox persistence, thread.read, CLI>=0.166.0 resumption with the saved provider and using thread_fork for a provider change are Python documentation contracts, not TypeScript contracts. Completion: interfaces match the selected language and thread ID map, with permissions explicit in that language. Stop resumption for unknown ID or language contracts; a Python provider change needs its new-thread/fork decision.
5. Handle the final result
Where: the code after each run() returns. Input: finalResponse or final_response. Action: store it as text in the application's task record with the thread ID and stage name, and validate it in the application before passing it downstream. The documentation does not say what is raised when a run fails or whether there is a structured status field, so the application records success, failure and timeout itself. Completion check: every stage has a result record containing the thread ID. Stop condition: mark an empty or errored result as failed, and never pass an empty value on as success.
6. Close out and audit
Where: the point where thread use ends. Input: the map and the result records. Action: in Python, bound the lifetime with with Codex() as codex or async with AsyncCodex() as codex; the TypeScript documentation shows no call to close a thread, so the application simply stops using it and marks the task ended. In Python use thread.read(include_turns=True) when you need the history. Completion check: every finished task has a terminal state in the map. Stop condition: if a thread's state cannot be explained, leave the task open and do not delete its records.
Task example
This is an unexecuted Codex Python SDK walkthrough: This is an original manual walkthrough and no code was run. Imagine an application handling one issue: it creates a thread with a read-only sandbox for diagnosis and saves the thread ID; the issue body is passed as an ExternalMessage whose tool_name is issue_tracker and which is the whole input of that call; after diagnosis it resumes the same thread ID and switches the sandbox to Sandbox.workspace_write to make the change. The expected observations are: the map holds a single thread ID for the task, each of the two stages has a result record, and the thread ID is the same before and after resuming. If the team later changes model provider, the documentation says to use thread_fork. All of this is hypothetical expectation, not output from an actual run.
Completion checks
- Every task has three records: task identifier, thread ID and source label.
- Python external content uses ExternalMessage with tool_name as the complete input. The supplied source gives no equivalent TypeScript contract; keep that requirement unconfirmed without inheriting Python checks.
- The sandbox setting of each stage is recorded, and a switch from read-only to writable is an explicit action.
- The final response of each run is validated on the application side before downstream use.
Verification scope
Only the official Codex SDK documentation was read. Installation, login, API execution and output were not tested, and every observation in the example is a hypothetical expectation rather than a run result. Behavior on run failure and how TypeScript marks external content are not covered by the documentation and remain to be confirmed.Python and TypeScript follow their supplied language sections separately. Pinned runtime, Sandbox, include_turns, thread.read and provider changes are Python contracts; threadSource follows TypeScript documentation.
Sources
All links are official pages; verification dates are listed per source, and links point to the version checked.