Building with LLMs (TypeScript) · Chapter 9 of 24

Prompt basics

By the end of this chapter you can write a prompt that says exactly what you want, keep your instructions apart from the customer's text, and show a model examples of the answers you expect. Build it in your browser: a real editor and terminal, AI that explains, and tests that tell you when it works.

  • Part: Reliable output
  • 4 tasks
  • TypeScript
  • In the all labs plan

The first 2 chapters of Building with LLMs (TypeScript) are free. This one comes with the all labs plan.

Why you need this

Lata reads every WhatsApp message twice: once to see what the customer wants, once to pass it to the right person. She asked for a program that puts a label on each message, so refunds go to one pile and delays to another. The first version sent each message with the question "What is this message about?", and the replies were sentences, a different shape every time. Nobody can sort a pile by a sentence.

Code does what it says. A prompt is different: it is text that a model reads and predicts a continuation of, so a vague prompt gets an answer to the question the model guessed you meant. The fix is not a magic phrase. It is the same care you put into a function's type signature and its doc comment: say what the job is, what the answer may be, and what shape it comes back in.

What you build: 4 tasks

Each task has tests. You write the code in the lab's editor and press Check.

  1. Task 1

    clear-instructions.ts: clear instructions

    Lata wants every WhatsApp message labelled with its intent. For the first 8 messages in /workspace/data/messages.jsonl, ask lab-chat with two messages:

    a system message with your instructions: classify the customer's message as exactly one of booking_request, cancellation, refund_status, delay_complaint, fare_query, lost_item, date_change, praise, and reply with only the label;

    then the customer's text as the user message.

    Print one line per message: its id, a space and the reply, for example:

    M200 lost_item

  2. Task 2

    delimiters.ts: instructions and data, apart

    Export a function wrap(text: string): string that returns the text between a line <message> and a line </message>, after taking out any <message> or </message> already inside the text.

    When the file is run, ask lab-chat to summarise Meera's complaint (in the starter) in 2 sentences: the system message holds only your instruction, saying the message is between the tags; the user message is wrap of the complaint. Print only the reply.

    The check imports the file to test wrap, so the call to the model goes inside if (import.meta.main) { ... }

  3. Task 3

    few-shot.ts: show the model examples

    Export a function fewShot(examples: [string, string][], text: string): ChatCompletionMessageParam[] that returns the messages for a few-shot request: a system message (classify as one of the eight labels of task 1, reply with only the label); then, for each [exampleText, label] pair in order, a user message with exampleText and an assistant message with label; last, a user message with text.

    When the file is run, use M002, M005 and M007 from messages.jsonl as the examples (their text and their intent) and label M021 to M026 with lab-chat. Print each id, a space and the reply.

    Put the calls to the model inside if (import.meta.main) { ... }

  4. Task 4 · interview question

    Interview: fix-prompt.ts, fix Lata's prompt

    fix-prompt.ts should label M101 to M106, printing each id and one of the eight labels, but it prints greetings. Its prompt has three problems. Fix the prompt so every line is the id and exactly one of the labels.

    Keep the LABELS array and the loop. The customer's text must reach the model as the user message, and the system message must hold only your instructions.

What you will learn

  • Clear instructions: the job, the allowed answers and the format.
  • Delimiters: marking where the customer's text starts and ends.
  • Few-shot examples: showing the model finished answers.
  • What the lab model does with a prompt, and what real models gain.

More in Reliable output

Build "Prompt basics" yourself

SafarLine, an intercity bus company, drowns in WhatsApp messages. You build its first AI assistant, in TypeScript. A real editor and terminal in your browser, AI that explains, and a Check that says when it works.

The first 2 chapters of Building with LLMs (TypeScript) are free. This one comes with the all labs plan.