Instead of hardcoding static few-shot examples, dynamic retrieval uses vector embeddings to calculate similarity between the incoming user query and a large database of labeled examples.
The prompt is then populated with the most semantically relevant examples, maximizing in-context learning relevance and saving context window space.
query_vector = get_embedding(user_query)
matches = vector_db.search(query_vector, limit=3) # returns matching few-shots
prompt = "Examples:\n" + compile_examples(matches) + f"\nQuery: {user_query}"
Claude benefits from highly relevant XML-tagged examples that align exactly with the input domain.