Skip to main content

Overview

The AutoGen.Anthropic package provides integration with Anthropic’s Claude models, including support for function calling, streaming, and prompt caching.

Installation

Basic Setup

1

Get your API key

Obtain an API key from Anthropic Console.
2

Set environment variable

3

Create an agent

AnthropicClientAgent

The main agent class for Claude models:

Constructor Parameters

AnthropicClient
required
Anthropic client instance
string
required
Unique identifier for the agent
string
required
Claude model to use (e.g., Claude 3 Opus, Sonnet, Haiku)
string
Instructions defining the agent’s behavior
float
default:"0.7"
Sampling temperature (0.0 = deterministic, 1.0 = creative)
int
default:"1024"
Maximum tokens to generate in response

Available Models

Claude models available through AutoGen:

Basic Usage

Simple conversation with Claude:

Streaming Responses

Stream responses token-by-token:

Function Calling (Tool Use)

Claude supports function calling through its tool use API:
1

Define functions

2

Register tools with agent

3

Use the agent

Prompt Caching

Claude supports prompt caching to reduce costs for repeated context:
Prompt caching is automatically enabled for Claude 3.5 Sonnet and Claude 3 Opus. Cached content reduces costs by up to 90% for repeated context.

Message Connector

Register the message connector to handle AutoGen message types:

Multi-Agent Conversation

Use Claude in group chats:

Vision Support

Claude 3 models support image understanding:

Configuration Options

Temperature Control

Token Limits

Best Practices

  • Claude 3.5 Sonnet: Best overall performance, coding, analysis
  • Claude 3 Opus: Most capable for complex reasoning
  • Claude 3 Sonnet: Balanced performance and cost
  • Claude 3 Haiku: Fast and affordable for simple tasks
  • Reuse AnthropicClient and HttpClient instances
  • Use streaming for long responses
  • Enable prompt caching for repeated contexts
  • Set appropriate maxTokens to control costs
  • Use Haiku for high-throughput applications

Environment Variables

string
required
Your Anthropic API key from console.anthropic.com

Comparison with OpenAI

Claude Advantages

  • Longer context windows (200K tokens)
  • Better instruction following
  • Stronger refusal of harmful requests
  • Prompt caching for cost savings
  • Constitutional AI training

When to Use Each

  • Claude: Long documents, detailed analysis, code review
  • GPT-4: Function calling, JSON mode, broader ecosystem
  • Mix both: Use strengths of each model in group chats

Next Steps

OpenAI Integration

Use GPT models with AutoGen

Function Calling

Add tools to Claude agents

Group Chat

Create multi-agent workflows

Examples

See complete examples