AnthropicEngine¶
The AnthropicEngine is used to make requests to the Anthropic API.
TL;DR
# see https://docs.anthropic.com/claude/docs/models-overview for a list of model IDs
from kani.engines.anthropic import AnthropicEngine
engine = AnthropicEngine(api_key=os.getenv("ANTHROPIC_API_KEY"), model="claude-sonnet-4-0")
Reference¶
- class kani.engines.anthropic.AnthropicEngine(
- api_key: str = None,
- model: str = 'claude-sonnet-4-0',
- max_tokens: int = 2048,
- max_context_size: int = None,
- *,
- retry: int = 2,
- api_base: str = None,
- headers: dict = None,
- client: AsyncAnthropic = None,
- **hyperparams,
Engine for using the Anthropic API.
This engine supports all Claude models. See https://docs.anthropic.com/claude/docs/getting-access-to-claude for information on accessing the Claude API.
See https://docs.anthropic.com/en/docs/about-claude/models/overview for a list of available models.
Multimodal support: images.
Additional capabilities: PDF document processing. Use
kani.ext.multimodal_core.BinaryFilePart.Message Extras:
"anthropic_message": The Message (raw response) returned by the Anthropic servers.- Parameters:
api_key – Your Anthropic API key. By default, the API key will be read from the ANTHROPIC_API_KEY environment variable.
model – The id of the model to use (e.g. “claude-opus-4-0”). See https://docs.anthropic.com/en/docs/about-claude/models/overview for a list of models.
max_tokens – The maximum number of tokens to sample at each generation (defaults to 2048). Generally, you should set this to the same number as your Kani’s
desired_response_tokens.max_context_size – The maximum amount of tokens allowed in the chat prompt. If None, uses the given model’s full context size.
retry – How many times the engine should retry failed HTTP calls with exponential backoff (default 2).
api_base – The base URL of the Anthropic API to use.
headers – A dict of HTTP headers to include with each request.
client – An instance of
anthropic.AsyncAnthropic(for reusing the same client in multiple engines). You must specify exactly one of (api_key, client). If this is passed theretry,api_base, andheadersparams will be ignored.hyperparams – Any additional parameters to pass to the underlying API call (see https://docs.claude.com/en/api/messages).
- class kani.engines.anthropic.AnthropicUnknownPart(*, extra: dict = {})[source]
A generic unknown response part from the server.
This generally corresponds to an Anthropic-specific feature. The raw response data is accessible in
data, and will be sent back to the language model in future rounds correctly. Will not be sent to other engines.- data: dict
The raw content of the part returned by the Anthropic API.
- extra: dict
Specific engines may store additional extra data in this dictionary. See an engine’s documentation for details about any extras it may store or expect.
This key will only be persisted to disk on a best-effort basis – any value that is not JSON-serializable or a Pydantic class will be cast to a repr. Upon loading, values may not retain the same type as they were saved as (Pydantic objects will be loaded as a dict).
Recipes¶
Server-Side Tools¶
To enable server-side tools, you pass them as additional arguments to the tools API argument.
You can do this by overriding AnthropicEngine._prepare_request.
from kani.engines.anthropic import AnthropicEngine
class AnthropicServersideToolsEngine(AnthropicEngine):
def __init__(self, *args, additional_tools: list = None, **kwargs):
super().__init__(*args, **kwargs)
self.additional_tools = additional_tools or []
# override prepare_request to inject serverside tool configs
def _prepare_request(self, messages, functions):
kwargs, prompt_msgs = super()._prepare_request(messages, functions)
if self.additional_tools:
kwargs.setdefault("tools", [])
kwargs["tools"].extend(self.additional_tools)
return kwargs, prompt_msgs
web_search_engine = AnthropicServersideToolsEngine(..., additional_tools=[
{"name": "web_search", "type": "web_search_20250305"}
])
PDF File Inputs¶
from kani import Kani
from kani.engines.anthropic import AnthropicEngine
from kani.ext.multimodal_core import BinaryFilePart
engine = AnthropicEngine(api_key=os.getenv("ANTHROPIC_API_KEY"), model="claude-sonnet-4-0")
ai = Kani(engine)
msg = await ai.chat_round([
BinaryFilePart.from_file("/path/to/file.pdf"),
"Summarize this PDF."
])
print(msg.text)