🤖 AI/ML

In-Context Learning

In-Context Learning

The ability of an LLM to learn a new task from examples in the prompt alone. Adapts without fine-tuning.

📖 Detailed Explanation

In-Context Learning (ICL) is the ability of a large language model (LLM) to perform a new task using only the examples provided within the prompt. Without updating the model parameters (fine-tuning), it learns patterns from a few examples (few-shot) included in the prompt and applies them to new inputs.

ICL first drew attention in the 2020 GPT-3 paper "Language Models are Few-Shot Learners." Rather than changing weights through backpropagation as in conventional supervised learning, it works by having the pretrained model understand and reason over the context of the prompt.

It is divided into Zero-shot (no examples), One-shot (1 example), and Few-shot (2–5 examples). According to research, 5–20 examples are optimal, and beyond that, an "Over-prompting" phenomenon in which performance actually degrades can occur.

In practice, it is effective to use at least 2 and at most 5 examples, and to include both positive and negative examples. There are research findings that the format and input distribution of the examples have a greater impact on performance than their correct labels.

💻 Code Example

from openai import OpenAI

client = OpenAI()

# Few-shot In-Context Learning 예시: 감정 분류
def classify_sentiment_icl(text):
    prompt = """다음은 텍스트와 감정 분류 예시입니다:

예시 1:
텍스트: "이 제품 정말 최고예요! 매일 사용하고 있습니다."
감정: 긍정

예시 2:
텍스트: "배송이 늦고 포장도 엉망이었어요. 실망입니다."
감정: 부정

예시 3:
텍스트: "보통이에요. 가격 대비 적당합니다."
감정: 중립

이제 다음 텍스트의 감정을 분류해주세요:
텍스트: "{text}"
감정:"""

    response = client.chat.completions.create(
        model="gpt-4o",
        messages=[
            {"role": "user", "content": prompt.format(text=text)}
        ],
        max_tokens=10,
        temperature=0
    )

    return response.choices[0].message.content.strip()

# 테스트
test_texts = [
    "완전 강추합니다! 친구들에게도 추천했어요.",
    "그냥 평범해요. 다시 살지는 모르겠네요.",
    "절대 사지 마세요. 시간 낭비였어요."
]

for text in test_texts:
    result = classify_sentiment_icl(text)
    print(f"텍스트: {text}")
    print(f"분류 결과: {result}\n")

🗣️ Say It Like This in Practice

💬 In a Meeting
"You can handle classification tasks with few-shot, without fine-tuning. If you put just 3–5 examples in the prompt, it classifies new data well too. It's great for quickly building a prototype when training data is scarce."
💬 In an Interview
"In-Context Learning means performing a task using only the examples in the prompt, without changing the model weights. It first drew attention with GPT-3, and there are research findings that the format and distribution of the examples have a greater impact on performance than their correct answers."
💬 In a Technical Discussion
"There's an Over-prompting phenomenon where putting in too many examples actually lowers performance. With GPT-4, 5–20 examples are optimal, and beyond that they actually get in the way. It's important to include both positive and negative examples."

Take a mock interview on this topic → WIA SOOM AI Interview · Start for free

⚠️ Common Mistakes & Cautions

❌
Indiscriminately adding many examples

Examples beyond 20 cause performance degradation. A representative set of around 5 examples is the most effective.

❌
Ignoring consistency in example format

If the format differs from example to example, it is hard for the model to identify the pattern. Provide examples with the same structure.

✅
Include a balance of positive and negative examples

Including not only correct answers but also incorrect examples helps the model learn the boundaries better.

🔗 Related Terms

📚 Learn More