Model Requests and Streaming
A client can display text before Codex has a completed assistant message to record. Start with Prompt Construction for the model input. This chapter follows the outgoing request and the incoming events in a scripted text response.
Evidence. Source and upstream assertions inspected at Codex CLI 0.172.0. The server-sent events (SSE) below come from a mock provider.
Separate requests, items, and turns
Section titled “Separate requests, items, and turns”| Unit | What it identifies |
|---|---|
| Provider response | One response to a model request. A turn can require a second request when a tool result becomes available. Tool-call fixture |
| Output item | One message, tool request, or other unit in that response. The text fixture uses the same message item ID at start and completion. Item assertions |
| Codex turn | A unit of runtime work that can accept additional user input through steering. The fixture’s delta event identifies the thread, turn, and item together. Delta event |
Follow the stream
Section titled “Follow the stream”- Build the request. The provider client takes the formatted input and selected model slug. The request includes tool definitions where applicable and sets
stream: true. Responses request - Decode the events. The SSE parser maps
response.output_text.deltatoOutputTextDeltaand parsesresponse.output_item.doneas a completeResponseItem. An item can finish before the provider marks the response complete. Event decoder · Fixture sequence - Publish visible text. In the ordinary assistant-message path, Codex uses the active item’s ID, parses the delta, and emits the visible text with its thread and turn IDs. The fixture observes this core event. Active item handling · Delta emitter
- Finalize and record the message. When the finished item reaches the non-tool branch, Codex finalizes its client-facing item and records the completed response item for conversation history. A completed tool call follows a separate dispatch path. Output item dispatch · Non-tool finalization · History recording
Read the fixture
Section titled “Read the fixture”The mock provider starts message msg-1, streams the text streamed response, completes that message with its full text, and then completes the response. The test waits for a started item, a text delta, and a completed item. It checks the delta’s exact text and thread, turn, and item IDs. It also checks that the completed item has the same ID as the started item. These assertions establish identity across the core event sequence. Inspect the recording code separately for the history claim. Fixture input · Fixture assertions
Count transport attempts during recovery
Section titled “Count transport attempts during recovery”The retry handler checks whether the stream error is retryable. On the bounded retry path, a required HTTP fallback or an exhausted retry budget can switch the session away from WebSockets. The model client disables WebSockets for that session and clears its cached WebSocket connection. Retry decision · Transport switch · Session state
| What you count | What each fixture observes |
|---|---|
| Network attempts | With two stream retries and zero HTTP request retries configured, the two-turn fixture counts four GETs and two POSTs on /responses. Its comments attribute the GETs to a prewarm, an initial attempt, and two retries on the first turn. The assertions check aggregate counts across both turns. Setup and assertions |
| Client-visible retry events | A separate single-turn fixture checks the messages emitted before completion. The release build reports the second reconnect. The debug build reports both. Counting those messages will undercount attempts in the release build. Reporting rule · Event assertions |
For a latency experiment, record transport attempts at the server and timestamp the client’s first delta separately. A connection attempt, a completed Responses request, and a user-visible turn measure different pieces of work.
Research question
Section titled “Research question”How does an event consumer behave when a stream breaks between the last text delta and item completion? Replay the same text twice, once with the terminal item and once with a controlled interruption. Compare time to first visible text and whether the consumer commits a final message. Keep the final text and event timestamps before the interruption fixed.
Check your understanding
Section titled “Check your understanding”- Which identifiers let a client attach a delta to the right item?
- If the stream shows text, which event tells you that the item has finished?
- Why would a release client show fewer reconnect messages than the server saw connection attempts?
Answers. Use the thread, turn, and item IDs in the core delta. For the text fixture, check the completed item event with the same item ID. The release build suppresses the first WebSocket retry notice, and a prewarm can also generate a network attempt.
Continue with Tool Failures and Continuation to follow a completed tool call that yields a validation error.