Making your first request
1. Making your first request
Hello! Welcome to this video on making our first request to Claude.2. What is the Anthropic library?
To start interacting with Claude from our code, we'll use the official Anthropic Python SDK. This library bridges our applications and Claude's AI capabilities: it makes it easy to handle formatting and authentication to return faster responses. To get started, we install it with pip and then import it into our Python scripts.3. Authentication and setup
Once the library is installed, we'll create a client using our API key. We won't need a key for the exercises in this course, but you can use this approach when working on your own projects. The client acts as our gateway to send messages to Claude.4. Sending our first prompt
Now, let’s send our first prompt. We use messages.create, specifying three essential parameters: the model, the maximum number of tokens, and the list of messages. But what are tokens? A token is the smallest unit of text, which can be a word or part of a word, that the model processes. For example, "DataCamp" might be split into two tokens: "Data" and "Camp." So the more text is generated, the more tokens the model uses. We want to limit this parameter to avoid unnecessary costs, and we use max_tokens to do that. The third parameter is messages, a list of turns in the conversation. We start with a message containing user content. Claude processes this prompt and returns the response.5. Extracting the model output
After sending our prompt, we need to extract Claude's actual response from the structured API return. The response object includes metadata like token usage and model type, but the assistant's reply lives inside the content field. This field is a list of message parts, and in most cases, there's just one item: a dictionary with a text key. To get the actual reply, we use response.content[0].text. This line gives us the clean, readable output Claude generated. We'll follow this same pattern, accessing content[0].text, in all our interactions with Claude to retrieve its response.6. Putting it all together
Now let's put everything together with a complete example. In this code, we're seeing the full process from start to finish. We begin by importing the anthropic library and creating our client. Then we make our API call, asking Claude to summarize its own benefits in just one sentence. The request includes our model selection, a token limit, and our formatted message. After Claude processes this request, we extract its response using content[0].text and print it out. This simple pattern, of7. Putting it all together
importing,8. Putting it all together
initializing,9. Putting it all together
requesting,10. Putting it all together
and extracting, forms the foundation for all our interactions with Claude through the API. With just these few lines of code, we've successfully made a complete request to Claude and retrieved its response.11. Let's practice!
Let's practice with some exercises!Create Your Free Account
or
By continuing, you accept our Terms of Use, our Privacy Policy and that your data is stored in the USA.