Skip to main content

Follow-up Questions: The Conversation Lives in Your Code

Why This Matters

The API is stateless. A follow-up question only works because your code sends the whole conversation again, and every message you resend costs tokens and attention. Understanding this is the basis for everything about context management later in the course.

What a conversation really is

A conversation is an array of messages that you keep:

const messages: MessageParam[] = [
  { role: "user", content: buildTriageMessage(run, failure) },
  { role: "assistant", content: firstAnswer },
  { role: "user", content: "Why not flaky?" },
];

On every turn you send the whole array. The model reads all of it, answers the last question, and forgets everything again.

Build npm run ask

Add src/ask.ts: triage one failure, then read questions from the terminal.

const messages: MessageParam[] = [{ role: "user", content: buildTriageMessage(run, failure) }];

while (true) {
  const reply = await client.messages.create({ model, max_tokens: 1024, system: SYSTEM_PROMPT, messages });
  const text = reply.content.filter((b) => b.type === "text").map((b) => b.text).join("");
  console.log(text);
  messages.push({ role: "assistant", content: reply.content });

  const question = await rl.question("> ");
  if (!question) break;
  messages.push({ role: "user", content: question });
}

Add "ask": "tsx --env-file-if-exists=.env src/ask.ts" to package.json and try it on t-004, the search test with a patchy history: ask “why not flaky?”, then “what would change your mind?”.

Two details matter:

  • Push the assistant’s reply before the next question. Forget it and the model answers a question about an answer it never gave.
  • Free text here, not the schema. A conversation with a person is a different job from a verdict for a pipeline; you can use both.

Watch it grow

Print reply.usage.input_tokens on every turn. The number grows with every question, because you resend everything. On the fifth question you are paying again for the triage evidence, the first answer and all four earlier exchanges.

What a real session looks like

npm run ask on t-004: the first answer is cut off at max_tokens with 571 input tokens; the follow-up “why not a product bug” is answered with 1607 input tokens and stop_reason end_turn

Three things to notice in this real run:

  1. The input tokens jump from 571 to 1,607 after one question. The whole conversation is resent, including the first answer.
  2. The first answer was cut off: stop_reason: max_tokens with exactly 1,024 output tokens, while far less text was printed. The rest was most likely thinking, which the CLI does not print but which counts towards max_tokens too, so a free-text conversation needs a higher limit than a short JSON verdict. The solution branch now defaults to 4,096. Always check stop_reason before you trust an answer (lesson 27).
  3. Without a schema, the model invents its own shape: test_id, reasoning and open_questions instead of the fields in triageResultSchema. Fine for a person reading along, useless for a pipeline. That is why the verdict in lesson 9 uses structured output, and why this conversation does not.

Test first

it("sends the first answer with the second question", async () => {
  const create = vi.fn()
    .mockResolvedValueOnce({ content: [{ type: "text", text: "product_bug" }] })
    .mockResolvedValueOnce({ content: [{ type: "text", text: "because..." }] });
  await askOnce(client, run, failure, ["Why not flaky?"]);
  const second = create.mock.calls[1][0].messages;
  expect(second).toHaveLength(3);
  expect(second[1].role).toBe("assistant");
});

When history gets too long

Long conversations get expensive and important facts get buried. Options, in order of preference:

  1. Keep sessions short; start a new one for a new failure.
  2. Keep a small block of established facts and send it with every request (module 7, “a case file”).
  3. Summarise older turns.

Exercise

docs/exercises.md, lesson 11. Compare with solution/lesson-11-ask.

Show suggested answers

What do you send on the fifth question? Everything: the system prompt, the original evidence message, the first verdict, the four earlier questions with their four answers, and then the fifth question. That is 11 messages, alternating user and assistant. The API is stateless: it remembers nothing between requests, so the conversation exists only in the array your code keeps and resends. That is also why each question costs more than the previous one, and why caching the stable start of the conversation (lesson 14) pays off.

See solution/lesson-11-ask for npm run ask and the test that proves the second request contains the first answer.