anthropic

package
v0.2.0 Latest Latest
Warning

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

Go to latest
Published: Nov 22, 2026 License: MIT Imports: 19 Imported by: 1 module, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package anthropic implements canonical-trajectory continuation through the Anthropic Messages API.

Anthropic is the one major provider whose language API is not a dialect of Chat Completions, so it gets an adapter rather than a profile. Three of its differences are load-bearing here and are handled explicitly rather than discovered as a 400 in production:

  • A turn must end with a user message. Assistant prefill is rejected on current models, so a continuation whose trajectory ends in model output has to be given something to answer.
  • Every tool_use block must be answered by a tool_result in the very next user message. A trajectory can hold a call whose result never arrived; the protocol cannot, so such a call is retold as text.
  • Thinking blocks carry a signature that must be replayed byte for byte. That is what the retained provider state is for.

The same wire format is served by Anthropic-compatible endpoints, so the base URL is configurable and the adapter does not assume it is talking to Anthropic itself.

Index

Constants

View Source
const (
	// DefaultBaseURL is the Anthropic API root.
	DefaultBaseURL = "https://api.anthropic.com/v1"
	// DefaultVersion is the API version header value. It is a date rather
	// than a semantic version and has been stable across every model release
	// this adapter targets.
	DefaultVersion = "2023-06-01"
	// ProviderStateType identifies a retained assistant content array,
	// including thinking blocks and their signatures, wrapped with the model
	// that produced it.
	ProviderStateType = "anthropic.messages.assistant-content.v1"
)

Variables

This section is empty.

Functions

This section is empty.

Types

type Adapter

type Adapter struct {
	// contains filtered or unexported fields
}

Adapter streams Messages API output and preserves signed thinking blocks so a later continuation can replay them.

func New

func New(config Config) (*Adapter, error)

New validates configuration and creates an adapter.

func (*Adapter) Continue

func (adapter *Adapter) Continue(
	ctx context.Context, request continuation.Request, emit continuation.Emit,
) (continuation.Completion, error)

Continue implements continuation.Provider over the streaming Messages API.

func (*Adapter) Descriptor

func (adapter *Adapter) Descriptor() continuation.Descriptor

Descriptor returns the immutable provider configuration.

type Config

type Config struct {
	APIKey  string
	Model   string
	BaseURL string
	// Version is the anthropic-version header. Empty selects DefaultVersion.
	Version string
	// Provider names the serving stack in descriptors. Empty selects
	// "anthropic". A compatible gateway should name itself.
	Provider      string
	Phase         trajectory.Phase
	Effort        continuation.Effort
	ToolAuthority continuation.ToolAuthority
	// SpeechAuthority declares whether this provider's output may be voiced.
	SpeechAuthority continuation.SpeechAuthority
	// AllowTools is a compatibility alias for ToolAuthorityExecute.
	AllowTools bool
	// Vision declares that images may be sent. Every current Claude model
	// accepts them, so this defaults to true and exists for a gateway that
	// serves something that does not.
	Vision *bool
	// Thinking selects the extended-thinking form. Empty selects adaptive.
	Thinking Thinking
	// ThinkingBudgetTokens is the budget for ThinkingBudget. It must be
	// smaller than the output limit and at least 1024.
	ThinkingBudgetTokens int
	// IncludeThoughts asks for a readable summary of the reasoning. Without
	// it, current models stream thinking blocks with empty text, which is not
	// a bug but does mean the trajectory records no reasoning.
	IncludeThoughts bool
	// EffortNames maps portable effort onto Anthropic's effort vocabulary. A
	// nil map sends no effort at all, which is correct for models that predate
	// the parameter. An effort with no entry is refused at construction.
	EffortNames map[continuation.Effort]string
	// Betas are anthropic-beta header values.
	Betas []string
	// Headers are extra request headers.
	Headers map[string]string
	// Temperature is omitted when nil. Zero is useful for a deterministic
	// voice and must therefore remain distinguishable from omission.
	Temperature    *float64
	HTTPClient     *http.Client
	RequestTimeout time.Duration
}

Config configures one Anthropic fast or slow continuation profile.

type Thinking

type Thinking string

Thinking selects how extended thinking is requested.

The right value depends on the model generation, not on the deployment's preference, and getting it wrong is a hard error rather than a degraded answer: current models reject a token budget, and models that predate adaptive thinking reject the adaptive form. So it is declared.

const (
	// ThinkingAdaptive lets the model decide how much to think. It is the
	// mode for Claude 4.6 and newer.
	ThinkingAdaptive Thinking = "adaptive"
	// ThinkingDisabled explicitly turns thinking off, which is what a voice
	// provider wants: the fast phase answers, it does not deliberate.
	ThinkingDisabled Thinking = "disabled"
	// ThinkingBudget requests a fixed token budget, which is how models older
	// than Claude 4.6 spell extended thinking.
	ThinkingBudget Thinking = "budget"
	// ThinkingOmitted sends no thinking field at all.
	ThinkingOmitted Thinking = "omitted"
)

Jump to

Keyboard shortcuts

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