Module 2, Lesson 4: Server-side Conversation (OpenAI)
Let OpenAI store the conversation with the Conversations API and send only new items each request.
Published: 11/22/2026
Lesson 4: Server-side conversation
In lesson 3 your code owned the history and resent all of it every time. OpenAI can also keep the history for you with the Conversations API. The file is src/openai/basic-chat-with-tools-conversation.ts.
The imports, tools, runTool and runTools are identical to lesson 3. Only the chat, the loop and the main loop change.
What changes
Create a conversation
// The conversation is stored by OpenAI (Conversations API). Every response // created with `conversation: id` automatically appends its input and output // items to it, so each request sends only the NEW items, not the full history. const conversation = await client.conversations.create();
The history array is gone.
Send only new items
const chat = (input: ResponseInput): Promise<Response> => client.responses.create({ model, // Instructions are not carried over by the conversation, so send them each time instructions: basicTravelSystemPrompt, conversation: conversation.id, input, tools, });
Compared with lesson 3, store: false and include: ["reasoning.encrypted_content"] are removed, and conversation is added. Because OpenAI keeps the items, including reasoning, you don't have to round-trip the encrypted reasoning yourself. instructions still have to be sent every time.
A simpler loop
const runTurn = async (input: ResponseInput): Promise<Response> => { while (true) { const response = await chat(input); const calls = response.output.filter( (item): item is ResponseFunctionToolCall => item.type === "function_call", ); if (calls.length === 0) return response; input = await runTools(calls); } };
The loop no longer copies output items into a history. The tool outputs become the next input.
The main loop
const rl = createInterface({ input: process.stdin, output: process.stdout }); try { let input: ResponseInput = [ { role: "user", content: await askHolidayPrompt(rl) }, ]; while (true) { const response = await runTurn(input); console.log(`\n${response.output_text}\n`); if (response.status !== "completed") { const reason = response.incomplete_details?.reason ?? "unknown"; console.warn(`[stopped early: ${response.status}] ${reason}\n`); } // Press return on an empty line to exit const followUp = (await rl.question("Follow-up (return to exit): ")).trim(); if (!followUp) break; input = [{ role: "user", content: followUp }]; } } finally { rl.close(); }
Each follow-up sends one new user message, not the whole conversation.
Which should you use?
| Client-side history (lesson 3) | Conversations API (lesson 4) | |
|---|---|---|
| Where the history lives | Your code | OpenAI |
| Request size | Grows every turn | Only the new items |
| Code | More: you manage items and reasoning | Less |
| Control | Full: edit, trim or summarise history | Less direct |
| Data retention | Nothing stored (store: false) | Stored by OpenAI |
Use client-side history when you need control over the history or don't want data stored. Use the Conversations API when you want simpler code and smaller requests. Anthropic's Messages API offers only the client-side style, which is why Module 1 has a single version.
👉 Next: Lesson 5: Showing Reasoning