anthropic

package
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Nov 4, 2026 License: MIT Imports: 28 Imported by: 0

Documentation

Overview

Package anthropic is an agent.Backend that calls the Anthropic Messages API directly: a model call loop over a run directory with read-only file tools (Read, Glob, Grep) confined to that directory, whose final answer is the document the model submits through a StructuredOutput tool.

It talks to the API over net/http, without the official SDK: the loop needs one endpoint, its retries must count against the run's wall clock, and a dependency here would ship in the published module.

Index

Constants

View Source
const APIVersion = "2023-06-01"

APIVersion is the anthropic-version header every request sends.

View Source
const DefaultBaseURL = "https://api.anthropic.com"

DefaultBaseURL is the Anthropic API.

View Source
const DefaultEffort = "medium"

DefaultEffort is the effort sent with DefaultModel. Thinking cannot be turned off on that model; effort is its only depth control, and medium is the model's own default, sent explicitly so a change of default does not change what a run costs.

View Source
const DefaultModel = "claude-opus-5-5"

DefaultModel is the model a Backend with no Model uses.

Variables

View Source
var ErrNoKey = errors.New("ANTHROPIC_API_KEY is not set; export your Anthropic API key to use the anthropic backend")

ErrNoKey is Check's failure for a Backend without an API key.

Functions

This section is empty.

Types

type Backend

type Backend struct {
	// Model defaults to DefaultModel.
	Model string
	// APIKey and BaseURL are used when the request has no Endpoint. BaseURL
	// defaults to DefaultBaseURL.
	APIKey, BaseURL string
	// Effort is output_config.effort. Empty sends DefaultEffort with
	// DefaultModel and nothing with any other model, which then runs at its
	// own default.
	Effort string
	// HTTPClient defaults to a client over the default transport (so the
	// proxy variables apply) that never follows a redirect.
	HTTPClient *http.Client
	// contains filtered or unexported fields
}

Backend runs the Messages API over a request's directory.

The key comes from the request's Endpoint, else APIKey; the local host sets APIKey from ANTHROPIC_API_KEY. A Backend is safe for concurrent use.

func (*Backend) Check

func (b *Backend) Check(context.Context) (string, error)

Check reports a Backend that cannot run: one without an API key. It makes no request, so it neither spends nor proves that the key is accepted.

func (*Backend) Complete

func (b *Backend) Complete(ctx context.Context, request agent.CompleteRequest) (completion agent.Completion, err error)

Complete makes one Messages call that offers only the StructuredOutput tool, whose input schema is the request's (agent.Completer). There are no file tools, no nudge and no repair: a reply without a valid StructuredOutput call fails, with the usage it was billed. The current models refuse a forced tool_choice while they think, so the choice is auto, limited to one call, and the system prompt asks for the call. A rate limit, an overload or a server error before the API answered is retried as in Run.

func (*Backend) ConcurrentCalls

func (*Backend) ConcurrentCalls() bool

ConcurrentCalls reports that calls may run over one run directory at once (agent.ConcurrentCaller): a call is HTTP requests and read-only tools, and writes nothing.

func (*Backend) Name

func (*Backend) Name() string

func (*Backend) Run

func (b *Backend) Run(ctx context.Context, request agent.Request) (result agent.Result, err error)

Run implements agent.Backend. Every response is one call and is charged to the request's budgets; a call the budgets cannot pay for is not made.

type Category

type Category string

Category names why a call failed. Each is a fixed word, recorded as the status of the run's error event, so a host may report it where the message may not be reported.

const (
	// CategoryAuthentication is a missing or rejected key (HTTP 401 or 403).
	CategoryAuthentication Category = "authentication_error"
	// CategoryRateLimit is HTTP 429 after the retries.
	CategoryRateLimit Category = "rate_limit"
	// CategoryOverloaded is HTTP 529 after the retries.
	CategoryOverloaded Category = "provider_overloaded"
	// CategoryServer is another 5xx (or 408) after the retries.
	CategoryServer Category = "provider_error"
	// CategoryTransport is a connection that failed after the retries.
	CategoryTransport Category = "transport_error"
	// CategoryInvalidRequest is a request the API refused (400, 404, 413 and
	// the other 4xx statuses), such as an unknown model.
	CategoryInvalidRequest Category = "invalid_request"
	// CategoryMalformed is a response that does not follow the Messages API:
	// not JSON, too large, without usage or with an unknown stop reason.
	CategoryMalformed Category = "malformed_response"
	// CategoryRefusal is a response the model's safeguards declined.
	CategoryRefusal Category = "refusal"
	// CategoryContextWindow is a conversation the model could not continue.
	CategoryContextWindow Category = "context_window_exceeded"
	// CategoryNoDocument is a run that ended without submitting.
	CategoryNoDocument Category = "no_document"
)

type Error

type Error struct {
	Category Category
	// StatusCode is the HTTP status, zero when there was none.
	StatusCode int
	// Attempts is how many times the call was sent.
	Attempts int
	Detail   string
}

Error is a named failure of the backend. Its text never holds the API key or text the model wrote; the API's own message is kept, bounded, only for a request it refused.

func (*Error) Error

func (e *Error) Error() string

Directories

Path Synopsis
Package anthropictest serves Messages API responses as the event streams the anthropic backend reads, for stub servers in tests.
Package anthropictest serves Messages API responses as the event streams the anthropic backend reads, for stub servers in tests.

Jump to

Keyboard shortcuts

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