Documentation
¶
Overview ¶
Package anthropic is the weft adapter for the Anthropic Messages API, wrapping the official anthropic-sdk-go. weft never owns the HTTP: every request goes through the SDK's client, and the SDK's stream type is the contract.
Mapping highlights (ADR 0013 has the full tables):
- weft's batched RoleTool message is already Anthropic's shape: one user message with N tool_result blocks.
- Streamed input_json_delta argument fragments surface live as ModelToolCallDelta progress; the assembled call is emitted whole before ModelFinish.
- Empty content the API rejects never reaches the wire: an empty tool result travels as "(empty tool output)", a user message with no sendable parts as "(empty message)", and an assistant message with nothing sendable is skipped.
- Reasoning round-trips as thinking blocks with their signature; an unsigned ReasoningPart (a transcript from another provider) is dropped rather than failing the call.
- SequentialTools sets tool_choice.auto with disable_parallel_tool_use.
- ModelRequest.Thinking overrides the Thinking(true) construction default: Off disables explicitly, a Budget pins budget_tokens, a bare level sends adaptive (ADR 0013 amendment 9).
- max_tokens is required by the API; the adapter defaults it to 4096 when MaxTokens is not given.
Retry stance: transport retries (429, 5xx, net.Error) belong to the SDK via MaxRetries; the weft loop never retries a model call, and logic retries are model-seam middleware.
Vertex/Bedrock clients compose through Client(c).
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Model ¶
Model returns a weft.Model backed by the Anthropic Messages API. A Model is immutable and safe for concurrent runs; tool definitions are converted once per *ToolDef and cached by pointer.
Example ¶
Constructing a model performs no I/O; a key ($ANTHROPIC_API_KEY) is read only when a run starts. Swap providers by swapping this one line.
package main
import (
"context"
"github.com/weftgo/weft"
"github.com/weftgo/weft/anthropic"
)
func main() {
m := anthropic.Model("claude-sonnet-5", anthropic.Thinking(true))
echo := weft.Tool("echo", "Echo a message.", func(ctx context.Context, in struct {
Message string `json:"message" jsonschema:"the text to repeat"`
}) (string, error) {
return in.Message, nil
})
agt := weft.New(m, echo)
_ = agt // a run is agt.Generate(ctx, weft.Prompt("Echo: hello"))
}
Output:
Types ¶
type Option ¶
type Option interface {
// contains filtered or unexported methods
}
Option configures the adapter at construction, the same functional style as the core. The zero configuration reads $ANTHROPIC_API_KEY; the SDK's transport default applies (2 retries on 429/5xx/connection errors).
func BaseURL ¶
BaseURL points the adapter at a custom endpoint (a gateway, a proxy). Also read from $ANTHROPIC_BASE_URL when the option is not given.
func Client ¶
Client uses an already-configured SDK client (Vertex AI and Bedrock clients, test doubles); it overrides BaseURL, APIKey, and MaxRetries. The WEFT_MODEL_REQUESTS kill switch guards egress from clients the adapter builds from credentials; an injected client's destinations are the caller's responsibility — which is why it stays reachable under deny (ADR 0013's kill-switch clause).
func IdleTimeout ¶
IdleTimeout is the maximum gap between two stream events before the call fails wrapping weft.ErrStreamIdle (default 60s; zero disables it). The ctx deadline stays the hard limit on the whole call — a slow but actively streaming response is never killed.
func MaxRetries ¶
MaxRetries forwards to the SDK's transport retry configuration (429/5xx/connection errors only). Only n > 0 is forwarded: the SDK's own default (2) applies otherwise, and 0 cannot disable it — keep a zero-retry client via Client(c) if you need one. The weft loop never retries a model call; logic retries are model-seam middleware (TODO §4.1).
func MaxTokens ¶
MaxTokens sets the per-step output-token limit. Anthropic requires the field; the adapter defaults it to 4096 when unset.
func Temperature ¶
Temperature sets the sampling temperature; it is not sent unless the option is given.