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
systemprompt becomes the top-levelsystem=parameter. - An assistant turn with tool calls becomes an assistant message whose
contentis a list oftext+tool_useblocks. agentix stores the structured :class:ToolCallobjects on the message'smeta["tool_calls"]so they can be replayed here. - A tool result becomes a
usermessage with atool_resultblock keyed by the originatingtool_use_id. - agentix tool schemas use
parameters(a JSON Schema); Anthropic wantsinput_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):
thinking—True/"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"; setsoutput_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 frommax_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 ¶
Return a copy that enforces JSON-schema output via
output_config.format (used by Agent(response_model=…)).