AnthropicV2Client
autogen.llm_clients.anthropic_v2.AnthropicV2Client #
Bases: ModelClient
Anthropic Messages API client implementing ModelClientV2 protocol.
This client works with Anthropic's Messages API (client.messages.create) which returns structured output with thinking blocks, tool calls, and more.
Key Features: - Preserves thinking blocks as ReasoningContent (extended thinking feature) - Handles tool calls and results - Supports native structured outputs (beta API) and JSON Mode fallback - Provides backward compatibility via create_v1_compatible() - Supports multiple authentication methods (API key, AWS Bedrock, GCP Vertex)
Example
client = AnthropicCompletionsClient(api_key="...")
Get rich response with thinking#
response = client.create({ "model": "claude-3-5-sonnet-20241022", "messages": [{"role": "user", "content": "Explain quantum computing"}] })
Access thinking blocks#
for reasoning in response.reasoning: print(f"Thinking: {reasoning.reasoning}")
Get text response#
print(f"Answer: {response.text}")
Initialize Anthropic Messages API client.
| PARAMETER | DESCRIPTION |
|---|---|
api_key | Anthropic API key (or set ANTHROPIC_API_KEY env var) TYPE: |
base_url | Optional base URL for the API TYPE: |
timeout | Optional timeout in seconds TYPE: |
response_format | Optional response format for structured outputs |
**kwargs | Additional arguments passed to Anthropic client TYPE: |
Source code in autogen/llm_clients/anthropic_v2.py
RESPONSE_USAGE_KEYS class-attribute instance-attribute #
ModelClientResponseProtocol #
create #
Create a completion and return UnifiedResponse with all features preserved.
This method implements ModelClient.create() but returns UnifiedResponse instead of ModelClientResponseProtocol. The rich UnifiedResponse structure is compatible via duck typing - it has .model attribute and works with message_retrieval().
Automatically selects the best structured output method: - Native structured outputs for Claude Sonnet 4.5+ (guaranteed schema compliance) - JSON Mode for older models (prompt-based with
| PARAMETER | DESCRIPTION |
|---|---|
params | Request parameters including: - model: Model name (e.g., "claude-3-5-sonnet-20241022") - messages: List of message dicts - temperature: Optional temperature - max_tokens: Optional max completion tokens - tools: Optional tool definitions - response_format: Optional Pydantic BaseModel or JSON schema dict - **other Anthropic parameters |
| RETURNS | DESCRIPTION |
|---|---|
UnifiedResponse | UnifiedResponse with thinking blocks, tool calls, and all content preserved |
Source code in autogen/llm_clients/anthropic_v2.py
load_config #
Load the configuration for the Anthropic API client.
Source code in autogen/llm_clients/anthropic_v2.py
create_v1_compatible #
Create completion in backward-compatible ChatCompletion format.
This method provides compatibility with existing AG2 code that expects ChatCompletion format. Note that thinking blocks will be preserved in the content string with [Thinking] tags, matching V1 behavior.
| PARAMETER | DESCRIPTION |
|---|---|
params | Same parameters as create() |
| RETURNS | DESCRIPTION |
|---|---|
ChatCompletion | ChatCompletion object compatible with OpenAI format |
Warning
This method may lose some information when converting to the legacy format. Prefer create() for new code.
Source code in autogen/llm_clients/anthropic_v2.py
848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 | |
message_retrieval #
Retrieve messages from response in OpenAI-compatible format.
Returns list of strings for text-only messages, or list of dicts when tool calls or complex content is present.
| PARAMETER | DESCRIPTION |
|---|---|
response | UnifiedResponse from create() TYPE: |
| RETURNS | DESCRIPTION |
|---|---|
list[str] | list[ChatCompletionMessage] | List of strings (for text-only) OR list of message dicts (for tool calls/complex content) |
Source code in autogen/llm_clients/anthropic_v2.py
cost #
Calculate cost from response usage.
Implements ModelClient.cost() but accepts UnifiedResponse via duck typing.
| PARAMETER | DESCRIPTION |
|---|---|
response | UnifiedResponse with usage information TYPE: |
| RETURNS | DESCRIPTION |
|---|---|
float | Cost in USD for the API call |
Source code in autogen/llm_clients/anthropic_v2.py
get_usage staticmethod #
Extract usage statistics from response.
Implements ModelClient.get_usage() but accepts UnifiedResponse via duck typing.
| PARAMETER | DESCRIPTION |
|---|---|
response | UnifiedResponse from create() TYPE: |
| RETURNS | DESCRIPTION |
|---|---|
dict[str, Any] | Dict with keys from RESPONSE_USAGE_KEYS |
Source code in autogen/llm_clients/anthropic_v2.py
openai_func_to_anthropic staticmethod #
Convert OpenAI function format to Anthropic format.
| PARAMETER | DESCRIPTION |
|---|---|
openai_func | OpenAI function definition TYPE: |
| RETURNS | DESCRIPTION |
|---|---|
dict | Anthropic function definition |
Source code in autogen/llm_clients/anthropic_v2.py
convert_tools_to_functions staticmethod #
Convert tool definitions into Anthropic-compatible functions, updating nested $ref paths in property schemas.
| PARAMETER | DESCRIPTION |
|---|---|
tools | List of tool definitions TYPE: |
| RETURNS | DESCRIPTION |
|---|---|
list | List of functions with updated $ref paths |