Files
model_garden/notebooks/official/generative_ai/anthropic_claude_intro.ipynb
T
Chun-Hsiang WangandGitHub 4b983a2701 feat: Claude Fable 5.1 Launch (#4581)
* feat: Claude Fable 5.1 Launch

* refactor: replace model/region if-elif chains with a dict lookup

Addresses review feedback on both Select Claude model cells. The mapping is
unchanged for all 20 models; only the lookup mechanism differs.

* chore: apply nbfmt

Runs the repo's own tensorflow-docs nbfmt over the notebook so the
'notebook format and lint' check passes.
2026-09-01 20:45:17 -04:00

38 KiB
Raw Blame History

In [ ]:
# Copyright 2026 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     https://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

Overview

Claude on Vertex AI

Anthropic Claude models on Vertex AI offer fully managed and serverless models are offered as managed APIs. To use a Claude model on Vertex AI, send a request directly to the Vertex AI API endpoint.

You can stream your Claude responses to reduce the end-user latency perception. A streamed response uses server-sent events (SSE) to incrementally stream the response.

Available Anthropic Claude models

Claude Fable 5.1

Claude Fable 5.1 delivers frontier intelligence for ambitious tasks across coding, scientific discovery, and enterprise workflows.

Claude Opus 5

Claude Opus 5 is Anthropic's most advanced Opus model, powering long-running agents while delivering improvements in coding and professional work.

Claude Sonnet 5

Claude Sonnet 5 is our most capable Sonnet model yet, built for coding, agents, and professional work at scale. It brings near-Opus intelligence to the model teams run at scale every day, with the same balance of capability, cost, and speed teams already rely on Sonnet for.

Claude Fable 5

Claude Fable 5 is our next generation of intelligence for the hardest knowledge work and coding problems. It works independently for longer than any prior generally available Claude model: run it in an agent harness and it can work for days at a time, planning across stages, delegating to sub-agents, and checking its own work.

Claude Opus 4.8

Claude Opus 4.8 is our most intelligent Opus model and the best generally available model for coding and agents, with deeper reasoning for enterprise workflows.

Claude Opus 4.7

Claude Opus 4.7 is our most capable production model yet, advancing performance across coding, enterprise workflows, and long-running agentic tasks.

Claude Sonnet 4.6

Claude Sonnet 4.6 delivers frontier intelligence at scale—built for coding, agents, and enterprise workflows.

Claude Opus 4.6

Claude Opus 4.6 is the next generation of Anthropic’s most intelligent model, and the world’s best model for coding, enterprise agents, and professional work.

Claude Opus 4.5

The next generation of Anthropic's most intelligent model, Claude Opus 4.5 is an industry leader across coding, agents, computer use, and enterprise workflows.

Claude Haiku 4.5

Anthropic's mid-size model with superior intelligence for high-volume uses in coding, in-depth research, agents, & more.

Claude Sonnet 4.5

Anthropic's most powerful model for powering real-world agents, with industry leading capabilities around coding, computer use, cybersecurity, and working with office files like spreadsheets.

Claude Opus 4.1

The next generation of Anthropic’s most powerful model yet, Claude Opus 4.1 is an industry leader for coding. It delivers sustained performance on long-running tasks that require focused effort and thousands of steps, significantly expanding what AI agents can solve. Claude Opus 4.1 is ideal for powering frontier agent products and features.

Claude Sonnet 4

Anthropic's mid-size model with superior intelligence for high-volume uses in coding, in-depth research, agents, & more.

Claude Opus 4

Anthropic’s most powerful model yet and the state-of-the-art coding model. It delivers sustained performance on long-running tasks that require focused effort and thousands of steps, significantly expanding what AI agents can solve. Claude Opus 4 is ideal for powering frontier agent products and features.

Claude 3.7 Sonnet

Industry-leading model for coding and powering AI agents—and the first Claude model to offer extended thinking.

Claude 3.5 Sonnet v2 (Deprecated)

The upgraded Claude 3.5 Sonnet is now state-of-the-art for a variety of tasks including real-world software engineering, enhanced agentic capabilities, and computer use.

Claude 3.5 Haiku (Deprecated)

Claude 3.5 Haiku, Anthropic’s fastest and most cost-effective model, excels at use cases like code and test case generation, sub-agents, and user-facing chatbots.

Claude 3.5 Sonnet (Deprecated)

Anthropic's most powerful AI model. Claude 3.5 Sonnet outperforms competitor models and Claude 3 Opus at higher speeds and lower cost.

Claude 3 Opus (Deprecated)

Claude 3 Opus is Anthropic's second-most intelligent AI model, with top-level performance on highly complex tasks.

Claude 3 Haiku (Deprecated)

Anthropic Claude 3 Haiku is Anthropic's fastest, most compact vision and text model for near-instant responses to simple queries, meant for seamless AI experiences mimicking human interactions.

Objective

This notebook shows how to use Vertex AI API and Anthropic’s Vertex SDK for Python to call the Claude models on Vertex AI API.

For more information and list of supported regions, see the Use Claude documentation.

Vertex AI API

Get Started

Authenticate your notebook environment (Colab only)

Authenticate your environment on Google Colab.

In [ ]:
import sys

if "google.colab" in sys.modules:

    from google.colab import auth

    auth.authenticate_user()

Select Claude model

In [ ]:
MODEL = "claude-fable-5-1"  # @param ["claude-fable-5-1","claude-opus-5","claude-sonnet-5","claude-fable-5","claude-opus-4-8","claude-opus-4-7","claude-sonnet-4-6","claude-opus-4-6","claude-opus-4-5","claude-haiku-4-5","claude-sonnet-4-5","claude-opus-4-1","claude-sonnet-4","claude-opus-4","claude-3-7-sonnet","claude-3-5-sonnet-v2","claude-3-5-haiku","claude-3-5-sonnet","claude-3-opus","claude-3-haiku"]
# Available regions per model.
MODEL_AVAILABLE_REGIONS = {
    "claude-fable-5-1": ["global", "us", "eu"],
    "claude-opus-5": ["global", "us", "eu"],
    "claude-sonnet-5": ["global", "us", "eu"],
    "claude-fable-5": ["global", "us", "eu"],
    "claude-opus-4-8": ["global", "us", "eu"],
    "claude-opus-4-7": ["global", "us", "eu"],
    "claude-sonnet-4-6": ["us-east5", "europe-west1", "asia-southeast1", "global"],
    "claude-opus-4-6": [
        "us-east5",
        "us-west4",
        "us-east1",
        "us-south1",
        "europe-west1",
        "europe-west4",
        "europe-north1",
        "asia-southeast1",
        "global",
    ],
    "claude-opus-4-5": ["us-east5", "europe-west1", "asia-southeast1", "global"],
    "claude-haiku-4-5": ["us-east5", "europe-west1", "global"],
    "claude-sonnet-4-5": ["us-east5", "europe-west1", "asia-southeast1", "global"],
    "claude-opus-4-1": ["us-east5", "europe-west4", "global"],
    "claude-sonnet-4": ["us-east5", "europe-west4", "global"],
    "claude-opus-4": ["us-east5", "europe-west4", "global"],
    "claude-3-7-sonnet": ["us-east5", "europe-west1", "europe-west4", "global"],
    "claude-3-5-sonnet-v2": ["us-east5", "europe-west1", "global"],
    "claude-3-5-haiku": ["us-east5"],
    "claude-3-5-sonnet": ["us-east5", "europe-west1", "asia-southeast1"],
    "claude-3-opus": ["us-east5"],
    "claude-3-haiku": ["us-east5", "europe-west1", "asia-southeast1"],
}

available_regions = MODEL_AVAILABLE_REGIONS[MODEL]

Select a location

In [ ]:
import ipywidgets as widgets
from IPython.display import display

dropdown = widgets.Dropdown(
    options=available_regions,
    description="Select a location:",
    font_weight="bold",
    style={"description_width": "initial"},
)


def dropdown_eventhandler(change):
    global LOCATION
    if change["type"] == "change" and change["name"] == "value":
        LOCATION = change.new
        print("Selected:", change.new)


LOCATION = dropdown.value
dropdown.observe(dropdown_eventhandler, names="value")
display(dropdown)

Set Google Cloud project and model information

To get started using Vertex AI, you must have an existing Google Cloud project and enable the Vertex AI API. Learn more about setting up a project and a development environment.

In [ ]:
PROJECT_ID = "[your-project-id]"  # @param {type:"string"}
MULTI_REGION_LIST = ["us", "eu"]

if LOCATION == "global":
    ENDPOINT = "https://aiplatform.googleapis.com"
elif LOCATION in MULTI_REGION_LIST:
    ENDPOINT = f"https://aiplatform.{LOCATION}.rep.googleapis.com"
else:
    ENDPOINT = f"https://{LOCATION}-aiplatform.googleapis.com"

if not PROJECT_ID or PROJECT_ID == "[your-project-id]":
    raise ValueError("Please set your PROJECT_ID")

Import required libraries

In [ ]:
import base64
import json

import requests
from IPython.display import Image

Text generation

Unary call

Sends a POST request to the specified API endpoint to get a response from the model for a banana bread recipe using the provided payload.

In [ ]:
PAYLOAD = {
    "anthropic_version": "vertex-2023-10-16",
    "messages": [{"role": "user", "content": "Send me a recipe for banana bread."}],
    "max_tokens": 100,
    "stream": False,
}

request = json.dumps(PAYLOAD)
!curl -X POST -H "Authorization: Bearer $(gcloud auth print-access-token)" -H "Content-Type: application/json" {ENDPOINT}/v1/projects/{PROJECT_ID}/locations/{LOCATION}/publishers/anthropic/models/{MODEL}:rawPredict -d '{request}'

Streaming call

Sends a POST request to the specified API endpoint to stream a response from the model for a banana bread recipe using the provided payload.

In [ ]:
PAYLOAD = {
    "anthropic_version": "vertex-2023-10-16",
    "messages": [{"role": "user", "content": "Send me a recipe for banana bread."}],
    "max_tokens": 100,
    "stream": True,
}

request = json.dumps(PAYLOAD)
!curl -X POST -H "Authorization: Bearer $(gcloud auth print-access-token)" -H "Content-Type: application/json" {ENDPOINT}/v1/projects/{PROJECT_ID}/locations/{LOCATION}/publishers/anthropic/models/{MODEL}:streamRawPredict -d '{request}'

Vision

Encode And Preview Image

In [ ]:
image_url = "https://upload.wikimedia.org/wikipedia/commons/a/a7/Camponotus_flavomarginatus_ant.jpg"

# Wikimedia Foundation blocks requests from default HTTP client libraries that
# do not specify a custom `User-Agent` header.
headers = {
    "User-Agent": (
        "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML,"
        " like Gecko) Chrome/115.0.0.0 Safari/537.36"
    )
}

response = requests.get(url=image_url, headers=headers)
response.raise_for_status()

image_b64 = base64.b64encode(response.content).decode("utf-8")
display(Image(data=response.content, width=300))

Unary call

Sends a POST request to the specified API endpoint to get a response from the model analyzing the content of an image, provided as base64-encoded data, along with the text prompt.

In [ ]:
PAYLOAD = {
    "anthropic_version": "vertex-2023-10-16",
    "messages": [
        {
            "role": "user",
            "content": [
                {
                    "type": "image",
                    "source": {
                        "type": "base64",
                        "media_type": "image/jpeg",
                        "data": image_b64,
                    },
                },
                {"type": "text", "text": "What is in this image?"},
            ],
        }
    ],
    "max_tokens": 100,
    "stream": False,
}

# Save payload to request.json
with open("request.json", "w") as f:
  json.dump(PAYLOAD, f)

# Pass the file to curl using -d @request.json
!curl -X POST \
  -H "Authorization: Bearer $(gcloud auth print-access-token)" \
  -H "Content-Type: application/json" \
  "{ENDPOINT}/v1/projects/{PROJECT_ID}/locations/{LOCATION}/publishers/anthropic/models/{MODEL}:rawPredict" \
  -d @request.json

Streaming call

Sends a POST request to the specified API endpoint to stream a response from the model analyzing the content of an image, provided as base64-encoded data, along with the text prompt.

In [ ]:
PAYLOAD = {
    "anthropic_version": "vertex-2023-10-16",
    "messages": [
        {
            "role": "user",
            "content": [
                {
                    "type": "image",
                    "source": {
                        "type": "base64",
                        "media_type": "image/jpeg",
                        "data": image_b64,
                    },
                },
                {"type": "text", "text": "What is in this image?"},
            ],
        }
    ],
    "max_tokens": 100,
    "stream": True,
}

# Save payload to request.json
with open("request.json", "w") as f:
  json.dump(PAYLOAD, f)

# Pass the file to curl using -d @request.json
!curl -X POST \
  -H "Authorization: Bearer $(gcloud auth print-access-token)" \
  -H "Content-Type: application/json" \
  {ENDPOINT}/v1/projects/{PROJECT_ID}/locations/{LOCATION}/publishers/anthropic/models/{MODEL}:streamRawPredict \
  -d @request.json

Using Anthropic's Vertex SDK for Python

Get Started

Install Anthropic's Vertex SDK for Python and other required packages

In [ ]:
! pip3 install -U -q 'anthropic[vertex]'

Restart runtime (Colab only)

To use the newly installed packages, you must restart the runtime on Google Colab.

In [ ]:
# Restart kernel after installs so that your environment can access the new packages
import sys

if "google.colab" in sys.modules:
    import IPython

    app = IPython.Application.instance()
    app.kernel.do_shutdown(True)
⚠️ The kernel is going to restart. Wait until it's finished before continuing to the next step. ⚠️

Authenticate your notebook environment (Colab only)

Authenticate your environment on Google Colab.

In [ ]:
import sys

if "google.colab" in sys.modules:

    from google.colab import auth

    auth.authenticate_user()

Select Claude model

In [ ]:
MODEL = "claude-fable-5-1"  # @param ["claude-fable-5-1","claude-opus-5","claude-sonnet-5","claude-fable-5","claude-opus-4-8","claude-opus-4-7","claude-sonnet-4-6","claude-opus-4-6","claude-opus-4-5","claude-haiku-4-5","claude-sonnet-4-5","claude-opus-4-1","claude-sonnet-4","claude-opus-4","claude-3-7-sonnet","claude-3-5-sonnet-v2","claude-3-5-haiku","claude-3-5-sonnet","claude-3-opus","claude-3-haiku"]
# Available regions per model.
MODEL_AVAILABLE_REGIONS = {
    "claude-fable-5-1": ["global", "us", "eu"],
    "claude-opus-5": ["global", "us", "eu"],
    "claude-sonnet-5": ["global", "us", "eu"],
    "claude-fable-5": ["global", "us", "eu"],
    "claude-opus-4-8": ["global", "us", "eu"],
    "claude-opus-4-7": ["global", "us", "eu"],
    "claude-sonnet-4-6": ["us-east5", "europe-west1", "asia-southeast1", "global"],
    "claude-opus-4-6": [
        "us-east5",
        "us-west4",
        "us-east1",
        "us-south1",
        "europe-west1",
        "europe-west4",
        "europe-north1",
        "asia-southeast1",
        "global",
    ],
    "claude-opus-4-5": ["us-east5", "europe-west1", "asia-southeast1", "global"],
    "claude-haiku-4-5": ["us-east5", "europe-west1", "global"],
    "claude-sonnet-4-5": ["us-east5", "europe-west1", "asia-southeast1", "global"],
    "claude-opus-4-1": ["us-east5", "europe-west4", "global"],
    "claude-sonnet-4": ["us-east5", "europe-west4", "global"],
    "claude-opus-4": ["us-east5", "europe-west4", "global"],
    "claude-3-7-sonnet": ["us-east5", "europe-west1", "europe-west4", "global"],
    "claude-3-5-sonnet-v2": ["us-east5", "europe-west1", "global"],
    "claude-3-5-haiku": ["us-east5"],
    "claude-3-5-sonnet": ["us-east5", "europe-west1", "asia-southeast1"],
    "claude-3-opus": ["us-east5"],
    "claude-3-haiku": ["us-east5", "europe-west1", "asia-southeast1"],
}

available_regions = MODEL_AVAILABLE_REGIONS[MODEL]

Select a region

In [ ]:
import ipywidgets as widgets
from IPython.display import display

dropdown = widgets.Dropdown(
    options=available_regions,
    description="Select a location:",
    font_weight="bold",
    style={"description_width": "initial"},
)


def dropdown_eventhandler(change):
    global LOCATION
    if change["type"] == "change" and change["name"] == "value":
        LOCATION = change.new
        print("Selected:", change.new)


LOCATION = dropdown.value
dropdown.observe(dropdown_eventhandler, names="value")
display(dropdown)

Set Google Cloud project and model information

To get started using Vertex AI, you must have an existing Google Cloud project and enable the Vertex AI API. Learn more about setting up a project and a development environment.

In [ ]:
PROJECT_ID = "[your-project-id]"  # @param {type:"string"}

if not PROJECT_ID or PROJECT_ID == "[your-project-id]":
    raise ValueError("Please set your PROJECT_ID")

Import required libraries

In [ ]:
import base64

import requests
from IPython.display import Image

Text generation

Unary call

Initializes a client for Anthropic's Vertex AI, sends a request to generate the content, and prints the response in a formatted JSON

In [ ]:
from anthropic import AnthropicVertex

client = AnthropicVertex(region=LOCATION, project_id=PROJECT_ID)
message = client.messages.create(
    max_tokens=1024,
    messages=[
        {
            "role": "user",
            "content": "Send me a recipe for banana bread.",
        }
    ],
    model=MODEL,
)
print(message.model_dump_json(indent=2))

Streaming call

Initializes a client for Anthropic's Vertex AI, sends a streaming request to generate the content, and continuously prints the received text as it is streamed.

In [ ]:
from anthropic import AnthropicVertex

client = AnthropicVertex(region=LOCATION, project_id=PROJECT_ID)

with client.messages.stream(
    max_tokens=1024,
    messages=[
        {
            "role": "user",
            "content": "Send me a recipe for banana bread.",
        }
    ],
    model=MODEL,
) as stream:
    for text in stream.text_stream:
        print(text, end="", flush=True)

Vision

Encode And Preview Image

In [ ]:
image_url = "https://upload.wikimedia.org/wikipedia/commons/a/a7/Camponotus_flavomarginatus_ant.jpg"
image_media_type = "image/jpeg"

# Wikimedia Foundation blocks requests from default HTTP client libraries that
# do not specify a custom `User-Agent` header.
headers = {
    "User-Agent": (
        "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML,"
        " like Gecko) Chrome/115.0.0.0 Safari/537.36"
    )
}

response = requests.get(url=image_url, headers=headers)
response.raise_for_status()

image_b64 = base64.b64encode(response.content).decode("utf-8")
display(Image(data=response.content, width=300))

Unary call

Initializes a client for Anthropic's Vertex AI, sends a request to describe an image (provided as base64-encoded data) along with a text prompt, and prints the response in a formatted JSON.

In [ ]:
from anthropic import AnthropicVertex

client = AnthropicVertex(region=LOCATION, project_id=PROJECT_ID)

message = client.messages.create(
    max_tokens=1024,
    messages=[
        {
            "role": "user",
            "content": [
                {
                    "type": "image",
                    "source": {
                        "type": "base64",
                        "media_type": image_media_type,
                        "data": image_b64,
                    },
                },
                {"type": "text", "text": "Describe this image."},
            ],
        }
    ],
    model=MODEL,
)
print(message.model_dump_json(indent=2))

Streaming call

Initializes a client for Anthropic's Vertex AI, sends a streaming request to describe an image (provided as base64-encoded data) along with a text prompt, and continuously prints the received text as it is streamed.

In [ ]:
from anthropic import AnthropicVertex

client = AnthropicVertex(region=LOCATION, project_id=PROJECT_ID)

with client.messages.stream(
    max_tokens=1024,
    messages=[
        {
            "role": "user",
            "content": [
                {
                    "type": "image",
                    "source": {
                        "type": "base64",
                        "media_type": image_media_type,
                        "data": image_b64,
                    },
                },
                {"type": "text", "text": "Describe this image."},
            ],
        }
    ],
    model=MODEL,
) as stream:
    for text in stream.text_stream:
        print(text, end="", flush=True)