Skip to content

agentix.providers.anthropic

anthropic

Anthropic model adapter.

Translates between agentix's framework-agnostic types and the Anthropic Messages API. Requires the anthropic package — install with pip install "agentix[anthropic]". The import is deferred to construction time so import agentix works without the dependency present.

Notes on the translation
  • agentix's system prompt becomes the top-level system= parameter.
  • An assistant turn with tool calls becomes an assistant message whose content is a list of text + tool_use blocks. agentix stores the structured :class:ToolCall objects on the message's meta["tool_calls"] so they can be replayed here.
  • A tool result becomes a user message with a tool_result block keyed by the originating tool_use_id.
  • agentix tool schemas use parameters (a JSON Schema); Anthropic wants input_schema — translated here.

AnthropicModel

AnthropicModel(
    *,
    model: str = DEFAULT_MODEL,
    max_tokens: int = 4096,
    api_key: str | None = None,
    client: Any = None,
    thinking: Thinking | None = None,
    effort: Effort | None = None,
    task_budget: int | None = None,
    **extra: Any,
)

A :class:~agentix.model.ModelFn backed by the Anthropic Messages API.

Example::

from agentix import Agent
from agentix.providers.anthropic import AnthropicModel

agent = Agent(model=AnthropicModel(), system_prompt="...")

Typed knobs (the cost-vs-quality / reasoning controls):

  • thinkingTrue / "adaptive" (let the model decide how much to think), "summarized" (adaptive + a visible summary), "disabled", or a raw dict. Note: on Opus 4.7+/Fable, extended thinking is adaptive only; "disabled" is rejected on Fable — omit it there.
  • effort"low" | "medium" | "high" | "xhigh" | "max"; sets output_config.effort (default is "high"). Lower = fewer tokens.
  • task_budget — an int token budget the model self-moderates against for the whole agentic loop (≥ 20000); adds the required beta header. Distinct from max_tokens (a hard per-response cap).

extra keyword arguments are forwarded to messages.create. For provider-enforced JSON, pass output_config={"format": {"type": "json_schema", "schema": ...}} (merged with effort/task_budget); pair with the agent's output_validator for client-side validation + retry. Tool schemas with a strict key are forwarded for strict tool validation.

Refusal fallback: a safety refusal surfaces as a normal final answer (stop_reason == "refusal"), not an exception — so :class:~agentix.resilience.FallbackModel (which falls back on errors) does not catch it. To fall back on a refusal, use the Claude API's server-side fallbacks parameter (pass it via extra) or detect the refusal text in your app. FallbackModel/RetryModel remain the right tools for outages and transient errors.

with_response_format

with_response_format(
    schema: dict[str, Any],
) -> AnthropicModel

Return a copy that enforces JSON-schema output via output_config.format (used by Agent(response_model=…)).