Claude With The Anthropic Api
← All lessons
Lesson 08Claude With The Anthropic Api

System prompts

Summary audio

No audio recap for this lesson.

Study notes

What is System Prompting?

  • System prompts customize the style and tone of Claude's responses
  • They are plain strings passed to the API's create function
  • They allow you to steer Claude's behavior in a particular direction

Key Use Case: Math Tutor Example

  • Without system prompt: Claude gives direct, complete answers
  • With system prompt: Claude guides students with hints and step-by-step guidance
  • System prompts help create more interactive and educational experiences

How to Implement System Prompts

  • Define a system prompt as a multi-line string
  • Assign Claude a role in the first line (e. g. , "You are a patient math tutor")
  • Pass the system prompt to the API using the system keyword argument in the create function call

Handling Optional System Prompts

  • Add a system parameter to your chat function with a default value of None
  • Create a params dictionary to hold API parameters
  • Only add the system key to the params dictionary if a system prompt was actually provided
  • Use **params syntax to unpack the dictionary when calling the create function
  • This prevents errors from passing system=None to the API

Benefits

  • Encourages desired behavior (e. g. , patient guidance instead of direct answers)
  • Makes chat functions reusable across different scenarios
  • Provides more control over response style without changing the user's input

Takeaways

  • System prompts customize Claude's style and tone by assigning it a role and behavior guidelines
  • System prompts enable interactive experiences like step-by-step tutoring instead of direct answers
  • Pass system prompts to the API using the system keyword argument in the create function
  • Handle optional system prompts by only adding the system key to the params dictionary if one is provided
  • System prompts make chat functions reusable across different scenarios without modifying user input
Flashcards 7 cards
Question
click to reveal · ←/→
Answer
click to flip back
Export to Anki (.tsv) ↓
Knowledge check 1 questions