Instructing AI by Example · scripted
Instructing AI by Example
Two ways to teach
So far, almost every program we have written has been a set of instructions. Double the number. Turn these notes into a flash card. Organize the observations into a table. We say what to do, and AI does it. This is the most natural way to tell anyone what we want, and it takes us a long way.
But we can also teach AI what to do without giving it instructions. In an earlier lesson we saw that when we work with ChatGPT, Claude, or Gemini, we are working with a large language model, or LLM. Large language models are experts at recognizing patterns. Showing an LLM what to do, instead of writing instructions for what to do, turns out to be extraordinarily powerful. That second way, showing it what to do, is teaching by example. Teaching by example is what this lesson is about.
Teaching by example is also known as in-context learning. The LLM learns how to perform the task from the context in the prompt, the examples we show it, rather than from any change to the model itself. Nothing is retrained. The examples are in the conversation, and the LLM uses them the same way it uses any other context. You may also see the term few-shot prompting, which means a prompt that includes a few examples, or a "few shots," of the task before asking for a new one. A prompt with no examples at all is sometimes called zero-shot. These are all names for the same idea: the examples in the prompt teach the LLM what to do.
How would you teach someone to write like your favorite author?
Suppose that is the assignment. The first way we might reach for is to write down the rules.
How to Write Like Hemingway: A Complete Manual (volume 1 of 14)
… continued in volumes 2 through 14. Exceptions begin in volume 6.
What would we actually do in the real world? We would show someone a short story by Hemingway. We would have them read it, think about how Hemingway writes, and then try to mimic the style. That might be an assignment you are given: write in the style of Hemingway. Nobody hands you a thousand rules to read first. They have you read some of his writing, because humans are good at recognizing patterns.
We can teach an LLM the same way. We show it examples of what we want it to do and let it figure out, on its own, how to do that. We never write the rules down. For example, we can show it a sample of how we want it to write and then ask it to write in the same style. We can show it a sample angry email from a customer and how we think the reply should be worded, and it learns how to respond to the next angry email. We can show it pictures of things and the names we use for them, and it learns to identify and name those things.
We are going to explore how to show an LLM examples of what we want and how to correctly perform the work. Sometimes we show it correctly completed work. Sometimes we show it incorrectly completed work and tell it that this is wrong. An LLM can learn from correct examples, and it can learn from incorrect ones.
Provide examples rather than rules
Start with a task we can teach both ways, so we can compare them. The task is to reverse a sentence character by character. The first conversation teaches it by instruction.
Whenever I give you input, write "output:" followed by the input reversed character by character.
Understood.
input: the cat sat
output: tas tac eht
The second conversation teaches the same task by example. There is no instruction anywhere in it. There are only pairs of input and output, and then a new input with the output left blank.
input: red car output: rac der
input: big dog output: god gib
input: the cat sat output:
tas tac eht
Both conversations accomplish the same thing. The first is teaching by instruction: we stated the rule, and the LLM applied it. The second is teaching by example: we never stated the rule. The LLM looked at two pairs, inferred the pattern, and completed the third pair. In this case the two ways take about the same effort, because the rule is easy to write down. Next we look at a case where the rule is not easy to write down.
A classifier, taught in one prompt
Classification means putting something into a category. You can think of it as categorization: we take an item and give it a label, and the label is the category it belongs to. Sorting email into “urgent” and “later” is classification. So is deciding whether a review is positive or negative.
Now imagine we want an LLM to learn what music we like. Writing down the rules for how we decide what music we like is hard. We may not know the rules ourselves. But we can go through a list of songs we have heard, give each one a label, and let the LLM learn from the examples. The labels are love, like, okay, don’t like, and can’t stand. (Every song and artist below is invented.)
input: “Neon Harbor” by The Lantern Keys (fast synth-pop, huge chorus) label: love
input: “Slow River Morning” by Wren Calloway (quiet acoustic guitar and voice) label: okay
input: “Bassline Riot” by DJ Voltcrank (loud club track, no vocals) label: can’t stand
input: “Static Hearts” by Paper Satellites (indie rock, catchy hooks, fast) label: like
input: “Endless Elevator” by The Smooth Committee (slow lounge cover of a pop song) label: don’t like
input: “Glass Weather” by Marigold Static (mid-tempo synth-pop, singalong chorus) label: love
input: “Ten Thousand Hooves” by Grandpa Copper (fast bluegrass, banjo) label: okay
input: “Midnight Circuit” by The Lantern Keys (fast synth-pop, bright chorus) label:
love
Nobody explained the labels. Nobody wrote a rule such as “fast songs with a big chorus are loved, and slow instrumental songs are not.” Seven labeled examples were enough for the LLM to see the pattern and apply it to a song it had never seen. It answered with the label and nothing else, because every example ended with the label and nothing else. If we wanted the classifier to be more accurate, we would add more examples, not more rules.
Examples can teach a boundary
Sometimes the examples we have are not enough. They do not make it clear where the boundary is: the line between what belongs in a category and what does not. When every example sits comfortably inside the category, the LLM has never seen the edge, so it has to guess where the edge is. The fix is one more example that sits on the edge and shows which side it belongs on.
Suppose we want an LLM to pull a shopping list out of text messages from home. The first conversation gives it two examples. In both of them, everything mentioned in the message goes into the list.
input: “Can you grab milk, eggs, and bread on the way home?” list: milk, eggs, bread
input: “We’re out of coffee and paper towels.” list: coffee, paper towels
input: “Pick up bananas, rice, and yogurt, and pick up your sister from practice.” list:
bananas, rice, yogurt, your sister
The LLM did exactly what the examples showed. In every example, every item after “grab” or “out of” went into the list, so it put every item after “pick up” into the list, including a person. We knew the list was for groceries. The examples never showed that, because none of them contained anything that was not a grocery. The boundary was never taught.
The second conversation adds one example. The new example contains something that should be left out, and the list leaves it out. Then it sends the same message again.
input: “Can you grab milk, eggs, and bread on the way home?” list: milk, eggs, bread
input: “We’re out of coffee and paper towels.” list: coffee, paper towels
input: “Grab oranges and dish soap, and grab the mail on your way in.” list: oranges, dish soap
input: “Pick up bananas, rice, and yogurt, and pick up your sister from practice.” list:
bananas, rice, yogurt
One example did the work. It showed the LLM a message with something in it that is not a grocery, and a list that leaves that thing out. The LLM inferred the boundary from that one example and applied it to the message about the sister. We could have written the boundary as a rule, such as “only include things you can buy at a store.” But we did not have to, and often we do not notice a boundary until the LLM crosses it. When that happens, the repair is the same every time: add one example that shows the correct side.
A gallery of little machines
Once you see the pattern, it is hard to stop building these. Each of the following would have been a serious project a few years ago. All three are taught the same way: examples first, then a new case.
What apartment listings actually mean:
“cozy” → under 500 square feet “charming” → built before 1950, nothing updated “up-and-coming neighborhood” → not yet “efficient layout” → the kitchen is in the living room “natural light” → one window, morning only
Decode: “full of character”
Something is visibly wrong and they are hoping you find it endearing. Slanted floors, a radiator that bangs, or a bathroom door that does not close. Ask what, specifically.
Here is how I group the reasons people give for missing a meeting:
“my laptop updated overnight” → Technology failed “the bus was late” → Getting there “I thought it was next week” → Calendar confusion “the file wouldn’t open” → Technology failed “the highway was closed” → Getting there “I was sure it was online” → Calendar confusion “my phone died” → Technology failed
Which group does this one go in?
“I had it down for Thursday”
Calendar confusion
Nightly temperature readings from the walk-in freezer at the restaurant where I work. Here are two normal nights:
Normal night 1
- 02:00 −18.1
- 03:00 −18.0
- 04:00 −18.2
- 05:00 −18.1
- 06:00 −17.9
- 07:00 −18.0
- 08:00 −18.1
Normal night 2
- 02:00 −18.0
- 03:00 −18.1
- 04:00 −18.1
- 05:00 −17.9
- 06:00 −18.2
- 07:00 −18.0
- 08:00 −18.0
Observed last night
- 02:00 −18.0
- 03:00 −18.1
- 04:00 −17.3
- 05:00 −18.0
- 06:00 −18.1
- 07:00 −18.0
- 08:00 −18.1
Is anything out of the ordinary, and why?
The 04:00 reading of −17.3 is out of the ordinary. On both normal nights every reading stays between −17.9 and −18.2, and no reading ever gets warmer than −17.9. The 04:00 reading last night is more than half a degree warmer than anything in the normal nights, and the readings on either side of it are normal, so the freezer warmed briefly and recovered within the hour. That is small, but it is not something the normal nights ever do. The most likely causes are a door that was opened around 04:00 or a defrost cycle that ran longer than usual. It is worth checking whether anyone was in the freezer at that time and watching the 04:00 reading tomorrow night.
Three prompts. The first decodes meaning that lives under the words. Five examples taught it, and the LLM decoded a phrase that was not in the examples. The second sorts a new excuse into a group. Seven examples taught the groups, and the LLM put the new excuse in the right one without anyone defining what “calendar confusion” means. The third spots something slightly off. Two normal nights taught what normal looks like, and the LLM found the one reading that did not fit and explained why. Notice that the odd reading is not wildly wrong. It is off by less than a degree, and the LLM found it because the examples showed how tight “normal” is.
Before large language models, doing what we just did in these examples required complex AI skills. You had to know how to train deep learning models, which people studied for years, and training one took huge amounts of computing power and time. Now you can build and do many of the same things, often at better accuracy than the old techniques could reach, in a single prompt. When someone talks about training an AI model, ask yourself whether it is something you could teach an LLM to do through examples. You might be able to build it for them in one message.
Think step by step
So far the examples taught answers. The next idea teaches a process for reasoning through a problem. Consider a person: if you have them write down their plan first, and the plan is right, the answer that follows is much more likely to be right. The same holds for an LLM. An LLM predicts the next words from the words that came before. If the LLM writes down a step-by-step plan to solve the problem first, and the plan is correct, it is much more likely to output the correct solution as the next words it predicts.
Answer with only the final number. A bus leaves every 45 minutes starting at 6:00 a.m. I arrive at the stop at 9:10. How many minutes until the next bus?
20
Forced to jump straight to the answer, the LLM had to land on the number in one move, and it got the answer wrong. Now the only change is one sentence at the end of the prompt.
A bus leaves every 45 minutes starting at 6:00 a.m. I arrive at the stop at 9:10. How many minutes until the next bus? Think step by step before answering.
- Departures are 6:00, 6:45, 7:30, 8:15, 9:00, 9:45, and so on.
- I arrive at 9:10, so the 9:00 bus has left.
- The next bus is 9:45.
- 9:45 minus 9:10 is 35 minutes.
Answer: 35 minutes.
The steps are not decoration. Each correct step makes the next correct words more likely. By the time the LLM writes the answer, the answer is simply the next thing that follows a correct plan.
This technique is called chain of thought. A chain of thought is the sequence of reasoning steps the LLM writes down on the way to an answer. Because an LLM produces each word based on the words before it, the chain of thought becomes part of the context for the answer. When the chain is right, the answer that follows it is usually right. When the LLM is forced to skip the chain and jump straight to the answer, it has to get there in one guess, and on anything with more than one step, the guess is often wrong.
"Think step by step" is the simplest way to ask for a chain of thought. It leaves the LLM to decide what the steps are. That works for many problems. For problems where we care about how the reasoning is done, we can go further and show the chain of thought we want, which is the next idea.
Teaching the reasoning for solving a type of problem by example
Now put the two ideas together. “Think step by step” asks for some reasoning, and the LLM decides what the steps are. We can go further and show a worked example of the reasoning we want, and the LLM will reason our way.
Here is how I check whether a headline is supported by the article under it.
Headline: “Study finds video games improve grades.” Article: “Researchers surveyed 200 students at one school. Those who reported playing games reported feeling more focused in class.”
Think step by step:
- What does the headline claim? Games improve grades.
- What was actually measured? Self-reported focus, not grades.
- Does the sample support the claim? 200 students at one school, self-reported. Weak.
- Verdict: NOT SUPPORTED. The headline changes both the thing measured and the scope.
Now check this one the same way:
Headline: “Protected bike lanes linked to fewer cyclist injuries.” Article: “After the city added 40 miles of protected bike lanes, reported cyclist injuries fell 22 percent over the following three years, while injuries in comparable cities that added no lanes stayed flat.”
Think step by step:
- What does the headline claim? Protected bike lanes are linked to fewer cyclist injuries.
- What was actually measured? Reported cyclist injuries, before and after the lanes were added, compared with similar cities that added none.
- Does the sample support the claim? Three years, a whole city, with a comparison group. Reasonable for a link, though not proof of cause.
- Verdict: SUPPORTED. The headline says “linked,” not “caused,” and stays within what was measured.
Compare this with the bus. There, the LLM invented its own steps. Here it walked our four steps, in order, applying our tests, including the same skepticism about what was measured and for whom. “Think step by step” asks for a chain of reasoning. A worked example designs it.
Teaching a voice
Now the case where rules fail almost completely. Try to write rules for how you write. Every style guide sounds the same, and none of them captures a voice. Samples do.
Here are three passages of my writing:
“The bus was late again. Nobody at the stop looked surprised. There is a lesson in that, and it is not about buses.”
“I spent a summer counting things nobody cared about. Best summer I have had. It taught me the difference between data and evidence.”
“You do not need a better argument. You need a better question. Most of debate club is convincing people of the second sentence.”
In my style, write a short opening for a talk about why everyone should learn statistics.
Nobody falls in love with statistics. They fall in love with a question, and then statistics turns out to be the only honest way to answer it. My job today is to get you to the question.
Short sentences. A concrete scene first, the lesson second. A turn in the last line. None of that was stated. All of it was shown, and the LLM absorbed it from three passages. Some knowledge is like this: you can recognize it and demonstrate it, but you cannot fully write it down. Examples are the only language it has.
Refining by pointing
So far the examples came first and the result came after. Now reverse it, because this becomes the simplest way to improve a prompt you already have. Instead of explaining what was wrong, point at it.
The refinement loop
The important part is step 3. Every preferred and not-preferred sentence is copied word for word from the response the LLM just produced. We do not invent them.
Write a short description of our dog-walking business, Maple Street Dog Walking, for a flyer. We walk dogs in the Maplewood and Riverside neighborhoods, weekdays from 7 a.m. to 6 p.m. Every walk is 30 minutes, and we send the owner a photo after each walk.
Maple Street Dog Walking offers premium, tailored care experiences for your beloved companion. We understand that your dog is a member of the family, and we treat every walk as an opportunity to enrich their day. Every dog gets a 30-minute walk, fresh water, and a photo sent to your phone before we leave. We serve Maplewood and Riverside, weekdays from 7 a.m. to 6 p.m. Our passionate team is committed to exceeding your expectations at every turn. Rain or shine, we show up, because your dog does not check the forecast.
Write a short description of our dog-walking business, Maple Street Dog Walking, for a flyer. We walk dogs in the Maplewood and Riverside neighborhoods, weekdays from 7 a.m. to 6 p.m. Every walk is 30 minutes, and we send the owner a photo after each walk.
Preferred, sentences like these:
- “Every dog gets a 30-minute walk, fresh water, and a photo sent to your phone before we leave.”
- “Rain or shine, we show up, because your dog does not check the forecast.”
Not preferred, never sentences like these:
- “Maple Street Dog Walking offers premium, tailored care experiences for your beloved companion.”
- “Our passionate team is committed to exceeding your expectations at every turn.”
Maple Street Dog Walking. Weekdays from 7 a.m. to 6 p.m. in Maplewood and Riverside. Every dog gets a 30-minute walk, fresh water, and a photo sent to your phone before we leave. Rain or shine, we show up, because your dog does not check the forecast. Text us to book a first walk.
All four quoted sentences came from the first response. Two of them were plain and specific, so we marked them preferred. Two of them were the kind of sentence that could be on any flyer for any business, so we marked them not preferred. The LLM kept the two preferred sentences almost word for word, dropped the two not-preferred ones, and wrote the rest to match the preferred ones. “Avoid buzzwords” is itself vague. “Never sentences like these” is exact, because the LLM can see exactly what we mean. When an LLM produces something you do not want, the strong move is to copy the mistake itself into the prompt as a not-preferred example, and to copy the best sentence it has produced as a preferred one.
Examples are the densest instruction we have. A rule describes a pattern and leaks at every edge. An example shows the pattern, edges included. The best prompts often mix them: a short rule for the intent, and examples for everything the rule cannot say.