anthropic

package
v0.1.97 Latest Latest
Warning

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

Go to latest
Published: Aug 30, 2026 License: Apache-2.0 Imports: 8 Imported by: 0

Documentation

Overview

Package anthropic implements the Anthropic Messages API client (design doc §7.1, issue #69).

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type ChatMessage

type ChatMessage struct {
	Role    string
	Content string
	Blocks  []ContentBlock
}

ChatMessage is one user or assistant turn (roles user|assistant only). Plain text uses Content (JSON string). Tool turns set Blocks (JSON array).

func (ChatMessage) MarshalJSON

func (m ChatMessage) MarshalJSON() ([]byte, error)

MarshalJSON encodes Content as a string or Blocks as a content-block array.

type Client

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

Client calls POST /v1/messages.

func (*Client) Generate

func (c *Client) Generate(ctx context.Context, req Request) (Response, error)

Generate performs one non-streaming Messages request. System may be empty. Tools / tool_choice are omitted when unset. ToolCalls are populated only when StopReason is tool_use.

type ContentBlock

type ContentBlock struct {
	Type      string          `json:"type"`
	Text      string          `json:"text,omitempty"`
	ID        string          `json:"id,omitempty"`
	Name      string          `json:"name,omitempty"`
	Input     json.RawMessage `json:"input,omitempty"`
	ToolUseID string          `json:"tool_use_id,omitempty"`
	Content   string          `json:"content,omitempty"`
}

ContentBlock is a Messages API content block (text, tool_use, or tool_result).

func (ContentBlock) MarshalJSON

func (b ContentBlock) MarshalJSON() ([]byte, error)

MarshalJSON keeps tool_result.content present even when empty. Anthropic requires that field; omitempty would drop a successful empty tool output.

type Request

type Request struct {
	Model      string
	System     string
	Messages   []ChatMessage
	Tools      []Tool
	ToolChoice *ToolChoice
}

Request is one non-streaming Messages API call.

type Response

type Response struct {
	Text         string
	ToolCalls    []ToolUse
	StopReason   string
	InputTokens  int
	OutputTokens int
	DurationMs   int64
}

Response is a decoded Messages API result.

type Tool

type Tool struct {
	Name        string          `json:"name"`
	Description string          `json:"description,omitempty"`
	InputSchema json.RawMessage `json:"input_schema"`
}

Tool is an Anthropic custom tool (name + JSON Schema input_schema).

type ToolChoice

type ToolChoice struct {
	Type string `json:"type"`
}

ToolChoice is the Messages API tool_choice object (type auto|any|none).

type ToolUse

type ToolUse struct {
	ID    string
	Name  string
	Input json.RawMessage
}

ToolUse is one parsed tool_use content block.

Jump to

Keyboard shortcuts

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