Loading video player…

Setting Up Claude API

Resources mentioned in this lesson:

00:00 The first thing that you need to do is to set up your Claude API. To do that, you need to follow the following steps. First, you need to log in to the Claude Console or create a new account.

00:11 The URL for that is platform.claude.com. Then you need to top off your account. Then you need to create a fresh virtual environment and activate it. Get your API key and store it as an environment variable.

00:24 You need to install anthropic. And after that, you’re ready to send your first prompt. The first five steps are explained in detail in the tutorial this video course is based on.

00:34 It’s called How to Use the Claude API in Python, so you can check it out and follow the steps. Once you’re ready, you can head over to your code editor.

00:43 Throughout this course, your goal is to create a Python coding assistant, and this is your starting point. So the question that you will be asking is, what is the Zen of Python?

00:54 Here you have a file called basic_claude_call.py. You can start off by importing anthropic.

01:01 Then you need to create an Anthropic client. This object handles authentication and lets you send requests to the API.

01:09 client = anthropic.Anthropic, open close parentheses. Make sure you capitalize the second Anthropic. This uses the API key that you stored earlier as an environment variable, so you don’t need to copy-paste your API, and you actually shouldn’t, because of security reasons.

01:28 Now you need to call the messages API to send a request to Claude.

01:33 response = client, so the same client that you created earlier, .messages.create. Then you open a parentheses inside of that.

01:46 First, you need to specify the model that you’re trying to communicate with. I’m using Claude Sonnet 4.6. You can use whichever model that you prefer. model="claude-sonnet-4-6" comma. Now you need to set the maximum number of tokens you want Claude to generate in its reply. Here I’m using max_tokens=1024.

02:16 Make sure you actually do set this limit, because each token does use up your credits. Now you need a messages list. This messages list stores the actual conversation.

02:27 Each message has a role that identifies who sends it, and a content field that contains the message itself. messages=[ and you need to open curly brackets.

02:44 Here the role is the user who’s sending the message. So "role": "user" comma, and then you have the content that it’s sending.

02:58 "content": "What is the Zen of Python?" You can use other content as well as you please. So "role": "user" tells Claude that this message came from the user, and the content is the text of the user’s question.

03:16 Together they represent a single message in the conversation. Later on, you’ll also use the role "assistant" to let Claude understand the conversation’s context and generate an appropriate response. For now, "user" is enough.

03:30 Now make sure you close your curly brackets, your bracket and parentheses, and then extract the text from the first content block in Claude’s response and then print it to the terminal. So print(response.content, the first index, which is zero, .text). Okay, make sure you save your file and head over to terminal.

03:53 python basic_claude_call.py. Okay, you got an answer. Prioritize readability and simplicity. Be explicit rather than clever, which is the Zen of Python.

04:14 Now your goal is to create a Python coding assistant. This somewhat worked, but there’s a problem. If in the content you write a non-Python question like "content": "How do I reverse a string in JavaScript?" Let’s run it again and see what happens.

04:39 You still do get an answer.

04:43 Let’s look at this up close. It does explain how to reverse a string in JavaScript. But you don’t want this. You want your coding assistant to only answer Pythonic questions.

04:54 How can you control Claude’s behavior? The answer is in the next lesson.

Become a Member to join the conversation.