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

Anthropic has unveiled a new line of Claude 3 models, comprising the Opus (largest), Sonnet (mid-size), and Haiku (smallest) models. Additionally, the company has promptly released the API for Claude 3 models.

Despite the relatively high pricing of Claude 3 API, especially for the Opus model, compared to GPT-4 Turbo, users and developers are eager to explore the capabilities of these models. To assist you in accessing the Claude 3 API for Opus and Sonnet models, we have prepared a straightforward tutorial. Furthermore, we have included some code examples to help you get started with testing the models.

Note: Anthropic is currently providing $5 worth of free credits for the Claude 3 API. Before purchasing the API, you can claim your free credits and immediately test the Opus and Sonnet models.

Accessing Claude 3 API Key For Free

1. Visit console.anthropic.com(visit) and register for an account.

2. Once logged in, you’ll see a banner offering $5 in free credits. Click on “Claim” to avail the offer.

3. Enter your mobile number and verify it to proceed.

4. On the dashboard, click on “Get API Keys” or visit console.anthropic.com/settings/keys (visit) to get Claude 3 API keys.

5. Click on “Create Key” and name it.

6. Copy the API key and ensure it is stored securely.

Guide to Use Claude 3 API With Examples

  • Begin by setting up Python and Pip on your computer.
  • Open the Terminal and use the following command to install the Claude library.
pip install anthropic

  • Anthropic’s documentation for Claude 3 API testing includes some excellent examples. Copy the code below 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 configured to use the largest Claude 3 Opus model (claude-3-opus-20240229). Replace my_api_key with your actual API key. If you prefer to use the Sonnet model, use the model name: claude-3-sonnet-20240229.

  • Save the file as claude3.py on your Desktop or preferred location. Ensure the file extension is .py.
  • Open Terminal, navigate to the Desktop, and run the claude3.py file. It will respond to the question set in the code, mimicking Yoda’s speech for the question, “How are you today?” You can modify the system prompt in the code to change its behavior.
cd Desktop
python claude3.py

  • Additionally, you can easily try out Simon Willison’s newly released plugin for Claude 3 models.
https://twitter.com/simonw/status/1764679795865165956?ref_src=twsrc%5Etfw%7Ctwcamp%5Etweetembed%7Ctwterm%5E1764679795865165956%7Ctwgr%5E1c99faaf2476177d1df682be662809150dd5e43a%7Ctwcon%5Es1_&ref_url=https%3A%2F%2Fbeebom.com%2Fhow-access-claude-3-api-opus-sonnet-models-examples%2F

This is how you can quickly access the Claude 3 API and test both Opus and Sonnet models. Currently, Anthropic does not offer an API for the smallest Haiku model. If there are any changes in the future, we will keep you informed and update accordingly. If you need to access the Gemini API key, refer to our guide. If you have any questions, please let us know in the comments below.

Share this article
Shareable URL
Leave a Reply

Your email address will not be published. Required fields are marked *

Read next
0
Share