Multi-turn conversations
Keep a conversation going by including relevant messages with each request.
How conversation history works#
A Chat Completions request contains the context for that call. To ask a follow-up, send the earlier user message, the assistant's answer and the new question in order. Your application is responsible for maintaining this history.
Send a follow-up#
Use the environment variables and Python package from the quickstart. This basic text example makes two billable requests. For reasoning or tool-enabled models, follow the provider's rules for carrying extra fields and tool results forward.
import os
from openai import OpenAI
client = OpenAI(
api_key=os.environ["CODING_PLAN_API_KEY"],
base_url=os.environ["CODING_PLAN_BASE_URL"],
)
messages = [{"role": "user", "content": "What is a Python generator?"}]
first = client.chat.completions.create(
model=os.environ["CODING_PLAN_MODEL"], messages=messages,
)
answer = first.choices[0].message.content
if answer is None:
raise RuntimeError("No text answer returned; inspect the response.")
messages.append({"role": "assistant", "content": answer})
messages.append({"role": "user", "content": "Show a short example."})
second = client.chat.completions.create(
model=os.environ["CODING_PLAN_MODEL"], messages=messages,
)
print(second.choices[0].message.content)Manage growing context#
Each request resends the included history as input. Long conversations can increase input usage and reach context limits. Retain the instructions and messages needed for the current task, and summarize or remove obsolete history deliberately.
Keep histories separate for different users. Store conversation data according to your application's privacy requirements; avoid logging keys or secrets contained in messages.
Next steps#
Combine history with streaming for an interactive UI. Review token usage to understand why a short follow-up can still have a large input.
Reference: DeepSeek Chat Completions message format.
Guide updated November 5, 2026 · All tutorials