Building a Chat Client with the Responses API
This lesson explains how a chat application sends the user's input to a deployed model and prints the reply through the Responses API, and how it keeps the conversation's context.
Learning outcomes
After completing this lesson, you will be able to:
- Describe the arguments of responses.create().
- Compare the ways a chat client keeps context between turns.
- Describe how streaming and error handling work.
Calling the model
The Responses API is the recommended API for generating responses. responses.create() takes the deployment name as model, optional instructions (the system prompt), the input (a string or a list of messages) and parameters such as max_output_tokens. The response's output_text property contains the generated text, and usage reports input and output tokens. The older Chat Completions API, which takes a list of system, user and assistant messages, is still supported.
response = openai.responses.create(
model="gpt-5.4-mini", # the deployment name
instructions="You are a concise assistant for a hiking equipment shop.",
input="Which sleeping bag rating do I need for spring camping?",
)
print(response.output_text)
print(response.usage.input_tokens, response.usage.output_tokens)Inside a Responses API call
Keeping context between turns
Models do not remember earlier requests. A chat client keeps context in one of three ways. It can resend the history, appending each user message and model reply to a list and sending the whole list as input; this is explicit but grows the prompt (and cost) every turn. It can pass previous_response_id, so that the service links the new request to the stored previous response and its context. Or it can create a conversation with conversations.create() and pass its ID with each request, which stores the conversation's items durably.
previous_id = None
while True:
user_text = input("You: ")
if user_text.lower() in ("quit", "exit"):
break
response = openai.responses.create(
model="gpt-5.4-mini",
instructions="You are a concise assistant for a hiking equipment shop.",
input=user_text,
previous_response_id=previous_id, # links this turn to the last one
)
previous_id = response.id
print("Assistant:", response.output_text)The following diagram compares the three ways to keep context.

Three ways to keep context
Streaming and errors
Passing stream=True returns events as the model generates tokens, so a chat interface can display text progressively; the client prints the text of each response.output_text.delta event. Applications should also handle errors: HTTP 429 means the deployment's rate limit was exceeded and the request should be retried after a delay, and a content-filter error means a guardrail blocked the prompt or response.
Streams and errors
A chat that forgets
A developer's chat client answers Which tents do you sell? correctly, but when the user then asks Which of those is lightest? the model replies that it does not know what those refers to. Each turn calls responses.create(model=..., input=user_text).
Each request is independent, so the second turn has no context. Passing the previous response's ID as previous_response_id, using a conversation, or sending the accumulated message history fixes it. Increasing max_output_tokens or choosing a larger model would not, because the missing information was never sent.
What does stream=True do in a Responses API call?
Show answer
It returns the output progressively as it is generated, instead of all at once.
Exam Tips
Remember for the exam
- responses.create() takes the deployment name, instructions and input; output_text holds the reply.
- Keep context with message history, previous_response_id or a conversation.
- stream=True returns output progressively; HTTP 429 calls for retry with backoff.