Making your first request

Introduction to Claude Models

Nikhil Rangarajan

Data Scientist

What is the Anthropic library?

  • Official Python SDK for accessing Claude

  • Helps structure Claude interactions

  • Installing via pip:

    pip install anthropic
    
  • Importing the library:

    import anthropic
    

Diagram starting from a prompt, going to the Anthropic API, to the Claude model

Introduction to Claude Models

Authentication and setup

$$

import anthropic

client = anthropic.Anthropic( api_key=os.getenv("ANTHROPIC_API_KEY") )

$$

🔑 Key not needed in course exercises

Introduction to Claude Models

Sending our first prompt

response = client.messages.create(

model="claude-sonnet-4-20250514",
max_tokens=100,
messages=[{"role": "user", "content": "What DataCamp course do you recommend to learn Claude?"}]
)
  • model: Claude Sonnet
  • max_tokens: Limits output size (about 4 characters per token in English.)
    • Token: smallest unit of text that the model processes
  • messages: A list of interactions starting with the user's input
Introduction to Claude Models

Extracting the model output

  • response - Entire structured API response
  • response.content - Returns list of message parts
    [{'type': 'text', 
    'text': 'Claude is...'}]
    
  • response.content[0].text - Assistant's reply
    "Claude is..."
    
Introduction to Claude Models

Putting it all together

import anthropic


client = anthropic.Anthropic()
response = client.messages.create( model="claude-sonnet-4-20250514", max_tokens=100, messages=[{"role": "user", "content": "Summarize the benefits of Claude in one sentence."}] )
print(response.content[0].text)
Introduction to Claude Models

Putting it all together

import anthropic #Importing

client = anthropic.Anthropic() 

response = client.messages.create( 
    model="claude-sonnet-4-20250514",
    max_tokens=100,
    messages=[{"role": "user", 
               "content": "Summarize the benefits of Claude in one sentence."}]
)

print(response.content[0].text) 
Introduction to Claude Models

Putting it all together

import anthropic #Importing

client = anthropic.Anthropic() #Initializing

response = client.messages.create( 
    model="claude-sonnet-4-20250514",
    max_tokens=100,
    messages=[{"role": "user", 
               "content": "Summarize the benefits of Claude in one sentence."}]
)

print(response.content[0].text) 
Introduction to Claude Models

Putting it all together

import anthropic #Importing

client = anthropic.Anthropic() #Initializing

response = client.messages.create( #Requesting
    model="claude-sonnet-4-20250514",
    max_tokens=100,
    messages=[{"role": "user", 
               "content": "Summarize the benefits of Claude in one sentence."}]
)

print(response.content[0].text) 
Introduction to Claude Models

Putting it all together

import anthropic #Importing

client = anthropic.Anthropic() #Initializing

response = client.messages.create( #Requesting
    model="claude-sonnet-4-20250514",
    max_tokens=100,
    messages=[{"role": "user", 
               "content": "Summarize the benefits of Claude in one sentence."}]
)

print(response.content[0].text) #Extracting
Introduction to Claude Models

Let's practice!

Introduction to Claude Models

Preparing Video For Download...