anthropic_api
microbots.llm.anthropic_api
¶
Anthropic Messages API client implementing the LLMInterface with native tool calling.
MAX_TOKENS = 16000
module-attribute
¶
api_key = os.getenv('ANTHROPIC_API_KEY')
module-attribute
¶
deployment_name = os.getenv('ANTHROPIC_DEPLOYMENT_NAME')
module-attribute
¶
endpoint = os.getenv('ANTHROPIC_END_POINT')
module-attribute
¶
logger = getLogger(__name__)
module-attribute
¶
AnthropicApi(system_prompt, tools: list[ToolAbstract], deployment_name=deployment_name, token_provider: Callable[[], str] | None = None)
¶
Bases: LLMInterface
LLM client backed by the Anthropic Messages API.
The model acts through native tool calls. The API keeps no conversation state, so the full history is sent with every request, and automatic prompt caching avoids reprocessing the part that was already sent.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
system_prompt
|
str
|
System prompt sent with every request. |
required |
tools
|
list[ToolAbstract]
|
Tools the model can call. Each tool's |
required |
deployment_name
|
str
|
Anthropic model name (e.g. 'claude-sonnet-4-5'). |
deployment_name
|
token_provider
|
Callable[[], str] | None
|
Optional callable returning an Azure AD bearer token, used with Azure AI Foundry instead of ANTHROPIC_API_KEY when provided. |
None
|
Create the client.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
system_prompt
|
str
|
System prompt sent with every request. |
required |
tools
|
list[ToolAbstract]
|
Tools the model can call. Each tool's |
required |
deployment_name
|
str
|
Anthropic model name (e.g. 'claude-sonnet-4-5'). |
deployment_name
|
token_provider
|
Callable[[], str] | None
|
Optional callable returning an Azure AD bearer token, used with Azure AI Foundry instead of ANTHROPIC_API_KEY when provided. |
None
|
Raises:
| Type | Description |
|---|---|
ValueError
|
If no authentication is configured or the token provider is invalid. |
Source code in src/microbots/llm/anthropic_api.py
ai_client = AnthropicFoundry(azure_ad_token_provider=token_provider, base_url=endpoint)
instance-attribute
¶
deployment_name = deployment_name
instance-attribute
¶
messages = []
instance-attribute
¶
system_prompt = system_prompt
instance-attribute
¶
token_provider = token_provider
instance-attribute
¶
tools = [self._to_anthropic_tool(tool) for tool in tools]
instance-attribute
¶
unanswered_call_ids = []
instance-attribute
¶
ask(message) -> LLMAskResponse
¶
Send a message to the LLM and return its parsed response.
Returns:
| Type | Description |
|---|---|
LLMAskResponse
|
The parsed LLM response. |
Source code in src/microbots/llm/anthropic_api.py
clear_history()
¶
Clear the LLM's conversation history.
Returns:
| Type | Description |
|---|---|
bool
|
True if the history was cleared successfully. |
send_tool_outputs(outputs: dict[str, str]) -> LLMAskResponse
¶
Send the outputs of the requested tool calls and return the LLM's next response.
Returns:
| Type | Description |
|---|---|
LLMAskResponse
|
The parsed LLM response. |