Help us shape the future of Go! Take the 2026 Go Developer Survey

anthropic

package module
v0.2.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Sep 19, 2026 License: MIT Imports: 13 Imported by: 0

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

func Model(name string, opts ...Option) weft.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"))
}

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 APIKey

func APIKey(k string) Option

APIKey sets the API key. Default: $ANTHROPIC_API_KEY.

func BaseURL

func BaseURL(u string) Option

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

func Client(c *anthropic.Client) Option

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

func IdleTimeout(d time.Duration) Option

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

func MaxRetries(n int) Option

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

func MaxTokens(n int) Option

MaxTokens sets the per-step output-token limit. Anthropic requires the field; the adapter defaults it to 4096 when unset.

func Temperature

func Temperature(t float64) Option

Temperature sets the sampling temperature; it is not sent unless the option is given.

func Thinking

func Thinking(on bool) Option

Thinking enables adaptive thinking: the request carries thinking:{"type":"adaptive"} and the stream surfaces thinking blocks (with their signatures) as reasoning. Without the option nothing is sent — the vendor default for the model applies.

Directories

Path Synopsis
Command example runs a two-step agent conversation against the real Anthropic API.
Command example runs a two-step agent conversation against the real Anthropic API.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL