Documentation
¶
Overview ¶
Package claude puts Claude behind one call an Arandu application can make and a test can replace: a prompt goes in, text or a schema-checked JSON answer comes out, with what it cost.
It is a thin layer over the official Anthropic Go SDK. The SDK keeps what it already does well -- the HTTP client, retries on 408, 409, 429 and 5xx (529 included), typed errors -- and this package adds the three things an application otherwise writes by hand every time: a configuration that is refused at boot rather than on the first request, the answer read only after the stop reason says it is whole, and an interface so the code that calls Claude is tested without the network.
Index ¶
Constants ¶
const ( // MaxImageBytes is the largest image the API accepts, 5 MB. MaxImageBytes = 5 * 1024 * 1024 // MaxImages is the most images the API accepts in one request. MaxImages = 100 )
The limits the Messages API puts on images, checked here so a request that cannot work fails before it is sent.
const DefaultMaxTokens = 16000
DefaultMaxTokens bounds one answer when neither Config nor Request says otherwise. It is generous on purpose: an answer cut at the cap is an error here (ErrTruncated), and a retry costs more than the headroom.
const DefaultModel = "claude-opus-5-5"
DefaultModel is the model asked when Config leaves Model empty.
Variables ¶
var ( // ErrRefused: the model declined the request for policy reasons, and no // fallback served it. Refusal carries the category. ErrRefused = errors.New("claude: the model declined the request") // ErrTruncated: the answer reached MaxTokens and stopped mid-way. ErrTruncated = errors.New("claude: the answer reached the token cap") // ErrEmpty: the answer carried no text. ErrEmpty = errors.New("claude: the answer is empty") )
The ways an answer can fail after the API accepted the request.
var ErrBadImage = errors.New("claude: invalid image")
ErrBadImage is what an image that cannot be sent wraps.
var ErrScriptExhausted = errors.New("claude: the fake has no scripted answer left")
ErrScriptExhausted is what a Fake answers once its script has run out.
Functions ¶
This section is empty.
Types ¶
type Answer ¶
Answer is one scripted answer: the text, or the JSON for a structured request, or an error.
type Client ¶
type Client struct {
// contains filtered or unexported fields
}
Client calls the Anthropic API.
type Completer ¶
Completer is what calls Claude. *Client implements it against the API and *Fake in memory, so the code that depends on it is tested without either a key or the network.
type Config ¶
type Config struct {
// APIKey is the Anthropic API key. Required.
APIKey string
// Model is the model id; empty means DefaultModel.
Model string
// Effort is the default effort; empty means EffortHigh. A Request can
// ask for another.
Effort Effort
// MaxTokens is the default cap on one answer; zero means DefaultMaxTokens.
MaxTokens int64
// Timeout bounds one request, retries included; zero means two minutes.
Timeout time.Duration
// MaxRetries is how many times the SDK retries a retryable failure; a
// negative value means none, zero means the SDK's default of two.
MaxRetries int
// BaseURL replaces the API's address, for a proxy or a test server.
BaseURL string
// HTTPClient replaces the SDK's HTTP client.
HTTPClient *http.Client
// WorkspaceID names the workspace a key that is not scoped to one runs
// in (a user key: "sk-ant-usr-…"), sent as the anthropic-workspace-id
// header. A workspace-scoped key needs none.
WorkspaceID string
// NoFallback turns off the server-side fallback that re-serves a request
// the model declined for policy reasons on the model's default
// substitute. It is on by default.
NoFallback bool
}
Config is what the application passes when it builds the client. The key comes from the application's own environment; this package never reads the environment itself.
type Effort ¶
type Effort string
Effort is how much the model should put into an answer: "low", "medium", "high", "xhigh" or "max".
type Fake ¶
type Fake struct {
// contains filtered or unexported fields
}
Fake answers from a script, in memory, and records what it was asked. It is what an application's tests pass where production passes *Client.
fake := claude.NewFake(claude.Answer{JSON: `{"fit": 80}`})
svc := services.NewProposals(db, fake)
...
fake.Requests()[0].Prompt // what the service asked
fake.Requests()[0].Images // the pictures that came with it
type Image ¶ added in v0.3.0
type Image struct {
// MediaType is "image/jpeg", "image/png", "image/webp" or "image/gif".
// Empty means it is detected from Data. A declared type that does not
// match the bytes is refused, as the API would refuse it.
MediaType string
// Data is the raw bytes of the file, not base64. At most MaxImageBytes.
Data []byte
}
Image is a picture sent with the question.
type Request ¶
type Request struct {
// System is the instruction that frames every answer: who is answering,
// the rules, the template. It is sent as a cached system block, so a
// long and stable System costs less from the second request on.
System string
// Prompt is the question itself. Required.
Prompt string
// Images are pictures the question is about, sent before the Prompt in
// the same message. Optional; at most MaxImages, each valid per Image.
Images []Image
// Schema, when set, is the JSON schema the answer must satisfy; the
// answer then arrives in Result.JSON. Objects in it need
// "additionalProperties": false.
Schema map[string]any
// Effort and MaxTokens override the client's defaults for this request.
Effort Effort
MaxTokens int64
}
Request is one question.
type Result ¶
type Result struct {
// Text is the answer's text. For a structured request it is the JSON.
Text string
// JSON is the answer for a request with a Schema; empty otherwise.
JSON json.RawMessage
// Model is the model that answered, which can differ from the one asked
// when the server-side fallback served the request.
Model string
// ID is the message id, for correlating with the Anthropic console.
ID string
Usage Usage
}
Result is a whole answer.