AnthropicProvider#

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

class zett_agent.providers.anthropic.AnthropicProvider(model, api_key, base_url='https://api.anthropic.com', transport=None, *, response=False, retry=RetryOptions(base_delay=0.25, max_delay=8.0, max_retries=3), prompt_cache=True, max_tokens=16384, extra_headers=None, extra_query=None, extra_body=None, timeout=TimeoutOptions(connect=5.0, first_byte=60.0, idle=600.0))[源代码]#

Map Anthropic SDK streams, including signed thinking and tool blocks.

参数:
  • model (str) -- Model identifier accepted by the Anthropic-compatible endpoint.

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

  • base_url (str) -- API root, defaulting to the official Anthropic endpoint.

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

  • response (bool) -- Must remain false; this adapter uses Anthropic Messages API.

  • 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: connecting, waiting for the response headers, and between chunks of a response that has started. See TimeoutOptions; a timeout raises ProviderTimeoutError and is not retried.

  • prompt_cache (bool) -- Mark cache breakpoints on the tool definitions, the system prompt, and the conversation so far. Anthropic caches only what a marker asks for, so disabling this trades prompt-cache reads for a payload without cache_control.

  • max_tokens (int) -- Output budget for one response, excluding thinking. A tool call that writes a large file must fit in it: a response cut off by this limit leaves the call's JSON incomplete, and the model is then told to retry with a smaller call. Lower it for models with a smaller output limit.

  • extra_headers (Mapping[str, str] | None) -- HTTP headers sent on every request, for gateways that route or bill on a header or for anthropic-beta flags. A header given here replaces the SDK default of the same name.

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

  • extra_body (Mapping[str, Any] | None) -- JSON fields merged into every request body last, so they add an option the adapter does not model and may override one it does.

备注

Client-side tool search works here too: ToolSearchExtension ranks the deferred tools locally, and this adapter declares them with defer_loading and answers the search with tool_reference blocks.

备注

Signed replay blocks belong to the originating provider and model. Persist them unchanged for tool round trips; do not render them as user-facing reasoning. Await aclose() to release client resources.

示例

Stream an Anthropic model through the provider-neutral client:

model = AnthropicProvider(
    model=os.environ["ANTHROPIC_MODEL"],
    api_key=os.environ["ANTHROPIC_API_KEY"],
)
try:
    client = await create_agent(model)
    reply = await client.run("Review the proposed API")
finally:
    await model.aclose()

警告

Signed thinking and tool replay blocks must be returned unchanged only to the same provider and model. They are not interchangeable with plain reasoning text.

参见

AssistantMessage documents replay_blocks; 模型看到什么 explains replay during tool round trips.

native_deferred_tools: bool = True#

See zett_agent.model.NativeDeferredToolsModel.native_deferred_tools. The Messages API accepts defer_loading tools and loads one when a tool_reference names it, so every deferred definition travels with the request.

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.

async aclose()[源代码]#

Close the owned SDK connection pool.

返回类型:

None

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.

示例

Use a model or tool from a plain function:

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

runtime (SyncRuntime | None)

返回类型:

SyncObject[Self]