windlass.providers.llm.anthropic¶
anthropic
¶
Anthropic Claude adapter.
Claude's Messages API differs from OpenAI's in three ways that matter, and this adapter absorbs all three so callers never have to think about them:
- the system prompt is a top-level parameter, not a message;
- content is a list of typed blocks (
text,tool_use,tool_result) rather than a string; - consecutive same-role messages must be merged.
Install with::
pip install "windlass[anthropic]"
Example
from windlass import Windlass # doctest: +SKIP Windlass.llm("anthropic").complete("Say hi").content # doctest: +SKIP 'Hi!'
AnthropicLLM
¶
AnthropicLLM(
model: str = "",
*,
api_key: str | None = None,
base_url: str | None = None,
max_retries: int = 0,
thinking_budget: int | None = None,
**config: Any
)
Bases: LLM
Chat completions via the official anthropic SDK.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
model
|
str
|
Model id, e.g. |
''
|
api_key
|
str | None
|
Credential. Falls back to |
None
|
base_url
|
str | None
|
Endpoint override. |
None
|
max_retries
|
int
|
SDK-level retries; 0 because Windlass retries itself. |
0
|
thinking_budget
|
int | None
|
Enables extended thinking with this token budget.
|
None
|
**config
|
Any
|
Forwarded to :class: |
{}
|
Raises:
| Type | Description |
|---|---|
MissingDependencyError
|
When |
AuthenticationError
|
When no API key can be found. |
Source code in src\windlass\providers\llm\anthropic.py
default_model
classmethod
¶
native
¶
agenerate
async
¶
agenerate(
messages: list[Message], *, tools: list[dict[str, Any]] | None = None, **kwargs: Any
) -> Completion
Request one message completion.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
messages
|
list[Message]
|
The conversation. A leading system message is lifted into
the top-level |
required |
tools
|
list[dict[str, Any]] | None
|
Anthropic-format tool definitions ( |
None
|
**kwargs
|
Any
|
Request overrides. |
{}
|
Returns:
| Type | Description |
|---|---|
Completion
|
The completion, with the SDK response on |
Raises:
| Type | Description |
|---|---|
AuthenticationError
|
Invalid credentials. |
RateLimitError
|
Quota or rate limit hit. |
ProviderError
|
Any other API failure. |
Source code in src\windlass\providers\llm\anthropic.py
astream_generate
async
¶
astream_generate(
messages: list[Message], *, tools: list[dict[str, Any]] | None = None, **kwargs: Any
) -> AsyncIterator[StreamEvent]
Stream a message completion.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
messages
|
list[Message]
|
The conversation. |
required |
tools
|
list[dict[str, Any]] | None
|
Anthropic-format tool definitions. |
None
|
**kwargs
|
Any
|
Request overrides. |
{}
|
Yields:
| Type | Description |
|---|---|
AsyncIterator[StreamEvent]
|
Text deltas, then completed tool calls, then |
Source code in src\windlass\providers\llm\anthropic.py
aclose
async
¶
to_anthropic_messages
¶
Translate Windlass messages into Anthropic's format.
Performs the three transformations Claude requires: system extraction,
typed content blocks, and merging of consecutive same-role turns (the API
rejects two user messages in a row, which happens naturally when several
tools return at once).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
messages
|
list[Message]
|
The conversation. |
required |
Returns:
| Type | Description |
|---|---|
tuple[str, list[dict[str, Any]]]
|
A |
Example
from windlass.core.types import Message system, msgs = to_anthropic_messages( ... [Message.system("be nice"), Message.user("hi")] ... ) system, msgs[0]["role"] ('be nice', 'user')