C.W.K.
Stream
Lesson 06 of 07 · published

Stateful Sessions

~22 min · sessions, previous_response_id, stateful

Level 0Tokenizer
0 XP0/54 lessons0/10 achievements
0/120 XP to next level120 XP to go0% complete

The Responses API's killer feature for multi-turn conversations: previous_response_id. Instead of resending entire conversation history, you just reference the prior response.

Context Management

The conversation parameter provides an alternative to previous_response_id for automatic history management. store defaults to true — set store=False for zero data retention (use encrypted reasoning for stateless multi-turn).

The chain is a tree

Two responses with the same previous_response_id create two children of one parent. They run independently; neither knows about the other. This is the right primitive for A/B prompt testing, parallel exploration, or 'try both styles and let the user pick' UX flows.

Server-side retention is ~30 days by default. For audit, compliance, or your own analytics, persist a copy of every response — JSONL append-only is the canonical shape. See the Production track on JSONL logging.

Code

Three-turn chain via previous_response_id·python
# Turn 1
r1 = client.responses.create(model="gpt-5.4", input="My name is Alice.")

# Turn 2 — no need to resend Turn 1's content
r2 = client.responses.create(
    model="gpt-5.4",
    input="What is my name?",
    previous_response_id=r1.id,
)
print(r2.output_text)  # → "Your name is Alice."
Branching: forking a response into two children·python
# Automatic compaction when context gets large
response = client.responses.create(
    model="gpt-5.4",
    input="Continue our discussion.",
    previous_response_id="resp_abc123",
    context_management={
        "strategy": "auto",
        "max_tokens": 100000,  # compact when approaching this
    },
)

External links

Exercise

Run a 3-turn dialog. After turn 2, fork: one child asks the model to summarize politely, the other asks it to summarize sarcastically. Verify both children share the same previous_response_id and produce different output.

Progress

Progress is local-only — sign in to sync across devices.
Spotted a bug or have feedback on this page?Report an Issue

Comments 0

🔔 Reply notifications (sign in)
Sign inPlease sign in to comment.

No comments yet — be the first.