liveBook · Manning

Few-shot learning

As explained in AI Agents and Applications

Prompt examples are illustrative instances supplied in a prompt to guide a language model toward a desired task behavior or output pattern.

Suppose an application must classify numbers using labels the model has never been given rules for. Without guidance, it cannot know what the labels mean. Few-shot learning puts several labeled examples directly in the prompt so the model can infer a pattern and apply it to new inputs. This is in-context learning, not fine-tuning. section 2.5.4 section 1.4.1

Teach a classification pattern

To classify numbers by divisibility by 5 and 7, include examples for neither divisor, each divisor alone, and both together. A sample can show the number, its divisibility, and its label: “70 // divisible by 5, divisible by 7 // Abra Kadabra.” The model then applies the pattern to numbers not shown in the examples. section 2.5.4

Build and check a few-shot prompt:

  • State the task and supply examples in a consistent format, including the distinctions the model must learn. section 2.5.4
  • Test the response rather than assuming the examples were enough: two palindrome examples still led the model to overlook 99 when summing palindromes. section 2.5.2
  • If the task needs a procedure, add explicit steps; chain-of-thought examples pair sample answers with the logical steps used to reach them. section 2.5.2 section 2.5.6
prompt_input = """Classify the following numbers as Abra, Kadabra 
or Abra Kadabra:

3, 4, 5, 7, 8, 10, 11, 13, 35

Examples: 
6 // not divisible by 5, not divisible by 7 // None
15 // divisible by 5, not divisible by 7 // Abra
12 // not divisible by 5, not divisible by 7 // None
21 // not divisible by 5, divisible by 7 // Kadabra
70 // divisible by 5, divisible by 7 // Abra Kadabra
"""

response = llm.invoke(prompt_input)
print(response.content)

A complete prompt and invocation show the instruction, new inputs, five labeled examples, and the model call together. section 2.5.5

For reuse, keep examples separate from the prompt template and inject them with LangChain’s FewShotPromptTemplate instead of hardcoding them into one string. More examples can improve consistency, but they also increase token costs. Examples guide the desired response pattern; they do not guarantee a correct answer. section 2.5.5 section 2.7 section 2.5.2

The same technique fits application prompts: three examples help select a research assistant and produce JSON, while schema and sample records can help a model generate SQL with fewer invented table or column names. When answers need user-specific or enterprise facts, prompt examples alone are not a substitute for retrieving that information. section 4.5.1 section 10.3 section 1.4.1

How AI Agents and Applications covers it

How deep it goes
Defines the technique, works through classification and an unsuccessful two-shot attempt, then shows LangChain implementation; does not explain model internals.
Examples
Uses divisibility-based number labels, a failed palindrome sum, research-assistant selection, and SQL prompts containing schema and sample records.
What it assumes
Assumes a configured LLM connection for the Python implementation, while demonstrating the classification task from a prompt with no rules.
What you can do after
Apply it via LangChain’s FewShotPromptTemplate to keep examples separate from a reusable prompt, then test whether outputs follow the intended pattern.
How it connects
Links in-context learning to prompt engineering, prompt templates, chain-of-thought prompting, retrieval-augmented generation, and fine-tuning.

Also explained in

Compare how 18 books explain few-shot learning →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of AI Agents and Applications. The section links point to the parts of the book it draws on.