How to Access Claude 3 API for Opus and Sonnet Models (With Examples)

In Short
  • Anthropic is offering $5 worth of free API access to users and developers.
  • You can start using the API for Opus and Sonnet models. However, API access for the smallest Haiku model is not available yet.
  • You can also try some code examples and test the models on your prompt.

Anthropic introduced a new family of Claude 3 models which include Opus (largest), Sonnet (mid-size), and Haiku (smallest). The company has also made the API for Claude 3 models available immediately.

While Claude 3 API pricing, especially for the Opus model, is quite expensive in comparison to GPT-4 Turbo, users and developers want to check out the model’s capabilities. So here is a simple tutorial on how to access Claude 3 API for Opus and Sonnet models. We have also added a few code examples for you to start testing the models.

Note: Anthropic is currently offering Claude 3 API worth $5 in free credits. Before you buy the API, you can claim free credits and test Opus and Sonnet models right away.

Access Claude 3 API Key For Free

  • Head to console.anthropic.com (visit) and create an account.
  • Next, you will see a banner on top informing users about Claude offering $5 in free credits. Click on “Claim“.
claim free credit by Anthropic
  • Enter your mobile number and verify it. You are almost done.
  • Now, click on “Get API Keys” on the dashboard. You can also head to console.anthropic.com/settings/keys (visit) to get Claude 3 API keys.
  • Here, click on “Create Key” and give it a name.
  • Now, copy the API key and store it securely.

Tutorial to Use Claude 3 API With Examples

  • Go ahead and set up Python along with Pip on your computer.
  • After that, open Terminal and run the below command to install the Claude library.
pip install anthropic
  • Anthropic has added some great examples in its documentation for Claude 3 API testing. You can copy the below code and paste it into a code editor like Notepad++.
import anthropic

client = anthropic.Anthropic(
    # defaults to os.environ.get("ANTHROPIC_API_KEY")
    api_key="my_api_key",
)

message = client.messages.create(
    model="claude-3-opus-20240229",
    max_tokens=1000,
    temperature=0.0,
    system="Respond only in Yoda-speak.",
    messages=[
        {"role": "user", "content": "How are you today?"}
    ]
)

print(message.content)
  • The code is already using the largest Claude 3 Opus model (claude-3-opus-20240229). You just need to replace my_api_key with the actual API key you copied above. If you want to use the Sonnet model, use this model name: claude-3-sonnet-20240229.
  • Now, save it as claude3.py on your Desktop or your preferred location. You can also name it differently, but make sure .py is appended.
  • Finally, fire up Terminal, and move to the Desktop. Next, run the claude3.py file. It will answer the question set in the code. Here, it replies like Yoda to the question, “How are you today?” You can also change the system prompt in the code to change its behavior.
cd Desktop
python claude3.py
  • Apart from that, you can try out Simon Willison’s newly released plugin for Claude 3 models with ease.

So that is how you can quickly access the Claude 3 API and test both Opus and Sonnet models. Anthropic is currently not offering API for the smallest Haiku model. If that changes in the future, we will let you know. Anyway, that is all from us. If you are looking to access the Gemini API key, follow our linked guide. And if you have any questions, let us know in the comment section below.

#Tags
Comments 0
Leave a Reply

Loading comments...