Prompt chaining splits one job into steps
Prompt chaining means doing a job as a chain of small model calls, where each step's output is the next step's input. Routing means sending each request to the step or model that suits it.
They matter because one huge prompt that reads a ticket, sorts it and writes a reply fails in ways you cannot see. Small steps can each be checked.
By the end of this lesson
Note
I can split a model job into a checked chain with a router in front.
Key words
| Word | What it means |
|---|---|
| Chain | steps where one output feeds the next |
| Gate | a check between steps that can stop the chain |
| Router | code that picks where a request goes |
| Route | one place a request can be sent |
| Fallback | where a request goes when the router is unsure |
The cells students fill in are left blank in this preview.
Starter
Have a go
Starter
4 questionsA chain is small steps with checks between
A help desk ticket needs three jobs. Find the room and the problem. Decide how urgent it is. Write a reply.
As a chain, each job is its own call with a short prompt. Between steps sits a gate: plain code that checks the output. If step 1 found no room, the gate stops the chain and asks a person, rather than letting step 3 write a reply about nowhere.
A chain with a gate after step 1
flowchart LR
T["Ticket"] --> S1["Step 1: extract room and route"]
S1 --> G{"Gate: room and route found?"}
G -->|yes| S2["Step 2: urgency"]
S2 --> S3["Step 3: draft reply"]
G -->|no| P["Stop and ask a person"]Check
Have a go
Check
3 questionsA router sends each request to one route
A router reads a request and picks a route: refunds, passwords or outages. Here each route has a few words, and the route with the most words found wins.
Two rules keep it safe. A tie goes to the fallback, and so does a request that matches nothing. The fallback here is a person. A router can also be a cheap model call, and the same two rules apply.
Write the router and the chain, part 1
docker exec -w /home/node/project agent sh -c "printf '%s\n' 'const RULES = {\"refund\":[\"refund\",\"charged\",\"money back\"],\"password\":[\"password\",\"locked out\",\"log in\"],\"outage\":[\"down\",\"not working\",\"wifi\"]};' 'const HUMAN = \"human\";' 'const ROOMS = [\"B12\", \"A3\", \"L1\"];' 'function score(text, words) {' ' let n = 0;' ' for (const word of words) if (text.toLowerCase().includes(word)) n = n + 1;' ' return n;' '}' 'function route(text) {' ' let best = HUMAN;' ' let top = 0;' ' let tie = false;' ' for (const name of Object.keys(RULES)) {' ' const s = score(text, RULES[name]);' ' if (s > top) { best = name; top = s; tie = false; } else if (s === top && s > 0) tie = true;' ' }' ' return tie ? HUMAN : best;' '}' 'const MESSAGES = [' ' \"I was charged twice for the trip, can I get a refund?\",' > chain2.js"Shown here, not run. Press Start lab to type it into your own lab’s terminal.
Write the router and the chain, part 2
docker exec -w /home/node/project agent sh -c "printf '%s\n' ' \"I am locked out and forgot my password\",' ' \"The wifi in the library is down\",' ' \"Can I log in to get my money back?\",' ' \"What time does the canteen open?\",' ' \"My password stopped working after the update\",' '];' 'console.log(\"--- \" + \"route\" + \" ---\");' 'for (const m of MESSAGES) console.log(route(m) + \" <- \" + m);' 'const URGENT = [\"today\",\"now\",\"exam\"];' 'function chain(text) {' ' let room = \"\";' ' for (const r of ROOMS) if (room === \"\" && text.includes(r)) room = r;' ' const problem = route(text);' ' console.log(\"Step 1, extract\" + \": \" + \"room\" + \"=\" + (room || \"-\") + \" \" + \"route\" + \"=\" + problem);' ' if (room === \"\") return console.log(\"Gate\" + \": \" + \"stop, no room given\");' ' if (problem === HUMAN) return console.log(\"Gate\" + \": \" + \"stop, no route found\");' ' console.log(\"Gate\" + \": \" + \"passed\");' >> chain2.js"Shown here, not run. Press Start lab to type it into your own lab’s terminal.
Write the router and the chain, part 3
docker exec -w /home/node/project agent sh -c "printf '%s\n' ' const when = score(text, URGENT) > 0 ? \"today\" : \"this week\";' ' console.log(\"Step 2, urgency\" + \": \" + when);' ' console.log(\"Step 3, draft\" + \": \" + \"Thanks. We have logged your \" + problem + \" problem in room \" + room + \". Someone will come \" + when + \".\");' '}' 'const TICKETS = [' ' \"Room B12: the projector is not working and my exam is today\",' ' \"The wifi is down again\",' ' \"Room L1 needs a new chair\",' '];' 'for (const t of TICKETS) { console.log(\"--- \" + t); chain(t); }' >> chain2.js"Shown here, not run. Press Start lab to type it into your own lab’s terminal.
Route the messages and run the chain
docker exec -w /home/node/project agent node chain2.jsShown here, not run. Press Start lab to type it into your own lab’s terminal.
What the router printed
| Message | Route | Why |
|---|---|---|
| I was charged twice for the trip, can I get a refund? | refund | charged and refund, 2 words |
| I am locked out and forgot my password | password | locked out and password |
| The wifi in the library is down | outage | wifi and down |
| Can I log in to get my money back? | human | a tie: 1 for password, 1 for refund |
| What time does the canteen open? | human | no words matched |
| My password stopped working after the update | password | password, 1 word |
The cells students fill in are left blank in this preview.
Check
Have a go
Check
3 questionsWhat the chain printed
The projector ticket passed the gate with room B12 and route outage. Step 2 found the word exam, so the urgency is today, and step 3 wrote: Thanks. We have logged your outage problem in room B12. Someone will come today.
The wifi ticket stopped at the gate: no room given. The chair ticket found room L1 but no route, so it stopped too. Neither reached the step that writes a reply.
A common mistake
Warning
Many people think more steps always make a better chain. Each step adds a call, its cost and its delay, and a chance to fail. Add a step only when you can check its output, and keep the gates in code.
Match each part to its job
Have a go
Loading…
Your answers are marked here and are not kept. An account keeps your marks.
Assignment: route messages and gate a step
Build the router and the gate from the lab in Python.
score(message, words) counts how many of the words appear in the message, ignoring capital letters. route(message, rules, fallback) returns the rule name with the highest score. A tie for the highest score, or a highest score of 0, returns fallback.
route_counts(messages, rules, fallback) returns a dictionary counting where each message went, with every rule name and the fallback present, even at 0. gate(record, required) returns the first required field that is missing from the record or is "", or "" when all are there.
def score(message, words):
return None
def route(message, rules, fallback):
return None
def route_counts(messages, rules, fallback):
return None
def gate(record, required):
return None
Make a free account to save your work and track your progress.
Exit quiz
Have a go