Help us shape the future of Go! Take the 2026 Go Developer Survey

providers

package
v0.2.0 Latest Latest
Warning

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

Go to latest
Published: Sep 25, 2026 License: MIT Imports: 9 Imported by: 0

Documentation

Overview

Package providers holds the dependency-free helpers every provider adapter shares: transport and HTTP error classification, a bounded body read, tool-use id synthesis, and price table helpers. It imports nothing outside the standard library and the runtime, so a provider SDK lives in its own module and the core stays untainted.

The classification contract matches what the runtime's accounting caller expects. A timeout is returned bare, because the request may have been received and charged and the caller must record it as ambiguous; every other failure is either a TransientError, which the caller retries, or a permanent error, which ends the run.

Index

Constants

View Source
const MaxBodyBytes = 16 << 20

MaxBodyBytes bounds how much of a provider response is read.

Variables

View Source
var ErrNoPrice = errors.New("no price for this model")

ErrNoPrice is wrapped by PriceFor for a name the table does not hold. A paid model runs only with a known price.

Functions

func ClassifyAPIError

func ClassifyAPIError(status int, err error) error

ClassifyAPIError classifies an SDK error that yields only a status code and the error itself. A status of zero means the SDK reported no HTTP response, so the error is a transport failure.

func ClassifyStatus

func ClassifyStatus(provider string, status int, body []byte) error

ClassifyStatus converts a provider response into an error, or nil for a 2xx. A retryable status yields a TransientError wrapping a StatusError; any other non-2xx yields the StatusError itself, which is permanent.

func ClassifyTransport

func ClassifyTransport(err error) error

ClassifyTransport maps a failed HTTP round trip onto the runtime's classes. A timeout is returned bare so the accounting caller charges it as ambiguous; any other transport error is transient.

func Free

func Free(names ...string) agentrt.PriceTable

Free records each name as costing nothing, which is what a local model costs. The zero prices make no difference to accounting; the entries make the difference to PriceFor.

func Merge

func Merge(tables ...agentrt.PriceTable) agentrt.PriceTable

Merge combines price tables into one for agentrt.ModelConfig, so a consumer with a local and a paid model passes a single table. Later tables win.

func ParseDollars

func ParseDollars(s string) (agentrt.Micros, error)

ParseDollars converts a budget written as "5", "4.41", or "$0.50" into micros, rounding to the nearest micro. Negative amounts are refused.

func PriceFor

func PriceFor(prices agentrt.PriceTable, name string) (agentrt.Price, error)

PriceFor returns the price recorded for a model. A name absent from the table is an error rather than a price of zero: agentrt.PriceTable charges nothing for an unknown name, which is right for a local model and would silently defeat a budget for a paid one. Local models are recorded with Free so that "free" and "unpriced" stay distinguishable.

func ReadBody

func ReadBody(r io.Reader) ([]byte, error)

ReadBody reads at most MaxBodyBytes from r. A provider that answers with an unbounded stream cannot exhaust the process.

func SynthesizeToolUseID

func SynthesizeToolUseID(i int) string

SynthesizeToolUseID names the ith tool use of a response for providers that return no id. Tool-use ids only have to be unique within a request, and both consumers' adapters use this form.

func Transient

func Transient(status int) bool

Transient reports whether an HTTP status is worth retrying: 408 request timeout, 429 rate limited, and every 5xx, which includes Anthropic's 529 overloaded.

Types

type StatusError

type StatusError struct {
	Provider string
	Status   int
	Body     string
}

StatusError is a provider's non-2xx response. It carries the status so a caller can act on it without matching strings.

func (StatusError) Error

func (e StatusError) Error() string

Directories

Path Synopsis
anthropic module
ollama module
openai module

Jump to

Keyboard shortcuts

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