Anthropic Claude 3: The Basics and a Quick API Tutorial

What Is Anthropic Claude 3?

Claude 3 is the latest generation of large language models (LLMs) developed by Anthropic, first released on March 14, 2024. This model family consists of three editions: Claude 3 Haiku, Claude 3 Sonnet, and Claude 3 Opus. Each edition offers different levels of intelligence, speed, and cost-effectiveness. These models are designed to perform tasks such as analysis, forecasting, content creation, code generation, and multilingual conversations.

According to benchmarks shared by Anthropic, Claude 3 Opus beats its main competitor, OpenAI GPT-4, on several important metrics, including MMLU, GPQA, GSM8K, MGSM, and HumanEval (0-shot).

Claude 3 models are integrated into the Claude API, accessible in 159 countries, and also as part of the Amazon Bedrock service.

Users can chat with Claude 3 for free here.

This is part of a series of articles about Anthropic Claude

Key Features of Claude 3

Claude 3 models provide the following key capabilities:

  • Long context with effective recall: Claude has a context window of 200,000 tokens, and Anthropic extends it to over 1 million tokens for selected customers. Together with its long context, Claude 3 Opus achieves over 99% recall accuracy.
  • Low latency: Suitable for live customer interactions, auto-completions, and data extraction tasks. The Haiku model is the fastest and can process long inputs in under three seconds.
  • Vision capabilities: Can interpret various visual formats, including photos, charts, graphs, and technical diagrams. For example, this makes it possible to process large amounts of information in visual formats like PDFs, flowcharts, and presentation slides.
  • Improved response accuracy: According to Anthropic, Claude 3 Opus has a twofold improvement in accuracy over previous models on challenging, open-ended questions. The model reduces incorrect responses and is more likely to admit uncertainty.
  • Fewer refusals: Previous models often declined to respond to queries that were close to their operational boundaries. Claude 3 models, including Opus, Sonnet, and Haiku, now demonstrate a more nuanced understanding, decreasing the frequency of inappropriate refusals.
  • Responsible design: Incorporates multiple safeguards to mitigate risks such as misinformation, privacy issues, and biases. Anthropic has employed techniques like Constitutional AI to enhance safety and transparency.

Claude 3 Model Editions

Here’s an overview of the Claude 3 model editions:

Haiku

Claude 3 Haiku is the fastest and most compact model in the Claude 3 family. Designed for near-instant responsiveness, it is optimized for tasks requiring quick turnarounds and seamless AI experiences that mimic human interactions. Haiku can process dense information, such as research papers with charts and graphs, in less than three seconds.

This model is particularly suited for applications that prioritize speed and cost-effectiveness, including real-time data extraction, auto-completions, and rapid customer interactions. Haiku's compact design ensures performance without compromising on the quality of responses.

Sonnet

Claude 3 Sonnet strikes a balance between intelligence and speed. It is designed to handle enterprise workloads and scaled AI deployments efficiently. Sonnet offers twice the speed of its predecessors, Claude 2 and 2.1, making it suitable for rapid-response tasks such as knowledge retrieval and sales automation.

Its enhanced intelligence allows it to handle open-ended conversations, collaborate on ideas, and perform coding tasks. Sonnet can meet the needs of enterprises, providing reliable and quick responses across a range of applications.

Opus

Claude 3 Opus is the most powerful model in the Claude 3 family, delivering high performance on complex tasks. It demonstrates top-level intelligence, fluency, and human-like understanding. Opus can handle challenging scenarios requiring deep understanding and precision, such as complex data analysis, advanced forecasting, and sophisticated content creation.

Opus provides improved intelligence while matching the speed of earlier models, making it suitable for tasks that demand the highest level of AI capability. Its accuracy and advanced features meet the needs of demanding and sensitive applications.

Claude 3 API Pricing

Claude 3 models are priced according to their capabilities and intended use cases, with costs calculated per million tokens (MTok).

  • Haiku: Priced at $0.25 per MTok for input and $1.25 per MTok for output. This model is suitable for applications requiring quick, efficient processing.
  • Sonnet: Priced at $3 per MTok for input and $15 per MTok for output. It is suitable for enterprise-level tasks that demand both rapid response times and enhanced AI capabilities.
  • Opus: Priced at $15 per MTok for input and $75 per MTok for output. It is designed for complex tasks that require the highest level of AI performance.

Getting Started with Claude 3

Before you begin, make sure you have the following:

  • An Anthropic Console account with Claude API access
  • A valid API key (available via your Account Settings)
  • Python (3.7.1 or newer)

Setting Up the Environment

Start by opening the command prompt or terminal (for Windows and macOS, respectively). Enter the following to check that Python is installed on your computer:

python --version

You should see a compatible version number, such as Python 3.12.2. If the version is older than 3.7.1, download a later version from the Python website.

Now you can build a virtual environment for the Claude 3 projects. It is possible to use Claude without this environment, but having a dedicated environment is recommended to keep the dependencies in order and avoid confusion with other projects. Use the following to create the environment:

python -m venv claude-env

Next, activate the environment by running

claude-env\Scripts\activate
on Windows or
source claude-env/bin/activate
on macOS or Linux.

Installing the Claude Python Library

To install the Anthropic Python SDK, run the following command:

pip install anthropic

This will install the latest version of the Claude Python library alongside the required dependencies.

Setting Up the API Key

You need an API key to use Claude. To make the API key available for Claude projects, set up an environment variable.

On Windows:

Using the command prompt, enter the following:

setx ANTHROPIC_API_KEY "my-api-key"

On macOS or Linux:

Using the terminal, enter the following:

nano ~/.bash_profile

Alternatively, use

nano ~/.zshrc
for newer versions of macOS.

Next, add the following line:

export ANTHROPIC_API_KEY='my-api-key'

After saving the file and exiting the editor, you can load your updated profile using the following command:

source ~/.bash_profile

You can make the change permanent with the following steps:

  1. Right-click on My Computer or This PC and choose Properties.
  2. Select Advanced system settings and then click on Environment Variables.
  3. Under System variables, select New… and specify the variable name (
    ANTHROPIC_API_KEY
    ) and value (your API key).

Sending an API Request {#sending-an-api-request}

You should now be able to send API requests to the Claude API. Start by creating a Python file such as

claude_test.py
and adding this script:

import anthropic client = anthropic.Anthropic( api_key="my-api-key", ) message = client.messages.create( model="claude-3-opus-20240229", max_tokens=1000, temperature=0.0, system="Respond in pirate style.", messages=[ {"role": "user", "content": "Hello!"} ] ) print(message.content)

This script imports the Anthropic library to enable interaction with Claude. It creates an Anthropic client instance and uses your API key to make an API call using the messages.create() method. The script specifies the Claude model to be used, the response’s token limit (a maximum of 1000 tokens), the temperature, and a system message that applies a context for your conversation.

To run this script, enter

python claude_test.py
in the command prompt or terminal. Claude will return a greeting in pirate style.

Building LLM Applications with Claude and Acorn

To download GPTScript visit https://gptscript.ai. As we expand on the capabilities with GPTScript, we are also expanding our list of tools. With these tools, you can build any application imaginable: check out tools.gptscript.ai and start building today.