Few-shot prompting and conversation roles

Introduction to Claude Models

Nikhil Rangarajan

Data Scientist

Conversation roles with message format

  • Messages array simulates multi-turn conversations
  • Each message has a role
  • Follows specific response styles
messages = [
    {"role": "user", 
 "content": "What's the weather like?"},
    {"role": "assistant", 
 "content": "I don't have access to 
     current weather data. You might 
     want to check a weather app."},
    {"role": "user", 
 "content": "How's traffic downtown?"}]

response = client.messages.create(
    model="claude-sonnet-4-20250514",
    messages=messages)
Introduction to Claude Models

Introduction to few shot prompting

  • Few-shot prompting teaches Claude through examples
  • User/assistant message pairs demonstrate desired behavior
  • Effective on new input

A user and an assistant

Introduction to Claude Models

Understanding zero-shot vs few-shot vs one-shot

$$

  • Zero-shot: Single prompt with no examples
  • One-shot: Single example provided before the task
  • Few-shot: Multiple examples showing the pattern
  • More examples generally improve consistency
Introduction to Claude Models

Few-shot code example

messages = [{"role": "user", "content": "Translate to French: Hello"}] # Zero-shot

# One-shot messages = [{"role": "user", "content": "Translate to French: Good morning"}, {"role": "assistant", "content": "Bonjour"}, {"role": "user", "content": "Translate to French: Hello"}]
# Few-shot (multiple examples) messages = [{"role": "user", "content": "Translate to French: Good morning"}, {"role": "assistant", "content": "Bonjour"}, {"role": "user", "content": "Translate to French: Thank you"}, {"role": "assistant", "content": "Merci"}, {"role": "user", "content": "Translate to French: Hello"}]
Introduction to Claude Models

Practical few shot applications

  • Format consistency: Show desired output structure
  • Tone matching: Demonstrate specific communication style
  • Complex reasoning: Guide multi-step problem solving
  • Domain expertise: Teach specialized knowledge application
# Teaching email response format
messages = [{"role": "user", "content": "Customer complaint: Late delivery"},
    {"role": "assistant", "content": "Dear Customer,\n\nWe apologize for the delay. 
     We'll track your order and update you within 24 hours.
     \n\nBest regards,\nSupport Team"},
    {"role": "user", "content": "Customer complaint: Wrong item received"}]
Introduction to Claude Models

Best practices for few-shot prompting

Prompting

  • Use 2-5 examples for most tasks
  • Ensure examples are high-quality and consistent
  • Vary examples to show pattern, not just repetition
  • Test with edge cases to verify robustness
Introduction to Claude Models

Let's practice!

Introduction to Claude Models

Preparing Video For Download...