Sign in to save

Bookmark this page so you can find it later.

Sign in to save

Bookmark this page so you can find it later.

In-context learning is a way an AI model can appear to learn a new task from examples placed inside the prompt. Instead of changing its stored weights, the model uses the pattern in the given examples to decide what to output next. This matters because it lets people guide an AI system quickly, using instructions and sample inputs rather than new training data.

It is one reason modern chatbots can adapt to many school, coding, math, and writing tasks.

Understanding AI & Machine Learning: In-Context Learning Explained

Inside a language model, each word piece is turned into numbers. The model processes these numbers through many layers. A mechanism called attention helps it compare one part of the prompt with another part.

When it sees several worked examples, attention can connect matching features. It may notice that a certain kind of input is followed by a certain label, style, or calculation step. The model does not store this pattern permanently during the conversation.

Instead, the pattern is held in its current processing state while it generates a response. This is closer to following a temporary rule from a worksheet than studying a subject for months.

The examples need to make the hidden rule clear. Suppose a prompt asks for movie reviews to be marked positive or negative. Examples should show typical reviews, the desired label spelling, and the exact output layout.

If one example uses a full sentence and another uses only one word, the model may copy the inconsistency. If labels are swapped by mistake, the model can faithfully follow the wrong pattern. This reveals an important fact about AI output.

A confident answer can come from a weak prompt. Clear boundaries between examples, input text, and expected answers reduce avoidable errors.

Students meet this idea whenever they give an AI tool a model answer before requesting similar work. A teacher may provide a sample lab conclusion, then ask students to write conclusions from new results. In programming, one example can show how a function should receive data and return a result.

In language learning, examples can demonstrate the tone of a formal email or the format of vocabulary flashcards. These uses can save time, but they do not replace checking the underlying work. For a physics calculation, the AI might imitate the structure of a solved problem while using the wrong unit or choosing an unsuitable formula.

In-context learning has limits that are easy to miss. The model can focus on surface patterns instead of the real meaning. It may match an answer to a familiar-looking example even when a small detail changes the problem.

Long prompts create another issue. Early instructions can be overlooked, while repeated information can distract from the important part. Some tasks need reliable facts, careful reasoning, or current data that the prompt cannot supply.

When learning to use AI, test it with a few cases whose answers you already know. Change one detail at a time.

Check facts, units, assumptions, and formatting. Treat the response as a draft produced from patterns, not as proof that the model understood the subject.

Key Facts

  • In-context learning uses examples in the prompt, not extra training, to guide the model's next output.
  • A prompt can include instructions, examples, a question, and formatting rules.
  • The model predicts the next token using probabilities: P(next token | context).
  • Few-shot prompting means giving a small number of examples, such as 2 to 5, before the new problem.
  • More relevant examples usually improve results, but too many examples can confuse the model or exceed the context window.
  • Accuracy can be estimated as accuracy = correct answers / total questions.

Vocabulary

In-context learning
A model behavior where an AI uses information and examples inside a prompt to handle a task without updating its weights.
Prompt
The text, data, instructions, and examples given to an AI model before it generates an answer.
Token
A small unit of text, such as a word, part of a word, number, or symbol, that a language model processes.
Context window
The maximum amount of prompt and conversation information a model can consider at one time.
Few-shot example
A sample input and correct output included in a prompt to show the model the pattern to follow.

Common Mistakes to Avoid

  • Thinking the model is permanently learning from the prompt, which is wrong because in-context learning usually does not update the model's stored weights.
  • Giving examples that do not match the final question, which is wrong because the model may copy the wrong pattern or format.
  • Using vague instructions like answer correctly, which is wrong because the model needs clear rules, labels, and output format to infer the task well.
  • Assuming more examples always help, which is wrong because irrelevant or excessive examples can waste context space and reduce clarity.

Practice Questions

  1. 1 A student gives an AI 4 example input-output pairs and then asks it to solve 6 new items. The AI gets 5 of the 6 new items correct. What is its accuracy on the new items?
  2. 2 A prompt has 120 tokens of instructions, 3 examples that are 80 tokens each, and a final question that is 60 tokens. How many total tokens are in the prompt?
  3. 3 You want an AI to convert temperatures from Celsius to Fahrenheit. Explain why giving two worked examples before the new temperature could help the model, and name one risk if one example contains a mistake.