Building with LLMs (TypeScript) · Chapter 10 of 24

Prompt patterns

By the end of this chapter you can build a system prompt from its parts, split a job into a chain of small requests, and add a self-check that catches invented answers, and you can say when each pattern helps and when it only costs tokens. 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

Zoya read online that a model answers better when you tell it who it is. She wrote a new system prompt for the WhatsApp assistant, sent with temperature: 0:

{ role: 'system', content: "You are SafarLine's most experienced support agent. " +
  'You know every SafarLine rule by heart.' }
{ role: 'user', content: 'Can I bring my dog?' }
Small pets are welcome on board in a carrier.

SafarLine carries no pets except guide dogs (policy, section 6.3). The prompt said the model knew the rules; it did not make that true. Chapter 9 gave you the basics of a clear prompt. This chapter is about the patterns people reach for next: personas, thinking step by step and self-checks. Each is real and useful, and each is oversold. Nikhil's rule: use a pattern when you can say what it adds.

What you build: 4 tasks

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

  1. Task 1

    system-prompt.ts: a system prompt with parts

    Export a function buildSystem(policy: string): string that returns SafarLine's system prompt in four parts, in order: a persona starting with You are; the rules (answer only from the policy, and say you don't know when it does not say); the format of the reply; and the policy text between a line <policy> and a line </policy>.

    When the file is run, ask lab-chat these two questions, one request each, with buildSystem of the text of /workspace/data/policy.md as the system message, and print each reply on its own line:

    Can I take my laptop in the hold?

    Is there a toilet on the bus?

    The check imports the file to test buildSystem, so the calls go inside if (import.meta.main) { ... }

  2. Task 2

    chain.ts: step by step, in a chain

    Answer the three questions in the starter in two steps each. Step 1: ask lab-chat to classify the question as one of booking_request, fare_query, cancellation, refund_status, date_change, delay_complaint, lost_item, praise, replying with only the label. Step 2: ask the question again with a system message holding only that label's section of /workspace/data/policy.md (from its heading to the next), inside <policy> and </policy>. The sections: booking_request 1, fare_query 2, cancellation 3, refund_status 3, date_change 4, delay_complaint 5, lost_item 7, praise 9.

    For each question print the label, a colon, a space and the answer.

  3. Task 3

    review.ts: a self-check that can see the facts

    Ask lab-chat in two requests. The draft: the question Can children travel free? alone, as one user message. The check: a system message telling the model to check the draft reply against SafarLine's policy and correct it, answering only from the policy, followed by the whole policy inside <policy> and </policy>; and a user message holding the draft reply word for word and the customer's question.

    Print two lines: Draft: and the first reply, then Checked: and the second reply.

  4. Task 4 · interview question

    Interview: fix-chain.ts, fix the broken chain

    fix-chain.ts answers three questions in two steps, but it has three mistakes: two crash, and one is silent. Fix them so that each step 1 reply is one of the eight LABELS, step 2 sends only that label's section (not the whole policy), and each line is the label, a colon, a space and the answer.

    Keep the questions, LABELS and the two steps.

What you will learn

  • Personas: what "You are ..." changes, and what it cannot.
  • A system prompt in parts: persona, rules, format, then the facts.
  • Step by step: reasoning inside one request, and a chain of requests where your code does the steps.
  • Self-checks: a second request that reviews the first, and why it needs the facts.

More in Reliable output

Build "Prompt patterns" 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.