Anthropic launches Claude Fable 5.1 and says it’s up to 45 percent cheaper for agentic work
Back to Tutorials
aiTutorialbeginner

Anthropic launches Claude Fable 5.1 and says it’s up to 45 percent cheaper for agentic work

September 1, 20267 views5 min read

Learn how to set up and use Claude AI models through the Anthropic API, with a focus on cost optimization for agentic work. This beginner-friendly tutorial teaches you to make API calls, choose models, and understand pricing differences.

Introduction

In this tutorial, you'll learn how to work with Claude AI models using the Anthropic API. We'll focus on using Claude Fable 5.1, which Anthropic claims is up to 45% cheaper for complex tasks. This beginner-friendly guide will walk you through setting up your environment, making API calls, and understanding how to optimize costs when working with AI models.

By the end of this tutorial, you'll have a working Python script that communicates with Claude's API, and you'll understand how to choose the right model for your specific needs while being mindful of pricing.

Prerequisites

Before starting this tutorial, you'll need:

  • A basic understanding of Python programming
  • An Anthropic API key (you can get one from Anthropic's website)
  • Python 3.7 or higher installed on your computer
  • Access to a terminal or command prompt

Step-by-Step Instructions

1. Install Required Python Packages

First, we need to install the Anthropic Python library that will help us communicate with Claude's API. Open your terminal and run:

pip install anthropic

Why this step? The Anthropic library provides a convenient way to interact with Claude's API without having to manually construct HTTP requests. It handles authentication, request formatting, and response parsing for us.

2. Set Up Your API Key

Before you can make any API calls, you need to store your API key. Create a new file called config.py in your project directory:

ANTHROPIC_API_KEY = 'your-api-key-here'

Replace 'your-api-key-here' with your actual API key from Anthropic's console. Keep this file secure and never commit it to version control.

Why this step? Storing your API key in a separate file keeps it out of your main code and prevents accidental exposure in public repositories.

3. Create Your Main Python Script

Create a new file called claude_demo.py and start by importing the necessary modules:

import os
from anthropic import Anthropic
from config import ANTHROPIC_API_KEY

Why this step? We're importing the Anthropic client library and our API key configuration. This sets up the foundation for making API calls.

4. Initialize the Anthropic Client

Add the following code to initialize your client:

client = Anthropic(api_key=ANTHROPIC_API_KEY)

Why this step? This creates a client object that we'll use to make all our API calls to Claude. The client handles authentication automatically.

5. Create a Simple Prompt Function

Now let's create a function that sends a simple prompt to Claude:

def ask_claude(prompt):
    response = client.messages.create(
        model="claude-3-5-sonnet-20240620",  # Using Claude 3.5 Sonnet
        max_tokens=1000,
        messages=[
            {
                "role": "user",
                "content": prompt
            }
        ]
    )
    return response.content[0].text

Why this step? This function demonstrates how to structure a basic API call to Claude. We're using Claude 3.5 Sonnet, which is one of the most capable models available.

6. Test Your Setup

Add this code to test your setup:

if __name__ == "__main__":
    prompt = "Explain what Claude AI is in simple terms."
    response = ask_claude(prompt)
    print("Claude's response:")
    print(response)

Why this step? This runs a quick test to make sure everything is working properly. You should see Claude's response to your question.

7. Understanding Model Selection for Cost Optimization

Anthropic mentions that Claude Fable 5.1 is up to 45% cheaper for agentic work. Let's modify our script to show how to select different models:

def ask_claude_with_model(prompt, model="claude-3-5-sonnet-20240620"):
    response = client.messages.create(
        model=model,
        max_tokens=1000,
        messages=[
            {
                "role": "user",
                "content": prompt
            }
        ]
    )
    return response.content[0].text

Why this step? Different Claude models have different pricing and capabilities. By allowing model selection, you can choose the most cost-effective option for your specific task.

8. Run Your Complete Script

Here's your complete working script:

import os
from anthropic import Anthropic
from config import ANTHROPIC_API_KEY

client = Anthropic(api_key=ANTHROPIC_API_KEY)

def ask_claude_with_model(prompt, model="claude-3-5-sonnet-20240620"):
    response = client.messages.create(
        model=model,
        max_tokens=1000,
        messages=[
            {
                "role": "user",
                "content": prompt
            }
        ]
    )
    return response.content[0].text

if __name__ == "__main__":
    # Test with different models
    prompt = "What are the key differences between Claude 3.5 Sonnet and Claude 3 Opus?"
    
    # Using Claude 3.5 Sonnet (more capable, more expensive)
    response = ask_claude_with_model(prompt, "claude-3-5-sonnet-20240620")
    print("Claude 3.5 Sonnet response:")
    print(response)
    
    print("\n" + "="*50 + "\n")
    
    # Using Claude 3 Haiku (faster, cheaper)
    response = ask_claude_with_model(prompt, "claude-3-haiku-20240307")
    print("Claude 3 Haiku response:")
    print(response)

Why this step? This demonstrates how you can choose different models based on your needs and budget considerations. Haiku is faster and cheaper, while Sonnet is more capable.

9. Run Your Script

Save your script and run it in the terminal:

python claude_demo.py

Why this step? Running the script will show you how Claude responds to the same prompt with different models, helping you understand the trade-offs between capability and cost.

10. Understanding Cost Savings

According to Anthropic, Claude Fable 5.1 costs 25% less typically and up to 45% less for complex agentic tasks. In your script, you can track costs by monitoring:

  • Number of tokens used in each response
  • Which model you're using (as different models have different pricing)

While the Anthropic library doesn't directly show costs, you can monitor token usage by looking at the response object:

response = client.messages.create(
    model="claude-3-5-sonnet-20240620",
    max_tokens=1000,
    messages=[
        {
            "role": "user",
            "content": prompt
        }
    ]
)
print(f"Input tokens: {response.usage.input_tokens}")
print(f"Output tokens: {response.usage.output_tokens}")

Why this step? Understanding token usage helps you optimize costs, as Claude charges based on the number of input and output tokens used in each API call.

Summary

In this tutorial, you've learned how to set up and use Claude AI models through the Anthropic API. You've created a Python script that can communicate with Claude, tested different models, and understood how to make cost-effective choices when working with AI.

Key takeaways:

  • Install the Anthropic Python library with pip install anthropic
  • Store your API key securely in a separate configuration file
  • Choose the right model based on your needs and budget (Claude 3 Haiku is cheaper, Claude 3.5 Sonnet is more capable)
  • Monitor token usage to optimize costs

Remember that Claude Fable 5.1 offers significant cost savings for complex agentic work, so consider using it when appropriate for your projects. This foundation will help you build more sophisticated AI applications while keeping costs in check.

Source: The Verge AI

Related Articles