Typorithm · medium

Few-Shot & Chain-of-Thought

few-shot-cotllmfew-shotchain-of-thoughtexamples
Saved on this device
Full lesson · 8 learning steps Open any exercise for focused practice
1

Step 1 of 8 · intro card

Learn Few-Shot & Chain-of-Thought

0%

Prompt Engineering

Few-shot prompting puts example user/assistant turns in the messages list to steer format and behavior, while chain-of-thought asks the model to reason step by step to improve accuracy on hard problems.

Few-shot prompting means including a few example turns — a sample question and its ideal answer — before the real request. The model imitates the demonstrated format and style.

Chain-of-thought (CoT) asks the model to reason step by step before answering. Surfacing the intermediate steps reliably improves accuracy on math, logic, and multi-step problems.

You can also prefill the start of the assistant turn to constrain the output — beginning the reply for the model so it continues in exactly the shape you need.

01

Few-shot: example turns steer format and behavior.

02

Chain-of-thought: 'think step by step' improves reasoning.

03

Prefilling the assistant turn constrains the output.

const messages = [
  { role: "user", content: "2+2?" },
  { role: "assistant", content: "4" },
  { role: "user", content: "Think step by step: 17*23?" },
];
// few-shot examples steer format; CoT improves reasoning

Read the implementation

Connect each part of the code to the concept before you type.

1. Messages array

A few-shot prompt is just a messages array that includes example turns before the real question.

const messages = [

2. Example question

An example user turn shows the model the kind of input it will be asked about.

{ role: "user", content: "2+2?" },

3. Example answer

The paired assistant turn demonstrates the exact answer format you want back.

{ role: "assistant", content: "4" },

4. Chain-of-thought ask

Asking the model to think step by step elicits intermediate reasoning, improving accuracy on hard problems.

{ role: "user", content: "Think step by step: 17*23?" },

5. Why it works

Few-shot examples shape the output format while chain-of-thought improves the reasoning behind it.

// few-shot examples steer format; CoT improves reasoning

A complete learning loop

01

Understand

Read the concept map and follow the code breakdown.

02

Reconstruct

Type the implementation, repair mistakes and work through every recall exercise.

03

Verify

Use the final check without hints; revisit weak concepts in short practice sessions.

Few-Shot & Chain-of-Thought: Lesson & Practice · Typorithm | ApplyDjinn