anthropic

package
v0.13.0 Latest Latest
Warning

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

Go to latest
Published: Nov 21, 2026 License: MIT Imports: 7 Imported by: 0 modules, 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 provides a minimal Claude Messages API client.

Index

Constants

View Source
const (

	// ModelHaiku is the default model — fast and cheap (~$0.001/command).
	ModelHaiku = "claude-haiku-4-5-20251001"
)

Variables

This section is empty.

Functions

This section is empty.

Types

type APIError

type APIError struct {
	StatusCode int
	Type       string `json:"type"`
	ErrorBody  struct {
		Type    string `json:"type"`
		Message string `json:"message"`
	} `json:"error"`
}

APIError is returned when the API responds with a non-2xx status.

func (*APIError) Error

func (e *APIError) Error() string

type Client

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

Client is a minimal Anthropic Messages API client.

func New

func New(apiKey string, opts ...Option) *Client

New creates a new Anthropic client.

func (*Client) CreateMessage

func (c *Client) CreateMessage(ctx context.Context, system string, messages []Message, maxTokens int) (*Response, error)

CreateMessage sends a message to the Claude API and returns the response.

type ContentBlock

type ContentBlock struct {
	Type string `json:"type"`
	Text string `json:"text,omitempty"`
}

ContentBlock is a single block in the response.

type Message

type Message struct {
	Role    string `json:"role"`
	Content string `json:"content"`
}

Message is a single message in a conversation.

type Option

type Option func(*Client)

Option configures a Client.

func WithBaseURL

func WithBaseURL(url string) Option

WithBaseURL overrides the API base URL (useful for testing).

func WithHTTPClient

func WithHTTPClient(hc *http.Client) Option

WithHTTPClient overrides the default HTTP client.

func WithModel

func WithModel(model string) Option

WithModel overrides the default model.

type Request

type Request struct {
	Model     string    `json:"model"`
	MaxTokens int       `json:"max_tokens"`
	System    string    `json:"system,omitempty"`
	Messages  []Message `json:"messages"`
}

Request is the Messages API request body.

type Response

type Response struct {
	ID           string         `json:"id"`
	Type         string         `json:"type"`
	Role         string         `json:"role"`
	Content      []ContentBlock `json:"content"`
	Model        string         `json:"model"`
	StopReason   string         `json:"stop_reason"`
	StopSequence *string        `json:"stop_sequence"`
	Usage        Usage          `json:"usage"`
}

Response is the Messages API response body.

func (*Response) Text

func (r *Response) Text() string

Text returns the concatenated text content from the response.

type Usage

type Usage struct {
	InputTokens  int `json:"input_tokens"`
	OutputTokens int `json:"output_tokens"`
}

Usage contains token usage information.

Jump to

Keyboard shortcuts

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