strands.models.anthropic
Anthropic Claude model provider.
AnthropicModel
Section titled “AnthropicModel”class AnthropicModel(Model)Defined in: src/strands/models/anthropic.py:31
Anthropic model provider implementation.
AnthropicConfig
Section titled “AnthropicConfig”class AnthropicConfig(TypedDict)Defined in: src/strands/models/anthropic.py:49
Configuration options for Anthropic models.
Attributes:
max_tokens- Maximum number of tokens to generate.model_id- Calude model ID (e.g., “claude-3-7-sonnet-latest”). For a complete list of supported models, see https://docs.anthropic.com/en/docs/about-claude/models/all-models.params- Additional model parameters (e.g., temperature). For a complete list of supported parameters, see https://docs.anthropic.com/en/api/messages.
__init__
Section titled “__init__”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.
update_config
Section titled “update_config”@overridedef update_config(**model_config: Unpack[AnthropicConfig]) -> NoneDefined in: src/strands/models/anthropic.py:82
Update the Anthropic model configuration with the provided arguments.
Arguments:
**model_config- Configuration overrides.
get_config
Section titled “get_config”@overridedef get_config() -> AnthropicConfigDefined in: src/strands/models/anthropic.py:92
Get the Anthropic model configuration.
Returns:
The Anthropic model configuration.
format_request
Section titled “format_request”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.
format_chunk
Section titled “format_chunk”def format_chunk(event: dict[str, Any]) -> StreamEventDefined 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.
stream
Section titled “stream”@overrideasync 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.
structured_output
Section titled “structured_output”@overrideasync 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.