Module 2, Lesson 2: Basic Chat with OpenAI

A multi-turn terminal chat with instructions, using the OpenAI Responses API and client-side history.

Published: 11/22/2026

Lesson 2: Basic chat

src/openai/basic-chat.ts:

import dotenv from "dotenv";
import { createInterface } from "node:readline/promises";
import OpenAI from "openai";
import type { ResponseInput } from "openai/resources/responses/responses";
import { basicTravelSystemPrompt } from "../common/prompt-ref.js";
import { model } from "./config.js";

dotenv.config();

const client = new OpenAI();

// History is kept here and resent in full each turn (no previous_response_id
// or Conversations API), so `store: false` keeps the server stateless.
const history: ResponseInput = [];
const rl = createInterface({ input: process.stdin, output: process.stdout });

try {
  while (true) {
    const input = (await rl.question("You (return to exit): ")).trim();
    if (!input) break;
    history.push({ role: "user", content: input });

    const response = await client.responses.create({
      model,
      instructions: basicTravelSystemPrompt,
      input: history,
      store: false,
    });

    history.push({ role: "assistant", content: response.output_text });

    console.log(`\n${response.output_text}\n`);
  }
} finally {
  rl.close();
}

Run it with pnpm tsx src/openai/basic-chat.ts.

How it works

  • instructions is the system prompt. It applies to this request only, so it is sent every time.
  • input is the history. Here it is a list of simple { role, content } messages that your code keeps and resends.
  • store: false tells OpenAI not to keep the response, so the server holds no state. Everything the model needs is in input.
  • output_text is a convenience that joins all the text in the reply. Compare this with Anthropic, where you loop over content blocks.

This matches the Anthropic version in Module 1 almost line for line. The differences are the parameter names and the shape of the reply.

👉 Continue to Lesson 3: Chat with tools