In-Context Learning: Teaching a GPT Without Retraining
In-context learning is like giving an LLM a cheat sheet in the prompt. You provide examples of a task, and the model follows the pattern for your new query without any permanent changes. Use it for one-off tasks like reformatting text or classifying sentiment.
WHY IT EXISTS: Training a massive model like a GPT is incredibly expensive and slow. In-context learning provides a way to guide the model's behavior for specific tasks on the fly, without needing to update its underlying weights. It makes a general-purpose, pre-trained model adaptable to new tasks instantly.
THE MENTAL MODEL: Think of it as a conversation with a brilliant but forgetful assistant. You can show them exactly how you want a task done by providing a few examples. They'll pick up the pattern instantly and apply it to the next thing you ask, but as soon as you start a new conversation, they've forgotten everything you just showed them.
HOW IT WORKS: When you send a prompt to a GPT, the entire text—your instructions, examples, and final query—is converted into tokens and fed into the model. The transformer architecture's self-attention mechanism processes this entire context. By seeing the relationship between the example inputs and their desired outputs, the model identifies the pattern. It then applies this inferred pattern to generate a response for your final query. This all happens in a single forward pass; no model weights are updated.
WHEN TO USE IT: Use it for any task where you need to steer a pre-trained model's output without permanent changes. This is the foundation of prompt engineering. It's ideal for quick, one-off tasks like summarizing text in a specific format, translating between languages with a certain tone, or classifying data into custom categories. Providing a few examples (few-shot learning) generally yields better results than just describing the task (zero-shot).
WHEN NOT TO USE IT: Don't use in-context learning when you need the model to gain deep, permanent knowledge about a new domain or consistently apply a specific personality. The 'learning' is temporary and limited by the size of the model's context window. For permanent changes, you need to fine-tune the model on a dedicated dataset.
ONE CANONICAL EXAMPLE: To get a GPT to generate JSON from unstructured text, you can show it an example. Your prompt would contain: 'Translate the following user data to JSON. User: name: John Doe, age: 30, city: New York. JSON: {"name": "John Doe", "age": 30, "city": "New York"}. Now, do the same for this user: name: Jane Smith, age: 25, city: London. JSON:'. The model sees the pattern and completes it for Jane Smith.
Read the original → en.wikipedia.org
Get five bites like this every day.
Tezvyn delivers a daily feed of 60-second tech bites with quizzes to lock in what you learn.