Skip to main content
Anthropic’s Claude is a family of large language models developed by Anthropic and designed to revolutionize the way you interact with AI. Claude excels at a wide variety of tasks involving language, reasoning, analysis, coding, and more. The models are highly capable, easy to use, and can be customized to suit your needs. In this notebook, we demonstrate how to use Anthropic Claude model for AgentChat in AG2.

Features

Requirements

To use Anthropic Claude with AG2, first you need to install the ag2[anthropic] package. To try out the function call feature of Claude model, you need to install anthropic>=0.23.1.
If you have been using autogen or ag2, all you need to do is upgrade it using:
or
as autogen and ag2 are aliases for the same PyPI package.

Set the config for the Anthropic API

You can add any parameters that are needed for the custom model loading in the same configuration list. It is important to add the api_type field and set it to a string that corresponds to the client type used: anthropic. Example:

Alternative

As an alternative to the api_key key and value in the config, you can set the environment variable ANTHROPIC_API_KEY to your Anthropic API key. Linux/Mac:
Windows:

Alternative Anthropic VertexAI Client (GCP)

To use the Anthropic VertexAI client in AG2, you need to configure it for use with Google Cloud Platform (GCP). Ensure you have the necessary project credentials and install the required package. Configuration The following configuration example demonstrates how to set up Anthropic VertexAI:

Alternative Anthropic VertexAI Client (Google Default Authentication)

If the gcp_auth_token is not provided in the configuration, the client will use Google’s default authentication mechanism. This requires the appropriate credentials to be configured in your environment, such as:
  • Service account key: You can set the GOOGLE_APPLICATION_CREDENTIALS environment variable to point to your service account key file.
  • Cloud Shell or GCP Compute Engine: When running in a GCP-managed environment, default authentication is automatically applied.
Example of setting up the environment variable:
This allows seamless integration without explicitly specifying the authentication token in your code.

Two-agent Coding Example

Construct Agents

Construct a simple conversation between a User proxy and an ConversableAgent based on Claude-3 model.

Tool Call Example with the Latest Anthropic API

Anthropic announced that tool use is supported in the Anthropic API. To use this feature, please install anthropic>=0.23.1.

Register the function

Group Chat Example with both Claude and GPT Agents

A group chat with GPT-4 as the judge

Same group chat with Claude 3.5 Sonnet as the judge

Thinking mode

You can utilize Anthropic’s thinking mode by setting it in the configuration.