In production systems, hardcoding a fixed set of few-shot examples can waste tokens and fail to represent the specific context of the user's input.
Dynamic Few-Shot Selection solves this by matching the user's input against an indexed vector database of examples at runtime.
By running a similarity search (e.g. cosine similarity over embeddings) on the query, the application retrieves the top-k most similar pairs of inputs and outputs, and injects them dynamically into the prompt before calling the LLM.
import chromadb from openai import OpenAI
client = chromadb.Client() collection = client.get_collection("support_examples") results = collection.query(query_texts=[user_ticket], n_results=3)
prompt = "Classify the incoming support ticket.\n\n" for doc, metadata in zip(results['documents'][0], results['metadatas'][0]): prompt += f"Ticket: {doc}\nCategory: {metadata['category']}\n---\n"
prompt += f"Ticket: {user_ticket}\nCategory:"
Claude's long context handles up to 20+ dynamic examples, making it suitable for complex pattern-matching.