Getting started with the Claude API in Python is refreshingly simple. By installing the anthropic package and configuring your API key, you can be querying the model in under a minute:

Claude, developed by Anthropic, offers easy access to a powerful large language model through a user-friendly REST API along with an official Python SDK. This streamlined approach contrasts with heavier frameworks that often require more setup before any output can be seen, making the anthropic package ideal for quick applications.
You'll follow a series of steps to install the anthropic SDK, invoke Claude from Python, customize responses with a system prompt, and produce structured JSON outputs via a schema or Pydantic model.
Tip: Keep in mind that Claude generates non-deterministic responses, meaning each input prompt can yield diverse outputs. Additionally, be aware that API usage incurs costs based on the number of tokens processed, so monitor your usage in the Claude Console as you develop.
This step-by-step guide not only aims for a thorough understanding but also keeps scripts concise enough for comfortable reading, while still being applicable for building your own practical applications.
Get Started: Download the sample code to learn how to implement the Claude API in Python!
Take the Quiz: Assess your understanding with our interactive quiz on using the Claude API in Python. Score yourself upon completion:
Interactive Quiz
Using the Claude API in PythonTest your proficiency in sending prompts, setting system instructions, and returning structured JSON using the API.
Prerequisites
Before diving in, ensure you have the following ready:
-
Basics of Python: Familiarity with Python operations, such as defining functions, executing scripts from the terminal, and understanding virtual environments is essential. For newcomers to virtual environments, check out this primer.
-
Python 3.9 or above: To utilize the
anthropicSDK, Python 3.9 is required. Verify your version by runningpython --versionin the terminal. If an update is needed, refer to the installation guide. -
Anthropic account: Account creation at Anthropic is essential to generate your API key, which you'll set up in Step 1 of this guide.
If API usage is new to you, worry not; this tutorial guides you through authentication and the initial API request process step-by-step.
Step 1: Establish the Claude API in Python
Your first task is obtaining an API key and installing the anthropic package to enable Claude interactions from your Python environment.
Obtain Your API Key and Set Up anthropic
Start by logging into the Claude Console or create a new account. A minimum of $5 in credits is required to start using the API.
Go to the API Keys section, click Create Key, assign a meaningful name such as real-python-tutorial, and be sure to copy it before closing the dialog, as it won't be shown again.
Important: Avoid embedding your API key directly in the code. Instead, store it as an environment variable, which the anthropic SDK will access as ANTHROPIC_API_KEY during execution, keeping your credentials secure.
This practice ensures your key remains out of source code and version control logs. The command you'll use varies by OS:
Next, you'll install the SDK in a separate virtual environment, which is vital for avoiding conflicts with system-level packages.