Skip to content

strands.models.anthropic

Anthropic Claude model provider.

class AnthropicModel(Model)

Defined in: src/strands/models/anthropic.py:31

Anthropic model provider implementation.

class AnthropicConfig(TypedDict)

Defined in: src/strands/models/anthropic.py:49

Configuration options for Anthropic models.

Attributes:

def __init__(*,
client_args: dict[str, Any] | None = None,
**model_config: Unpack[AnthropicConfig])

Defined in: src/strands/models/anthropic.py:65

Initialize provider instance.

Arguments:

  • client_args - Arguments for the underlying Anthropic client (e.g., api_key). For a complete list of supported arguments, see https://docs.anthropic.com/en/api/client-sdks.
  • **model_config - Configuration options for the Anthropic model.
@override
def update_config(**model_config: Unpack[AnthropicConfig]) -> None

Defined in: src/strands/models/anthropic.py:82

Update the Anthropic model configuration with the provided arguments.

Arguments:

  • **model_config - Configuration overrides.
@override
def get_config() -> AnthropicConfig

Defined in: src/strands/models/anthropic.py:92

Get the Anthropic model configuration.

Returns:

The Anthropic model configuration.

def format_request(messages: Messages,
tool_specs: list[ToolSpec] | None = None,
system_prompt: str | None = None,
tool_choice: ToolChoice | None = None) -> dict[str, Any]

Defined in: src/strands/models/anthropic.py:204

Format an Anthropic streaming request.

Arguments:

  • messages - List of message objects to be processed by the model.
  • tool_specs - List of tool specifications to make available to the model.
  • system_prompt - System prompt to provide context to the model.
  • tool_choice - Selection strategy for tool invocation.

Returns:

An Anthropic streaming request.

Raises:

  • TypeError - If a message contains a content block type that cannot be converted to an Anthropic-compatible format.
def format_chunk(event: dict[str, Any]) -> StreamEvent

Defined in: src/strands/models/anthropic.py:257

Format the Anthropic response events into standardized message chunks.

Arguments:

  • event - A response event from the Anthropic model.

Returns:

The formatted chunk.

Raises:

  • RuntimeError - If chunk_type is not recognized. This error should never be encountered as we control chunk_type in the stream method.
@override
async def stream(messages: Messages,
tool_specs: list[ToolSpec] | None = None,
system_prompt: str | None = None,
*,
tool_choice: ToolChoice | None = None,
**kwargs: Any) -> AsyncGenerator[StreamEvent, None]

Defined in: src/strands/models/anthropic.py:375

Stream conversation with the Anthropic model.

Arguments:

  • messages - List of message objects to be processed by the model.
  • tool_specs - List of tool specifications to make available to the model.
  • system_prompt - System prompt to provide context to the model.
  • tool_choice - Selection strategy for tool invocation.
  • **kwargs - Additional keyword arguments for future extensibility.

Yields:

Formatted message chunks from the model.

Raises:

  • ContextWindowOverflowException - If the input exceeds the model’s context window.
  • ModelThrottledException - If the request is throttled by Anthropic.
@override
async def structured_output(
output_model: type[T],
prompt: Messages,
system_prompt: str | None = None,
**kwargs: Any) -> AsyncGenerator[dict[str, T | Any], None]

Defined in: src/strands/models/anthropic.py:427

Get structured output from the model.

Arguments:

  • output_model - The output model to use for the agent.
  • prompt - The prompt messages to use for the agent.
  • system_prompt - System prompt to provide context to the model.
  • **kwargs - Additional keyword arguments for future extensibility.

Yields:

Model events with the last being the structured output.