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

anthropic

package module
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Sep 11, 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.
  • 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.
  • 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-4-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 and does not retry.

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 compose here); it overrides BaseURL, APIKey, and MaxRetries.

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). 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