A Note before we start

This course is a demonstration of how we can adapt the content in https://www.anthropic.com/learn to the CodeSignal platform. CodeSignal brings a lot of value including:

  • A course structure for full instructional design
  • Text or Video based lessons
  • Hands-on playgrounds for all supported languages
  • A personal tutor named Cosmo who aids you in your learning journey
  • A full library for learners to understand pre-requiesites like Python, JavaScript, Java, Go, Working with APIs, etc
Introduction: Meet the *Anthropic API* and *Claude*

Welcome to your first lesson in the "Anthropic Academy -- Build with Claude" course! In this lesson, you will learn how to connect to the Anthropic API and send your very first message to Claude using Python.

The Anthropic API allows you to interact with Claude, an advanced AI assistant. With this API, you can send messages to Claude and receive helpful, conversational responses. This is the foundation for building all sorts of applications, from chatbots to productivity tools.

By the end of this lesson, you will know how to set up your Python environment, connect to the Anthropic API, and send a simple message to Claude. This is the first step in your journey to building with Claude.

Basic Concepts: Python and API Keys

Before we dive in, let’s quickly review two important concepts:

  • Importing Libraries in Python:
    In Python, you use the import statement to bring in external libraries. For example, if you want to use the math library, you would write:

    This allows you to use functions and classes from that library in your code.

  • What is an API Key?
    An API key is a special code that lets you access a service, like the Anthropic API. It’s like a password for your program. You need to keep it secret and never share it publicly.
    On CodeSignal, the Anthropic API key is already set up for you, so you don’t need to worry about finding or storing it for now. On your own computer, you would usually set it as an environment variable or pass it in as a parameter. You can get your own Anthropic API key from your Anthropic account settings.

Setting Up: Installing and Importing the Anthropic Library

To use the Anthropic API in Python, you need the anthropic library. This library makes it easy to connect to Claude and send messages.

  • Installing the Library:
    On your own computer, you would install the library using pip:

    On CodeSignal, this library is already installed, so you can skip this step here.

  • Importing the Library:
    To use the library in your code, you need to import it:

    This line tells Python to load the anthropic library so you can use its features.

Connecting to Anthropic: Creating a Client

To talk to Claude, you first need to create a client object. This client handles the connection to the Anthropic API.

Here’s how you create a client:

  • The Anthropic() class creates a client object.
  • By default, it looks for your API key in an environment variable called ANTHROPIC_API_KEY.
  • On CodeSignal, this is already set up for you, so you don’t need to pass the key directly.

Now, your client is ready to send messages to Claude.

Sending Your First Message to Claude

Now, let’s send a message to Claude and get a response. This is the main part of the lesson.

Step 1: Prepare the Message

To send a message, you use the client.messages.create() method. You need to provide a few details:

  • model: The name of the Claude model you want to use. For this lesson, we’ll use "claude-sonnet-4-20250514".
  • max_tokens: The maximum number of tokens (words and parts of words) you want in the response. We’ll use 1024 for now.
  • messages: A list of messages in the conversation. Each message is a dictionary with a role and content. For your first message, you’ll send a message as the user.

Here’s how you put it all together:

  • model tells Claude which version to use.
  • max_tokens limits the length of the response.
  • messages is a list. Each item is a message. Here, you send one message from the user: "Hello, Claude."
Step 2: Print the Response

After sending the message, you want to see Claude’s reply. The response is stored in the message object. To print the reply, use:

This will display Claude’s response in your output.

Full Example

Here is the complete code for sending your first message to Claude:

Expected Output:
When you run this code, you will see a friendly response from Claude, such as:

The exact wording may vary, but you should see a helpful reply from Claude.

Accepted Model Strings

As of this writing, the Anthropic API supports multiple different models:

Review and What’s Next

In this lesson, you learned how to:

  • Import the anthropic library in Python
  • Create a client to connect to the Anthropic API
  • Send a simple message to Claude and print the response

You now have the basic skills to interact with Claude using Python. In the next exercises, you will get hands-on practice with these steps. You’ll try sending different messages and see how Claude responds. This will help you get comfortable with the API before moving on to more advanced features.

Great job getting started with Claude and the Anthropic API!

Sign up
Join the 1M+ learners on CodeSignal
Be a part of our community of 1M+ users who develop and demonstrate their skills on CodeSignal