anthropic

package module
v0.21.0 Latest Latest
Warning

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

Go to latest
Published: Sep 12, 2026 License: Apache-2.0 Imports: 18 Imported by: 0

Documentation

Overview

Package anthropic implements the Anthropic Messages wire protocol reused by native and compatible provider endpoints inside the models module.

Constructors:

  • NewMessages — native /v1/messages. Full Claude surface: extended thinking blocks, tool_use with signature continuity, citations, fine-grained tool-result content blocks, cache_control.

Provider packages exposing an Anthropic-compatible endpoint reuse the Messages protocol through NewCompatibleMessages and select one typed Dialect. Application code continues to use the provider's own chat type.

Messages.CountInputTokens exposes the provider-specific complete-request token endpoint. NewTextCounter serves isolated text workflows.

Anthropic's Message Batches API (~50% pricing, up to 24h asynchronous) doesn't fit core/chat's synchronous request/response shape and is not exposed.

Model id constants aren't exported — anthropic-sdk-go owns them (anthropicsdk.ModelClaudeOpus5, anthropicsdk.ModelClaudeSonnet5, anthropicsdk.ModelClaudeFable5, etc.). Import the SDK directly when you need them.

Index

Constants

View Source
const (
	// ResponseExtensionKey preserves the complete official Anthropic response.
	ResponseExtensionKey = "anthropic/response"
	// StreamEventExtensionKey preserves each official Anthropic stream event.
	StreamEventExtensionKey = "anthropic/stream_event"
)
View Source
const (
	// RequestExtensionKey stores provider-owned top-level Messages API fields in
	// a Core request for fields without a provider-neutral equivalent.
	RequestExtensionKey = "anthropic/request"
)

Variables

This section is empty.

Functions

func NewRedactedThinkingPart

func NewRedactedThinkingPart(data []byte) (corechat.Part, error)

NewRedactedThinkingPart preserves an opaque Anthropic thinking block without pretending its content is readable text.

func NewThinkingPart

func NewThinkingPart(text string, signature []byte) (corechat.Part, error)

NewThinkingPart preserves the signature Anthropic requires when thinking is replayed in a later request.

Types

type Dialect

type Dialect struct {
	// Provider scopes opaque reasoning state to the API that issued it. It is
	// required even when the provider uses Anthropic's wire shape because signed
	// thinking is not portable across vendors.
	Provider       string
	MaxTemperature float64
	RejectTopK     bool
	RejectTopP     bool
	// NativeJSONSchema reports whether this endpoint implements Anthropic's
	// output_config.format JSON Schema control. Compatible providers that do not
	// declare it receive the shared prompt fallback.
	NativeJSONSchema bool
}

Dialect declares the protocol differences selected by an Anthropic-compatible provider adapter.

type Messages added in v0.13.0

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

Messages implements Anthropic's Messages protocol and is also the reusable protocol base for provider packages exposing a compatible endpoint.

func NewCompatibleMessages added in v0.13.0

func NewCompatibleMessages(_ context.Context, config MessagesConfig, dialect Dialect) (*Messages, error)

NewCompatibleMessages rejects an invalid compatible binding before the first call.

func NewMessages added in v0.13.0

func NewMessages(_ context.Context, config MessagesConfig) (*Messages, error)

NewMessages rejects an invalid provider binding before the first Messages call.

func (*Messages) Call added in v0.13.0

func (m *Messages) Call(ctx context.Context, req *corechat.Request) (*corechat.Response, error)

func (*Messages) CountInputTokens added in v0.13.0

func (m *Messages) CountInputTokens(ctx context.Context, req *corechat.Request) (int64, error)

CountInputTokens calls the provider's Messages token-count endpoint with the same provider request projection used by Call.

func (*Messages) Stream added in v0.13.0

Stream performs one streaming Messages API request and yields provider deltas without accumulating text or tool arguments across events.

type MessagesConfig added in v0.13.0

type MessagesConfig struct {
	APIKey         string
	DefaultOptions corechat.Options
	BaseURL        string
	HTTPClient     *http.Client
	Headers        http.Header
}

MessagesConfig configures an Anthropic Messages adapter. Defaults are copied during construction; callers may select the model per request.

func (MessagesConfig) Validate added in v0.13.0

func (m MessagesConfig) Validate() error

type ReasoningBlockKind

type ReasoningBlockKind string

ReasoningBlockKind identifies the official Anthropic reasoning block carried by a Core reasoning part.

const (
	ReasoningBlockThinking ReasoningBlockKind = protocolReasoningThinking
	ReasoningBlockRedacted ReasoningBlockKind = protocolReasoningRedacted
)

The vocabulary is closed because these kinds classify reasoning blocks the service returns. A redacted block carries no readable text but must still round trip intact for a later turn to remain valid, so it cannot be folded into the thinking kind.

func ReasoningBlockKindOf

func ReasoningBlockKindOf(part corechat.Part) (ReasoningBlockKind, bool, error)

ReasoningBlockKindOf reports whether part contains Anthropic-issued reasoning replay state and, when it does, which official block variant it is.

type TextCounter added in v0.21.0

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

TextCounter reports input-token counts via Anthropic's /messages/count_tokens endpoint. Implements tokenizer.TextCounter so it drops into code paths already gating on token budgets (RAG chunking, prompt-window checks, cost preflight).

Every count is a network round-trip; for high-QPS counting reach for an offline tokenizer instead.

func NewTextCounter added in v0.21.0

func NewTextCounter(_ context.Context, config TextCounterConfig) (*TextCounter, error)

NewTextCounter rejects an invalid provider/model binding before counting begins.

func (*TextCounter) CountText added in v0.21.0

func (t *TextCounter) CountText(ctx context.Context, text string) (int, error)

CountText returns the prompt-token count Anthropic would charge if text were sent as a single user message under the configured model.

type TextCounterConfig added in v0.21.0

type TextCounterConfig struct {
	APIKey     string
	Model      string
	BaseURL    string
	HTTPClient *http.Client
}

TextCounterConfig configures an Anthropic-backed token counter. Model picks the tokenizer vocabulary Anthropic counts against; a mismatch (Claude 3 model name vs Claude 4 vocab) produces wrong counts.

func (TextCounterConfig) Validate added in v0.21.0

func (t TextCounterConfig) Validate() error

Jump to

Keyboard shortcuts

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