Skip to content

Claude

ClaudeModelName module-attribute

ClaudeModelName = Literal['claude-3-5-haiku-20241022', 'claude-3-5-sonnet-20241022']

Claude

Claude(logger: Logger, model_name: ClaudeModelName, prompt_caching: bool = False, system_extension: str | None = None, system_message: str | None = None, retry_max_attempts: int = 10, retry_wait_strategy: WaitStrategy = WaitExponential(multiplier=1, max=10, exp_base=2))

Bases: CodeActModel

A CodeActModel implementation based on Anthropic's Claude API.

Parameters:

Name Type Description Default
logger Logger

Logger instance for logging requests and responses.

required
model_name ClaudeModelName

Name of the Claude model to use (e.g., "claude-3-5-sonnet-20241022").

required
prompt_caching bool

Whether to enable prompt caching. Defaults to False.

False
system_extension str | None

Additional system prompt text. Defaults to None.

None
system_message str | None

Complete system message to override default. Defaults to None.

None
retry_max_attempts int

Maximum number of retry attempts. Defaults to 10.

10
Source code in freeact/model/claude/model.py
def __init__(
    self,
    logger: Logger,
    model_name: ClaudeModelName,
    prompt_caching: bool = False,
    system_extension: str | None = None,
    system_message: str | None = None,
    retry_max_attempts: int = 10,
    retry_wait_strategy: WaitStrategy = WaitExponential(multiplier=1, max=10, exp_base=2),
):
    if system_message and system_extension:
        raise ValueError("If system_message is provided, system_extension must be None")

    if system_message:
        self.system_message = system_message
    else:
        self.system_message = SYSTEM_TEMPLATE.format(extensions=system_extension or "")

    self.logger = logger
    self.model_name = model_name
    self.prompt_caching = prompt_caching

    self._history = []  # type: ignore
    self._tool_names = [t["name"] for t in TOOLS]

    self._client = AsyncAnthropic(
        default_headers={
            "anthropic-beta": "prompt-caching-2024-07-31",
        }
        if prompt_caching
        else None,
    )
    self._retry_max_attempts = retry_max_attempts
    self._retry_wait_strategy = retry_wait_strategy