AnthropicCompatibleProvider#

Connect a provider and configure retries, credentials, and transport. See the guide.

class zett_agent.providers.anthropic_compatible.AnthropicCompatibleProvider(*, model, api_key, base_url, transport=None, retry=RetryOptions(base_delay=0.25, max_delay=8.0, max_retries=3), timeout=TimeoutOptions(connect=5.0, first_byte=60.0, idle=600.0), prompt_cache=False, max_tokens=16384, extra_headers=None, extra_query=None, extra_body=None)[source]#

Stream a service that speaks the Anthropic Messages protocol.

This is the base class of AnthropicProvider and the providers for the services that bend the protocol. A provider for another one is a subclass that sets provider_name and overrides the hooks below where the service differs; it does not copy the stream loop.

Parameters:
  • model (str) – Model identifier the endpoint accepts.

  • api_key (str) – API credential supplied by the application.

  • base_url (str) – API root. Required: only a subclass knows a default.

  • transport (httpx.AsyncBaseTransport | None) – Optional HTTPX transport for isolated tests or custom routing.

  • retry (RetryOptions) – Exponential backoff for transient failures before output starts.

  • timeout (TimeoutOptions) – How long the provider may stay silent before the request is given up on. See TimeoutOptions.

  • prompt_cache (bool) – Mark cache breakpoints, for a service that reads them. Off by default.

  • max_tokens (int) – Output budget for one response, excluding thinking.

  • extra_headers (Mapping[str, str] | None) – HTTP headers sent on every request after the adapter’s own.

  • extra_query (Mapping[str, str] | None) – URL query parameters sent on every request.

  • extra_body (Mapping[str, JsonValue] | None) – JSON fields merged into every request body last.

provider_name: str = 'anthropic'#

Name stored on messages this provider produced. Signed blocks are sent back only to the same name and model.

bearer_auth: ClassVar[bool] = False#

Authenticate with Authorization: Bearer as well as x-api-key. The services that take only the bearer token ignore the other header.

native_deferred_tools: bool = False#

See zett_agent.model.NativeDeferredToolsModel.native_deferred_tools. Only Anthropic itself loads a defer_loading tool when a tool_reference names it.

vendor_headers()[source]#

Return the headers this service wants on every request.

They are sent before extra_headers, so the application can still replace one.

Return type:

dict[str, str]

request_fields(request)[source]#

Return the reasoning and output-budget fields of request.

These are merged over the standard body. The default is Anthropic’s: extended thinking with a token budget taken from the reasoning level, and max_tokens raised by that budget because thinking is paid out of it. Thinking cannot be combined with a forced tool choice, and is explicitly disabled when the level is OFF. A field given in extra_body replaces one returned here.

Parameters:

request (ModelRequest)

Return type:

dict[str, JsonValue]

mark_cache(body, client_tools)[source]#

Put cache markers into body (and the tool definitions in client_tools).

The default marks Anthropic’s breakpoints on the largest stable request prefixes: the tool definitions, the whole system prompt, and the conversation so far. Anthropic caches only what a cache_control marker asks for, then reads each cached prefix at a fraction of the input price. A prefix shorter than the model’s minimum cacheable length is ignored silently. Nothing is marked unless prompt_cache is on.

Parameters:
Return type:

None

start_usage(usage)[source]#

Read the usage object of the message_start event.

Cache reads and writes are reported next to input_tokens, not inside it, so the total input is their sum.

Parameters:

usage (Mapping[str, JsonValue])

Return type:

ModelUsage

finish_usage(usage, update)[source]#

Fold the usage object of the message_delta event into usage.

The default takes the final output_tokens, which is the only count Anthropic updates.

Parameters:
Return type:

ModelUsage

message_stop(finish_reason, details, *, has_tool_calls)[source]#

Say why the response ended, from stop_reason and stop_details.

Parameters:
  • finish_reason (str | None)

  • details (Mapping[str, JsonValue])

  • has_tool_calls (bool)

Return type:

StopInfo

status_error(error)[source]#

Turn an SDK failure from opening the request into the typed error the runtime reports.

Parameters:

error (AnthropicError)

Return type:

ProviderError

async aclose()[source]#

Close the owned SDK connection pool.

Return type:

None

response: bool = False#

Whether this adapter uses a Responses-style endpoint instead of its provider’s traditional message or chat-completions endpoint. Only the providers read it; the runtime does not.

sync(*, runtime=None)#

Return a context-managed synchronous view; optionally share a runtime.

Omitting runtime creates a new background thread for this view. Passing an existing runtime reuses its loop and leaves its shutdown to the caller. This API is for service entry points, not Extension lifecycle hooks.

Examples

Use a model or tool from a plain function:

with read_file.sync() as read:
    result = read({"path": "README.md"})
Parameters:

runtime (SyncRuntime | None)

Return type:

SyncObject[Self]