# provider/anthropic

> Package anthropic implements the Anthropic Messages provider port.

<!-- Generated from the Go source of github.com/barevoidco/zelk/core/provider/anthropic (cmd/zelk/internal/reference TestReferenceGoAPI). Don't edit: change the code, then run make reference. -->

```go
import "github.com/barevoidco/zelk/core/provider/anthropic"
```

Package anthropic implements the Anthropic Messages provider port. It translates wire formats without changing zelk's shared harness behavior. Open tool schemas retain their server contract with non-strict rendering; canonical tools retain the adapter's existing schema dialect.


## Client

```go
type Client struct {
	// contains filtered or unexported fields
}
```

Client calls Anthropic Messages. It is safe for concurrent use.


### New

```go
func New(baseURL string, opts ...Option) (*Client, error)
```

New creates a client for baseURL, normally [https://api.anthropic.com/v1](https://api.anthropic.com/v1). It performs no I/O and reads no process configuration.

**Example**

```go
c, err := anthropic.New("https://api.anthropic.com/v1", anthropic.WithAPIKey("host-supplied-key"))
if err != nil {
	fmt.Println(err)
	return
}
defer c.Close()
fmt.Println(c.Family())
// Output: anthropic
```

Output:

```text
anthropic
```


### Client.Capabilities

```go
func (c *Client) Capabilities(ctx context.Context, model string) (provider.Caps, error)
```

Capabilities returns the injected capability snapshot without network I/O.


### Client.Close

```go
func (c *Client) Close() error
```

Close releases idle connections owned by this client. Injected clients are left to their owners; active calls retain their original contexts.


### Client.Family

```go
func (c *Client) Family() history.Family
```

Family identifies the Anthropic Messages wire format.


### Client.Models

```go
func (c *Client) Models(ctx context.Context) ([]provider.Model, error)
```

Models returns the models declared with WithModels.


### Client.Reservation

```go
func (c *Client) Reservation(req *provider.Request, caps provider.Caps) event.Tokens
```

Reservation conservatively bounds the exact rendered body when the host has recorded a byte input bound. Otherwise it reserves the whole context window.


### Client.SearchBackend

```go
func (c *Client) SearchBackend(model string) (*Search, error)
```

SearchBackend returns a backend using model and this client's credentials. The caller must keep the client open until all searches finish. The endpoint and model must support web\_search\_20250305 with thinking disabled; no request is made here.

**Example**

```go
c, err := anthropic.New("https://api.anthropic.com/v1", anthropic.WithAPIKey("host-supplied-key"))
if err != nil {
	panic(err)
}
defer c.Close()
backend, err := c.SearchBackend("claude-haiku-5-5")
if err != nil {
	panic(err)
}
// Inject backend with zelk.WithSearch; this example makes no requests.
fmt.Printf("%T\n", backend)
// Output: *anthropic.Search
```

Output:

```text
*anthropic.Search
```


### Client.Stream

```go
func (c *Client) Stream(ctx context.Context, req *provider.Request) (provider.Stream, error)
```

Stream prepares a lazy single-use stream. Iteration owns and closes its HTTP response; breaking iteration or cancelling ctx stops the call.


## Option

```go
type Option func(*Client)
```

Option configures a Client.


### WithAPIKey

```go
func WithAPIKey(key string) Option
```

WithAPIKey sets the credential sent only to the provider.


### WithCache

```go
func WithCache(enabled bool) Option
```

WithCache enables or disables explicit and automatic prompt cache breakpoints. Caching is enabled by default.


### WithCapabilities

```go
func WithCapabilities(caps provider.Caps) Option
```

WithCapabilities sets the model capabilities supplied by the host.


### WithHTTPClient

```go
func WithHTTPClient(hc *http.Client) Option
```

WithHTTPClient injects the caller-owned HTTP client. It must be safe for concurrent use.


### WithModels

```go
func WithModels(models ...provider.Model) Option
```

WithModels declares the models the client offers, each with its own capabilities, for Capabilities and WithModels to report. Capabilities remain those of any model not declared.


### WithStreamIdleTimeout

```go
func WithStreamIdleTimeout(d time.Duration) Option
```

WithStreamIdleTimeout sets the maximum interval without response bytes on a stream, including with an injected HTTP client. It must be positive and defaults to five minutes. Expiry closes the body and returns ErrorTransient.

**Example**

```go
client, err := anthropic.New("https://example.test/v1",
	anthropic.WithStreamIdleTimeout(2*time.Minute))
if err != nil {
	panic(err)
}
defer client.Close()
fmt.Println("client configured")
// Output: client configured
```

Output:

```text
client configured
```


### WithToolLoading

```go
func WithToolLoading(loads func(model string) bool) Option
```

WithToolLoading says which models load deferred tools natively, through tool\_reference blocks (docs/design/tools.md, deferred tools): Anthropic documents it for Claude Haiku 4.5, Sonnet 4.5, Opus 4.5 and later. A gateway may reject the fields, so a host sets it only for an endpoint known to accept them. A model declared with DeferredTools loads them regardless.


## Search

```go
type Search struct {
	// contains filtered or unexported fields
}
```

Search is a native Messages web search backend. It borrows its client's transport and credentials, and is safe for concurrent use.


### Search.Limit

```go
func (s *Search) Limit() event.Tokens
```

Limit bounds the entire native request, including server-supplied search context. Unknown model limits are zero and cannot support hard token budgets.

**Example**

```go
c, err := anthropic.New("https://unused.test/v1", anthropic.WithCapabilities(provider.Caps{ContextWindow: 200000}))
if err != nil {
	panic(err)
}
defer c.Close()
backend, err := c.SearchBackend("fast")
if err != nil {
	panic(err)
}
limit := backend.Limit()
fmt.Println(limit.Input, limit.Output)
// Output: 200000 2048
```

Output:

```text
200000 2048
```


### Search.Search

```go
func (s *Search) Search(ctx context.Context, q search.Query) (search.Response, error)
```

Search makes one bounded request with at most one native search. When both domain lists are set, allowed domains go to the server and blocked domains are applied locally, because Messages rejects both lists in one request.
