Documentation
¶
Index ¶
- Constants
- Variables
- func CountMessage(model string, msg Message) (int, error)
- func CountMessagesAndTools(tc *TokenCount, req TokenCountRequest, encoding string, perMsgOverhead int, ...) error
- func CountMessagesAndToolsAnthropic(tc *TokenCount, req TokenCountRequest) error
- func CountText(model, text string) (int, error)
- func DefaultHttpClient() *http.Client
- func DeltaIndex(i int) *uint32
- func NewHttpClient(opts HttpClientOpts) *http.Client
- func NewRequestID() string
- type AssistantMsg
- type BoundToolSpec
- type CacheHint
- type Delta
- type DeltaType
- type EventStream
- func (s *EventStream) C() <-chan StreamEvent
- func (s *EventStream) Close()
- func (s *EventStream) Delta(d *Delta)
- func (s *EventStream) Done(usage *Usage)
- func (s *EventStream) Error(err *ProviderError)
- func (s *EventStream) Routed(r Routed)
- func (s *EventStream) Send(ev StreamEvent)
- func (s *EventStream) Start(opts StreamStartOpts)
- func (s *EventStream) ToolCall(tc ToolCall)
- type HttpClientOpts
- type Message
- type Messages
- func (m *Messages) AddAssistantMsg(content string, toolCalls ...ToolCall)
- func (m *Messages) AddSystemMsg(content string)
- func (m *Messages) AddToolCallResult(toolCallID, output string, isError bool)
- func (m *Messages) AddUserMsg(content string)
- func (m *Messages) Append(msg ...Message)
- func (m *Messages) UnmarshalJSON(data []byte) error
- type Model
- type ModelFetcher
- type Option
- type Options
- type ParsedToolCall
- type Provider
- type ProviderError
- func AsProviderError(provider string, err error) *ProviderError
- func NewErrAPIError(provider string, statusCode int, body string) *ProviderError
- func NewErrBuildRequest(provider string, cause error) *ProviderError
- func NewErrContextCancelled(provider string, cause error) *ProviderError
- func NewErrMissingAPIKey(provider string) *ProviderError
- func NewErrNoProviders(provider string) *ProviderError
- func NewErrProviderMsg(provider string, msg string) *ProviderError
- func NewErrRequestFailed(provider string, cause error) *ProviderError
- func NewErrStreamDecode(provider string, cause error) *ProviderError
- func NewErrStreamRead(provider string, cause error) *ProviderError
- func NewErrUnknownModel(provider string, modelID string) *ProviderError
- type ReasoningEffort
- type Resolver
- type Role
- type Routed
- type SortedMap
- type StopReason
- type StreamEvent
- type StreamEventType
- type StreamRequest
- type StreamResponse
- func (r *StreamResponse) DispatchAsync() *StreamResponse
- func (r *StreamResponse) HandleTool(handlers ...ToolHandler) *StreamResponse
- func (r *StreamResponse) OnReasoning(fn func(chunk string)) *StreamResponse
- func (r *StreamResponse) OnStart(fn func(*StreamStart)) *StreamResponse
- func (r *StreamResponse) OnText(fn func(chunk string)) *StreamResponse
- func (r *StreamResponse) OnToolDelta(fn func(d *Delta)) *StreamResponse
- func (r *StreamResponse) Result() <-chan *StreamResult
- func (r *StreamResponse) WithToolDispatcher(d ToolDispatcher) *StreamResponse
- type StreamResult
- type StreamStart
- type StreamStartOpts
- type Streamer
- type SystemMsg
- type TokenCount
- type TokenCountRequest
- type TokenCounter
- type ToolCall
- type ToolCallResult
- type ToolChoice
- type ToolChoiceAuto
- type ToolChoiceNone
- type ToolChoiceRequired
- type ToolChoiceTool
- type ToolDefinition
- type ToolDispatcher
- type ToolHandler
- type ToolSet
- type ToolSpec
- type TypedToolCall
- type Usage
- type UserMsg
Constants ¶
const ( ProviderNameAnthropic = "anthropic" ProviderNameClaude = "claude" ProviderNameBedrock = "bedrock" ProviderNameOllama = "ollama" ProviderNameOpenAI = "openai" ProviderNameOpenRouter = "openrouter" ProviderNameRouter = "router" )
Provider name constants used in ProviderError.Provider.
Variables ¶
var ( // ErrContextCancelled is returned when the caller's context is cancelled // while a stream is in progress. ErrContextCancelled = errors.New("context cancelled") // ErrRequestFailed is returned when the HTTP transport fails before a // response is received (e.g. network error, DNS failure). ErrRequestFailed = errors.New("request failed") // ErrAPIError is returned when the provider API responds with a non-2xx // HTTP status. The ProviderError carries StatusCode and Body. ErrAPIError = errors.New("API error") // ErrStreamRead is returned when reading or scanning the response stream // fails at the I/O level (e.g. scanner error, connection reset). ErrStreamRead = errors.New("stream read error") // ErrStreamDecode is returned when a stream chunk cannot be decoded // (e.g. malformed JSON in an SSE data line). ErrStreamDecode = errors.New("stream decode error") // ErrProviderError is returned when the provider sends an explicit // error inside the stream (e.g. Anthropic error event, OpenRouter // chunk-level error). ErrProviderError = errors.New("provider error") // ErrMissingAPIKey is returned when a provider requires an API key // but none has been configured. ErrMissingAPIKey = errors.New("missing API key") // ErrBuildRequest is returned when serialising the outgoing request // fails before it is sent. ErrBuildRequest = errors.New("build request error") // ErrUnknownModel is returned when a model ID or alias cannot be resolved. ErrUnknownModel = errors.New("unknown model") // ErrNoProviders is returned when no providers are configured or all // failover targets have been exhausted. ErrNoProviders = errors.New("no providers configured") // ErrUnknown is used to wrap any error that is not already a ProviderError. // Callers can test for it with errors.Is(err, llm.ErrUnknown). ErrUnknown = errors.New("unknown error") )
Sentinel errors for use with errors.Is. Each ProviderError wraps one of these so callers can inspect the error kind without string matching.
Functions ¶
func CountMessage ¶ added in v0.24.0
CountMessage returns the number of tokens for a single Message for the given model. The message is converted to its text representation using the same logic as CountTokens (role content + tool call names/args for AssistantMsg, output for ToolCallResult, etc.).
This is a convenience function for callers that count messages individually rather than as a batch — for example, per-entry token estimates in a conversation history manager.
func CountMessagesAndTools ¶ added in v0.24.0
func CountMessagesAndTools(tc *TokenCount, req TokenCountRequest, encoding string, perMsgOverhead int, replyPriming int) error
CountMessagesAndTools is a low-level helper for provider TokenCounter implementations. Library consumers should use the TokenCounter interface directly rather than calling this function.
It fills tc.PerMessage, tc.ToolsTokens, tc.PerTool, and tc.InputTokens using the given BPE encoding, then calls applyRoleBreakdown to populate the role breakdown fields.
Returns an error if req.Model is empty.
perMsgOverhead is added to InputTokens once per message (e.g. 4 for the OpenAI cookbook formula; 0 for approximation-only providers). replyPriming is a fixed addend for reply-priming tokens (e.g. 3 for OpenAI; 0 for others).
func CountMessagesAndToolsAnthropic ¶ added in v0.24.0
func CountMessagesAndToolsAnthropic(tc *TokenCount, req TokenCountRequest) error
CountMessagesAndToolsAnthropic is like CountMessagesAndTools but applies Anthropic-specific tool overhead constants: the hidden tool-use system preamble (~330 tokens, paid once) plus per-tool serialisation framing (~126 tokens first tool, ~85 tokens each additional). In total, a request with N tools adds 330+126+(N-1)×85 tokens on top of the raw JSON counts.
Use this for anthropic, bedrock, and claude providers.
func CountText ¶ added in v0.24.0
CountText returns the number of tokens in text for the given model. The encoding is selected automatically based on the model ID: o200k_base for GPT-4o/o-series, cl100k_base for everything else.
This is a convenience function for callers that need to count raw text without constructing a full TokenCountRequest — for example, context-budget managers that count individual history entries.
func DefaultHttpClient ¶ added in v0.23.0
DefaultHttpClient returns the shared default HTTP client. It is safe for concurrent use and is reused across all providers that do not supply their own client.
func DeltaIndex ¶ added in v0.23.0
DeltaIndex converts an int to a *uint32 for use as Delta.Index.
func NewHttpClient ¶ added in v0.23.0
func NewHttpClient(opts HttpClientOpts) *http.Client
NewHttpClient creates a new *http.Client with sensible defaults for LLM provider use. The client has no top-level Timeout — LLM streams can be arbitrarily long and are cancelled via context. Transport-level timeouts guard against stalled connections at the TCP/TLS layer.
When opts.Logger is non-nil, every request and response is logged at Debug level. Set opts.Debug = true to also include headers and bodies. Response bodies are tee-logged as they stream — no buffering, no broken SSE.
func NewRequestID ¶ added in v0.23.0
func NewRequestID() string
NewRequestID generates a unique correlation ID for a stream request. Uses a URL-safe nanoid with a length of 12 characters.
Types ¶
type AssistantMsg ¶ added in v0.5.0
AssistantMsg contains an assistant response, optionally with tool calls.
func (*AssistantMsg) MarshalJSON ¶ added in v0.5.0
func (m *AssistantMsg) MarshalJSON() ([]byte, error)
func (*AssistantMsg) Role ¶ added in v0.5.0
func (m *AssistantMsg) Role() Role
func (*AssistantMsg) Validate ¶ added in v0.5.0
func (m *AssistantMsg) Validate() error
type BoundToolSpec ¶ added in v0.23.0
type BoundToolSpec[In, Out any] struct { // contains filtered or unexported fields }
BoundToolSpec pairs a ToolSpec[In] with a handler function, satisfying both ToolHandler (for StreamResponse.HandleTool) and toolRegistration (for NewToolSet). Create one with the package-level Handle function.
func Handle ¶ added in v0.23.0
func Handle[In, Out any](spec *ToolSpec[In], fn func(ctx context.Context, in In) (*Out, error)) *BoundToolSpec[In, Out]
Handle binds a handler function to a ToolSpec, producing a *BoundToolSpec that satisfies both ToolHandler and toolRegistration.
Because Go methods cannot introduce new type parameters, this is a package-level generic function rather than a method on ToolSpec.
Example:
weatherSpec := llm.NewToolSpec[GetWeatherParams]("get_weather", "Get weather")
// Register with StreamResponse:
llm.Process(ctx, ch).
HandleTool(llm.Handle(weatherSpec, func(ctx context.Context, in GetWeatherParams) (*GetWeatherResult, error) {
return &GetWeatherResult{Temp: 22}, nil
}))
// Or pass directly to NewToolSet — BoundToolSpec satisfies toolRegistration too:
tools := llm.NewToolSet(
llm.Handle(weatherSpec, weatherFn),
llm.Handle(searchSpec, searchFn),
)
func (*BoundToolSpec[In, Out]) Definition ¶ added in v0.23.0
func (b *BoundToolSpec[In, Out]) Definition() ToolDefinition
Definition implements toolRegistration — delegates to the embedded spec.
func (*BoundToolSpec[In, Out]) Handle ¶ added in v0.23.0
Handle implements ToolHandler — validates, unmarshals, calls fn, marshals result.
func (*BoundToolSpec[In, Out]) ToolName ¶ added in v0.23.0
func (b *BoundToolSpec[In, Out]) ToolName() string
ToolName implements ToolHandler — returns the spec's tool name.
type CacheHint ¶ added in v0.20.0
type CacheHint struct {
// Enabled marks this content as a cache breakpoint candidate.
// For Anthropic/Bedrock: emits cache_control / cachePoint at this position.
// For OpenAI: no-op (caching is automatic).
Enabled bool
// TTL requests a specific cache duration.
// Valid values: "" (provider default, typically 5m), "5m", "1h".
// The "1h" option requires a supporting model (Claude Haiku/Sonnet/Opus 4.5+).
TTL string
}
CacheHint requests provider-side prompt caching for a message or request. It is a provider-neutral instruction: Anthropic and Bedrock translate it to explicit cache breakpoints on content blocks; OpenAI caching is always automatic and ignores per-message hints, but honours TTL on StreamRequest.CacheHint.
type Delta ¶ added in v0.23.0
type Delta struct {
// Type identifies which payload field is set.
Type DeltaType `json:"type"`
// Index is the position of this content block in the model's output array.
// nil when the provider does not supply block-level indexing.
//
// Index is meaningful because a single HTTP response can contain multiple
// blocks of the same type. With Anthropic's interleaved-thinking beta a
// single response may produce: thinking(0) → text(1) → tool(2) → thinking(3) → text(4).
// Without Index a consumer cannot tell which thinking or text block a delta
// belongs to.
//
// Provider semantics:
// Anthropic — content_block index, all block types
// Bedrock — ContentBlockIndex, all block types
// OpenAI Responses — output_index, all output types
// OpenAI Completions — tool_calls[].index, tool calls only; text=nil
// OpenRouter — tool_calls[].index, tool calls only; text=nil
// Ollama — nil (complete tool calls only, no streaming fragments)
Index *uint32 `json:"index,omitempty"`
// Text is populated for DeltaTypeText.
Text string `json:"text,omitempty"`
// Reasoning is populated for DeltaTypeReasoning.
Reasoning string `json:"reasoning,omitempty"`
// ToolID, ToolName, and ToolArgs are populated for DeltaTypeTool.
// ToolArgs is a raw partial JSON fragment — not yet a complete object.
ToolID string `json:"tool_id,omitempty"`
ToolName string `json:"tool_name,omitempty"`
ToolArgs string `json:"tool_args,omitempty"`
}
Delta carries one incremental content chunk from the model stream. Exactly one payload field is populated, indicated by Type.
func ReasoningDelta ¶ added in v0.23.0
ReasoningDelta creates a Delta for an incremental reasoning/thinking chunk.
type DeltaType ¶ added in v0.23.0
type DeltaType string
DeltaType identifies the kind of incremental content carried by a Delta.
type EventStream ¶ added in v0.23.0
type EventStream struct {
// contains filtered or unexported fields
}
EventStream wraps a buffered StreamEvent channel and stamps every outgoing event with the same RequestID, an incrementing sequence number, and a timestamp. Providers create one at the top of CreateStream via NewEventStream, send all events through Send, and return C() to callers.
func NewEventStream ¶ added in v0.23.0
func NewEventStream() *EventStream
NewEventStream creates an EventStream with a freshly generated RequestID, records the creation time, emits a StreamEventCreated event, and returns a buffered channel of 64 events.
func (*EventStream) C ¶ added in v0.23.0
func (s *EventStream) C() <-chan StreamEvent
C returns the read-only channel to hand back to the caller of CreateStream.
func (*EventStream) Close ¶ added in v0.23.0
func (s *EventStream) Close()
Close closes the underlying channel. Safe to call multiple times.
func (*EventStream) Delta ¶ added in v0.23.0
func (s *EventStream) Delta(d *Delta)
Delta sends a StreamEventDelta event with the given delta.
func (*EventStream) Done ¶ added in v0.23.0
func (s *EventStream) Done(usage *Usage)
Done sends a StreamEventDone event with the given usage statistics. usage may be nil if the provider did not return token counts.
func (*EventStream) Error ¶ added in v0.23.0
func (s *EventStream) Error(err *ProviderError)
Error sends a StreamEventError event. It accepts a *ProviderError so the contract is enforced at compile time: every error in a stream is structured.
func (*EventStream) Routed ¶ added in v0.23.0
func (s *EventStream) Routed(r Routed)
Routed sends a StreamEventRouted event with routing metadata.
func (*EventStream) Send ¶ added in v0.23.0
func (s *EventStream) Send(ev StreamEvent)
Send stamps ev with the stream's RequestID, a monotonically incrementing sequence number, and the current timestamp, then sends it on the channel. The first event sent has Seq 1.
func (*EventStream) Start ¶ added in v0.23.0
func (s *EventStream) Start(opts StreamStartOpts)
Start sends a StreamEventStart event with the given provider metadata. TimeToFirstToken is computed automatically from the stream's createdAt time.
func (*EventStream) ToolCall ¶ added in v0.23.0
func (s *EventStream) ToolCall(tc ToolCall)
ToolCall sends a StreamEventToolCall event for the given tool call.
type HttpClientOpts ¶ added in v0.23.0
type HttpClientOpts struct {
// Logger enables transport-level request/response logging at Debug level.
// When nil, no logging is performed.
Logger *slog.Logger
// Debug extends logging to include request/response headers and bodies.
// Has no effect when Logger is nil.
Debug bool
}
HttpClientOpts configures the HTTP client created by NewHttpClient.
type Message ¶
type Message interface {
Role() Role
Validate() error
json.Marshaler
// contains filtered or unexported methods
}
Message is the interface all message types implement.
type Messages ¶ added in v0.5.0
type Messages []Message
Messages is a slice of Message with JSON unmarshal support.
func (*Messages) AddAssistantMsg ¶ added in v0.23.0
func (*Messages) AddSystemMsg ¶ added in v0.23.0
func (*Messages) AddToolCallResult ¶ added in v0.23.0
func (*Messages) AddUserMsg ¶ added in v0.23.0
func (*Messages) UnmarshalJSON ¶ added in v0.5.0
type Model ¶
type Model struct {
ID string `json:"id"`
Name string `json:"name"`
Provider string `json:"provider"`
Aliases []string `json:"aliases,omitempty"`
}
Model represents an LLM model.
type ModelFetcher ¶
ModelFetcher is an optional interface providers can implement to list models dynamically from their API instead of returning a static list.
type Option ¶ added in v0.12.0
type Option func(*Options)
Option configures provider options.
func APIKeyFromEnv ¶ added in v0.12.0
APIKeyFromEnv returns an Option that reads the API key from environment variables. It tries each candidate in order, returning the first non-empty value. Returns an error at call time if none of the candidates are set.
func WithAPIKey ¶ added in v0.12.0
WithAPIKey sets a static API key.
func WithAPIKeyFunc ¶ added in v0.12.0
WithAPIKeyFunc sets a dynamic API key resolver. The function is called on each CreateStream() call, enabling:
- Lazy key resolution (fetch from secret manager on first use)
- Key rotation (fetch fresh key each time)
- Context-aware resolution (respect timeouts/cancellation)
func WithBaseURL ¶ added in v0.12.0
WithBaseURL sets a custom base URL for the provider.
func WithHTTPClient ¶ added in v0.23.0
WithHTTPClient sets a custom HTTP client for the provider. When not set, providers use DefaultHttpClient().
func WithLogger ¶ added in v0.23.0
WithLogger sets a logger for providers that emit events outside the HTTP transport layer (e.g. Bedrock's binary eventstream). Events are logged at Debug level using the same format as the HTTP transport, so the same log renderer handles output from all providers.
type Options ¶ added in v0.12.0
type Options struct {
// BaseURL is the base URL for the provider's API.
BaseURL string
// APIKeyFunc returns the API key for authentication.
// It is called on each CreateStream() call, allowing for lazy/dynamic resolution.
APIKeyFunc func(ctx context.Context) (string, error)
// HTTPClient is the HTTP client to use for API requests.
// When nil, providers fall back to DefaultHttpClient().
HTTPClient *http.Client
// Logger is used by providers that cannot log via the HTTP transport
// (e.g. Bedrock's binary eventstream). When set, stream events are logged
// at Debug level using the same message format as the HTTP transport logger
// so the same renderer handles both.
Logger *slog.Logger
}
Options holds configuration shared across providers.
type ParsedToolCall ¶
ParsedToolCall is the interface for parsed tool call results. Use a type switch on the concrete *TypedToolCall[T] to access typed params.
Example:
switch c := call.(type) {
case *TypedToolCall[GetWeatherParams]:
fmt.Println(c.Params.Location) // strongly typed
case *TypedToolCall[SearchParams]:
fmt.Println(c.Params.Query)
}
type ProviderError ¶ added in v0.23.0
type ProviderError struct {
// Sentinel is one of the Err* vars above. errors.Is matches against it.
Sentinel error `json:"-"`
// Provider is the name of the provider that produced this error.
// Use the ProviderName* constants.
Provider string `json:"provider"`
// Message is a human-readable description of the error.
Message string `json:"message"`
// Cause is the underlying error that triggered this one, if any.
Cause error `json:"-"`
// StatusCode is the HTTP response status code. Only set for ErrAPIError.
StatusCode int `json:"status_code,omitempty"`
// Body is the raw HTTP response body. Only set for ErrAPIError.
Body string `json:"body,omitempty"`
}
ProviderError is a structured error emitted by any provider. It wraps a sentinel so errors.Is works, carries the provider name for identification, and optionally holds an HTTP status code and body for API errors.
func AsProviderError ¶ added in v0.23.0
func AsProviderError(provider string, err error) *ProviderError
AsProviderError ensures err is a *ProviderError. If it already is one, it is returned as-is. Otherwise it is wrapped in a new ProviderError with ErrUnknown as the sentinel. This guarantees that every error surface from CreateStream and EventStream.Error() is a *ProviderError.
func NewErrAPIError ¶ added in v0.23.0
func NewErrAPIError(provider string, statusCode int, body string) *ProviderError
NewErrAPIError wraps a non-2xx HTTP response from a provider API.
func NewErrBuildRequest ¶ added in v0.23.0
func NewErrBuildRequest(provider string, cause error) *ProviderError
NewErrBuildRequest wraps a failure that occurred while building the outgoing request (e.g. JSON serialisation error).
func NewErrContextCancelled ¶ added in v0.23.0
func NewErrContextCancelled(provider string, cause error) *ProviderError
NewErrContextCancelled wraps a context cancellation for a provider stream.
func NewErrMissingAPIKey ¶ added in v0.23.0
func NewErrMissingAPIKey(provider string) *ProviderError
NewErrMissingAPIKey returns an error for a provider that has no API key configured.
func NewErrNoProviders ¶ added in v0.23.0
func NewErrNoProviders(provider string) *ProviderError
NewErrNoProviders returns an error when no providers are available or all failover targets have been exhausted.
func NewErrProviderMsg ¶ added in v0.23.0
func NewErrProviderMsg(provider string, msg string) *ProviderError
NewErrProviderMsg wraps an explicit error message sent by the provider inside the stream (e.g. an Anthropic error event or OpenRouter chunk error).
func NewErrRequestFailed ¶ added in v0.23.0
func NewErrRequestFailed(provider string, cause error) *ProviderError
NewErrRequestFailed wraps an HTTP transport-level failure.
func NewErrStreamDecode ¶ added in v0.23.0
func NewErrStreamDecode(provider string, cause error) *ProviderError
NewErrStreamDecode wraps a JSON or protocol decode failure mid-stream.
func NewErrStreamRead ¶ added in v0.23.0
func NewErrStreamRead(provider string, cause error) *ProviderError
NewErrStreamRead wraps an I/O or scanner error that occurred while reading the response stream.
func NewErrUnknownModel ¶ added in v0.23.0
func NewErrUnknownModel(provider string, modelID string) *ProviderError
NewErrUnknownModel returns an error for a model ID or alias that cannot be resolved by the provider.
func (*ProviderError) Error ¶ added in v0.23.0
func (e *ProviderError) Error() string
Error returns a human-readable error string in the form: "<provider>: <sentinel>: <message>" or "<provider>: <sentinel>: <message>: <cause>"
func (*ProviderError) Is ¶ added in v0.23.0
func (e *ProviderError) Is(target error) bool
Is reports whether this error matches target. It matches if target is the same sentinel, enabling errors.Is(err, ErrAPIError) etc.
func (*ProviderError) MarshalJSON ¶ added in v0.23.0
func (e *ProviderError) MarshalJSON() ([]byte, error)
MarshalJSON serialises ProviderError to JSON. Sentinel and Cause are rendered as strings so the full error is machine-readable.
func (*ProviderError) Unwrap ¶ added in v0.23.0
func (e *ProviderError) Unwrap() error
Unwrap returns Cause when set, allowing errors.As/Is to traverse the chain. When Cause is nil, Unwrap returns Sentinel so errors.Is(err, ErrAPIError) still works even with no underlying cause.
type ReasoningEffort ¶ added in v0.7.0
type ReasoningEffort string
ReasoningEffort controls the amount of reasoning for reasoning models. Lower values result in faster responses with fewer reasoning tokens.
const ( // ReasoningEffortNone disables reasoning (GPT-5.1+ only). ReasoningEffortNone ReasoningEffort = "none" // ReasoningEffortMinimal uses minimal reasoning effort. ReasoningEffortMinimal ReasoningEffort = "minimal" // ReasoningEffortLow uses low reasoning effort. ReasoningEffortLow ReasoningEffort = "low" // ReasoningEffortMedium uses medium reasoning effort (default for most models before GPT-5.1). ReasoningEffortMedium ReasoningEffort = "medium" // ReasoningEffortHigh uses high reasoning effort. ReasoningEffortHigh ReasoningEffort = "high" // ReasoningEffortXHigh uses extra high reasoning effort (codex-max+ only). ReasoningEffortXHigh ReasoningEffort = "xhigh" )
func (ReasoningEffort) Valid ¶ added in v0.8.0
func (r ReasoningEffort) Valid() bool
Valid returns true if the ReasoningEffort is a known valid value or empty.
type Resolver ¶ added in v0.16.0
type Resolver interface {
// Resolve returns the Model for the given model ID or alias.
// Returns an error if the model is not recognized.
Resolve(modelID string) (Model, error)
}
Resolver resolves a model alias or ID to its full Model representation.
type Routed ¶ added in v0.23.0
type Routed struct {
// Provider is the name of the backend provider selected (e.g. "anthropic", "bedrock").
Provider string `json:"provider"`
// ModelRequested is the model alias or name the caller originally asked for.
ModelRequested string `json:"model_requested,omitempty"`
// ModelResolved is the fully qualified model identifier dispatched to the provider.
ModelResolved string `json:"model_resolved,omitempty"`
// Errors contains errors from any targets that were tried and failed before
// this provider was selected. Empty when the first target succeeded.
Errors []error `json:"-"`
}
Routed carries routing metadata emitted by meta-providers (e.g. router) when a request has been dispatched to a specific backend provider.
func (Routed) MarshalJSON ¶ added in v0.23.0
MarshalJSON serialises Routed, rendering Errors as strings since []error is not directly JSON-marshallable.
type SortedMap ¶ added in v0.22.0
type SortedMap struct {
// contains filtered or unexported fields
}
SortedMap is a map[string]any that serialises its keys in alphabetical order. This guarantees deterministic JSON output for tool schema definitions, which is required for stable prompt-cache fingerprints on providers that hash tool definitions (Anthropic, Bedrock).
Construct with NewSortedMap. The zero value is valid and marshals as {}.
func NewSortedMap ¶ added in v0.22.0
NewSortedMap converts a map[string]any into a SortedMap whose keys are sorted alphabetically at every level of nesting. Nested map[string]any values and []any arrays are recursed so that all object nodes in the tree are also sorted. A nil or empty map produces a SortedMap that marshals as {}.
func (*SortedMap) MarshalJSON ¶ added in v0.22.0
MarshalJSON implements json.Marshaler. Keys are emitted in the order they were inserted (alphabetical, because NewSortedMap sorts them on construction).
type StopReason ¶ added in v0.23.0
type StopReason string
StopReason describes why the model stopped generating.
const ( // StopReasonEndTurn is natural completion — the model finished its response. StopReasonEndTurn StopReason = "end_turn" // StopReasonToolUse means the model emitted one or more tool calls. StopReasonToolUse StopReason = "tool_use" // StopReasonMaxTokens means the output length limit was reached. StopReasonMaxTokens StopReason = "max_tokens" // StopReasonContentFilter means output was blocked by the provider. StopReasonContentFilter StopReason = "content_filter" // StopReasonCancelled means the context was cancelled before the stream ended. StopReasonCancelled StopReason = "cancelled" // StopReasonError means the stream ended with a StreamEventError. StopReasonError StopReason = "error" )
type StreamEvent ¶
type StreamEvent struct {
// Type identifies which kind of event this is.
Type StreamEventType `json:"type"`
// RequestID is the library-assigned correlation ID for this stream.
// Generated once per CreateStream call; identical across all events in a stream.
RequestID string `json:"request_id,omitempty"`
// Seq is a monotonically incrementing sequence number within a stream.
// The first event has Seq 1. Useful for detecting dropped or reordered events.
Seq uint64 `json:"seq,omitempty"`
// Timestamp is the wall-clock time at which this event was sent.
Timestamp time.Time `json:"timestamp,omitempty"`
// Delta carries incremental model output. Populated for StreamEventDelta.
Delta *Delta `json:"delta,omitempty"`
// ToolCall is the tool invocation requested by the model. Populated for StreamEventToolCall.
ToolCall *ToolCall `json:"tool_call,omitempty"`
// Error holds the error that terminated the stream. Populated for StreamEventError.
Error *ProviderError `json:"error,omitempty"`
// Usage holds token counts and cost for the completed request. Populated for StreamEventDone.
Usage *Usage `json:"usage,omitempty"`
// Start holds stream metadata. Populated for StreamEventStart.
Start *StreamStart `json:"start,omitempty"`
// Routed holds routing metadata. Populated for StreamEventRouted.
Routed *Routed `json:"routed,omitempty"`
}
StreamEvent is a single event emitted by a provider during streaming.
func (StreamEvent) ReasoningText ¶ added in v0.23.0
func (e StreamEvent) ReasoningText() string
ReasoningText returns the reasoning content of the delta if this is a StreamEventDelta of type DeltaTypeReasoning, otherwise returns "".
func (StreamEvent) Text ¶ added in v0.23.0
func (e StreamEvent) Text() string
Text returns the text content of the delta if this is a StreamEventDelta of type DeltaTypeText, otherwise returns "".
type StreamEventType ¶
type StreamEventType string
StreamEventType identifies the kind of streaming event from a provider.
const ( StreamEventCreated StreamEventType = "created" StreamEventRouted StreamEventType = "routed" StreamEventStart StreamEventType = "start" StreamEventDelta StreamEventType = "delta" StreamEventToolCall StreamEventType = "tool_call" StreamEventDone StreamEventType = "done" StreamEventError StreamEventType = "error" )
type StreamRequest ¶ added in v0.23.0
type StreamRequest struct {
// Model is the model identifier or alias to use, e.g. "fast", "anthropic/claude-sonnet-4-5".
Model string `json:"model"`
// Messages is the conversation history to send to the model.
Messages Messages `json:"messages"`
// Tools is the set of tools the model may call during the response.
Tools []ToolDefinition `json:"tools,omitempty"`
// ToolChoice controls how the model selects tools. Defaults to Auto when Tools are provided.
ToolChoice ToolChoice `json:"tool_choice,omitempty"`
// ReasoningEffort controls the depth of reasoning for models that support it (e.g. OpenAI o-series).
ReasoningEffort ReasoningEffort `json:"reasoning_effort,omitempty"`
// CacheHint is a top-level prompt caching hint. Behaviour is provider-specific:
// Anthropic auto mode, Bedrock trailing cachePoint, OpenAI extended retention.
CacheHint *CacheHint `json:"cache_hint,omitempty"`
}
StreamRequest configures a provider CreateStream call.
func (StreamRequest) Validate ¶ added in v0.23.0
func (o StreamRequest) Validate() error
Validate checks that the options are valid.
type StreamResponse ¶ added in v0.23.0
type StreamResponse struct {
// contains filtered or unexported fields
}
StreamResponse is a client-side, stateful stream processor. Create one with Process, register callbacks and tool handlers fluently, then call Result() to start consuming the stream.
Example:
weatherSpec := llm.NewToolSpec[GetWeatherParams]("get_weather", "Get weather")
ch, err := provider.CreateStream(ctx, opts)
if err != nil { ... }
result := <-llm.Process(ctx, ch).
OnText(func(s string) { fmt.Print(s) }).
HandleTool(llm.Handle(weatherSpec, func(ctx context.Context, in GetWeatherParams) (*GetWeatherResult, error) {
return &GetWeatherResult{Temp: 22}, nil
})).
Result()
if result.Error() != nil { ... }
result.Apply(&msgs)
func Process ¶ added in v0.23.0
func Process(ctx context.Context, ch <-chan StreamEvent) *StreamResponse
Process creates a new StreamResponse that will consume ch. Call fluent methods to register callbacks and tool handlers, then call Result() to begin processing.
func (*StreamResponse) DispatchAsync ¶ added in v0.23.0
func (r *StreamResponse) DispatchAsync() *StreamResponse
DispatchAsync switches tool handler dispatch to concurrent mode: all tool calls emitted in a single response are executed in parallel, one goroutine per call. Results are collected in emission order before the stream is considered complete.
func (*StreamResponse) HandleTool ¶ added in v0.23.0
func (r *StreamResponse) HandleTool(handlers ...ToolHandler) *StreamResponse
HandleTool registers a ToolHandler that is invoked when the model emits a completed tool call matching h.ToolName(). The handler's output is stored in StreamResult.ToolResults and included in the messages returned by Next/Apply.
Pass a *BoundToolSpec (from llm.Handle) for typed, spec-aware handlers:
proc.HandleTool(llm.Handle(weatherSpec, func(ctx context.Context, in GetWeatherParams) (*GetWeatherResult, error) {
return &GetWeatherResult{Temp: 22}, nil
}))
func (*StreamResponse) OnReasoning ¶ added in v0.23.0
func (r *StreamResponse) OnReasoning(fn func(chunk string)) *StreamResponse
OnReasoning registers a callback that is called for each incremental reasoning/thinking token.
func (*StreamResponse) OnStart ¶ added in v0.23.0
func (r *StreamResponse) OnStart(fn func(*StreamStart)) *StreamResponse
OnStart registers a callback that is called when the StreamEventStart event arrives, carrying provider metadata (request ID, model, time-to-first-token).
func (*StreamResponse) OnText ¶ added in v0.23.0
func (r *StreamResponse) OnText(fn func(chunk string)) *StreamResponse
OnText registers a callback that is called for each incremental text token. Panics in the callback are recovered and recorded on the StreamResult error.
func (*StreamResponse) OnToolDelta ¶ added in v0.23.0
func (r *StreamResponse) OnToolDelta(fn func(d *Delta)) *StreamResponse
OnToolDelta registers a callback that is called for each partial tool-call argument fragment (DeltaTypeTool deltas).
func (*StreamResponse) Result ¶ added in v0.23.0
func (r *StreamResponse) Result() <-chan *StreamResult
Result starts consuming the stream (at most once) and returns a channel that yields exactly one *StreamResult when the stream is fully processed. The channel is closed after the result is sent.
Calling Result() multiple times is safe — the stream is only consumed once and the same channel is returned on subsequent calls.
func (*StreamResponse) WithToolDispatcher ¶ added in v0.23.0
func (r *StreamResponse) WithToolDispatcher(d ToolDispatcher) *StreamResponse
WithToolDispatcher sets the tool dispatcher explicitly.
type StreamResult ¶ added in v0.23.0
type StreamResult struct {
// Text is the concatenation of all DeltaTypeText deltas.
Text string
// Reasoning is the concatenation of all DeltaTypeReasoning deltas.
Reasoning string
// ToolCalls contains every tool call emitted by the model, in order.
ToolCalls []ToolCall
// ToolResults holds the output of every executed tool handler, in the same
// order as ToolCalls. Entries are present only when a ToolHandler was
// registered for the tool name; unhandled tools have no entry here.
ToolResults []ToolCallResult
// Usage holds token counts and cost. Nil if the provider did not report usage.
Usage *Usage
// Start holds the stream metadata emitted by StreamEventStart.
// Nil if the provider did not emit a start event.
Start *StreamStart
// Routed holds routing metadata emitted by meta-providers (e.g. router).
// Populated when the stream passed through a router that selected a backend.
// Nil when the request was sent directly to a provider.
Routed *Routed
// StopReason describes why the stream ended.
StopReason StopReason
// contains filtered or unexported fields
}
StreamResult is the final accumulated result of a processed stream. It is delivered exactly once on the channel returned by StreamResponse.Result().
func (*StreamResult) Apply ¶ added in v0.23.0
func (r *StreamResult) Apply(msgs *Messages)
Apply appends the assistant message and any tool results to msgs. Equivalent to msgs.Append(result.Next()...).
func (*StreamResult) Error ¶ added in v0.23.0
func (r *StreamResult) Error() error
Error returns any stream-level error (e.g. provider error, context cancellation).
func (*StreamResult) Message ¶ added in v0.23.0
func (r *StreamResult) Message() *AssistantMsg
Message builds an AssistantMsg from the accumulated result. Use this to append the assistant turn to a conversation history.
func (*StreamResult) Next ¶ added in v0.23.0
func (r *StreamResult) Next() []Message
Next returns the messages that should be appended to the conversation history after this turn: the AssistantMsg followed by one ToolCallResult message for each executed tool handler. If no tool handlers ran, only the AssistantMsg is returned.
This is the primary convenience for agentic loops:
msgs.Append(result.Next()...)
type StreamStart ¶ added in v0.16.0
type StreamStart struct {
// RequestID is the unique identifier returned by the upstream API.
// Useful for debugging and support tickets. May be empty if the API doesn't provide one.
RequestID string `json:"request_id,omitempty"`
// Model is the model identifier returned by the upstream API in its response.
// e.g., "claude-haiku-4-5-20251001". May be empty if the API doesn't echo the model back.
Model string `json:"model,omitempty"`
TimeToFirstToken time.Duration `json:"time_to_first_token,omitempty"`
}
StreamStart contains metadata about the stream, emitted with StreamEventStart.
func (StreamStart) MarshalJSON ¶ added in v0.23.0
func (s StreamStart) MarshalJSON() ([]byte, error)
MarshalJSON renders TimeToFirstToken as a human-readable string (e.g. "412ms") instead of raw nanoseconds. All other fields use their struct tags directly via the type alias trick to avoid infinite recursion.
type StreamStartOpts ¶ added in v0.23.0
type StreamStartOpts struct {
// RequestID is the unique identifier returned by the upstream API.
// Useful for debugging and support tickets. May be empty if the API doesn't provide one.
RequestID string
// Model is the model identifier returned by the upstream API in its response.
// e.g., "claude-haiku-4-5-20251001". May be empty if the API doesn't echo the model back.
Model string
}
StreamStartOpts is the input to EventStream.Start — what the provider knows from the upstream API response. Routing fields (requested model, resolved model) are not included here; they belong to a separate routing event in meta-providers.
type Streamer ¶ added in v0.19.0
type Streamer interface {
CreateStream(ctx context.Context, opts StreamRequest) (<-chan StreamEvent, error)
}
type SystemMsg ¶ added in v0.5.0
SystemMsg contains a system prompt.
func (*SystemMsg) MarshalJSON ¶ added in v0.5.0
type TokenCount ¶ added in v0.24.0
type TokenCount struct {
// InputTokens is the total estimated input token count:
// all messages + all tool definitions + any provider-specific overhead.
InputTokens int
// PerMessage contains the token count for each entry in TokenCountRequest.Messages,
// in the same index order. Does not include tool definitions or overhead.
// len(PerMessage) == len(TokenCountRequest.Messages) is guaranteed.
PerMessage []int
// Role breakdowns — derived from PerMessage, provided for convenience.
// SystemTokens + UserTokens + AssistantTokens + ToolResultTokens == sum(PerMessage).
SystemTokens int // sum of PerMessage for all RoleSystem messages
UserTokens int // sum of PerMessage for all RoleUser messages
AssistantTokens int // sum of PerMessage for all RoleAssistant messages
ToolResultTokens int // sum of PerMessage for all RoleTool (ToolCallResult) messages
// ToolsTokens is the total raw token count for all tool definitions combined,
// derived purely from the JSON-serialised tool schemas.
// sum(values(PerTool)) == ToolsTokens.
ToolsTokens int
// PerTool maps each tool definition's Name to its individual raw token count.
// sum(values(PerTool)) == ToolsTokens.
PerTool map[string]int
// OverheadTokens is the number of tokens the provider adds on top of the
// caller-supplied content — tokens the caller did not write and cannot
// control. Examples:
// - Anthropic: hidden tool-use system preamble + per-tool framing (~330+126+85×n)
// - Claude OAuth: injected billing/identity system blocks (~45 tokens)
//
// Zero for providers that add no hidden content (OpenAI, OpenRouter, Ollama).
//
// The invariant: InputTokens == sum(PerMessage) + ToolsTokens + OverheadTokens
// (plus any per-message overhead, e.g. +4/msg for OpenAI).
OverheadTokens int
}
TokenCount holds the result of a CountTokens call.
Invariants:
- len(PerMessage) == len(TokenCountRequest.Messages)
- SystemTokens + UserTokens + AssistantTokens + ToolResultTokens == sum(PerMessage)
- sum(values(PerTool)) == ToolsTokens (raw tool JSON counts only, no overhead)
- InputTokens == sum(PerMessage) + ToolsTokens + OverheadTokens + provider-specific per-message overhead
type TokenCountRequest ¶ added in v0.24.0
type TokenCountRequest struct {
// Model is the model ID to count tokens for (e.g. "gpt-4o", "claude-sonnet-4-5").
// Required — returns an error if empty.
Model string
Messages Messages
Tools []ToolDefinition
}
TokenCountRequest is the input to TokenCounter.CountTokens. Model is required — providers use it to select the correct BPE encoding.
type TokenCounter ¶ added in v0.24.0
type TokenCounter interface {
CountTokens(ctx context.Context, req TokenCountRequest) (*TokenCount, error)
}
TokenCounter is an optional interface providers may implement to estimate token usage before sending a request.
All implementations in this codebase are local/offline — no network call is made. Counts should be treated as estimates; accuracy varies by provider:
- OpenAI: exact (tiktoken matches the API tokenizer)
- OpenRouter: approximate (tiktoken, best-effort model prefix matching)
- Anthropic: approximate (cl100k_base, ±5-10% for English; tokenizer not public)
- Bedrock: approximate (same as Anthropic)
- Ollama: approximate (cl100k_base; no public tokenize endpoint)
Usage:
if tc, ok := provider.(llm.TokenCounter); ok {
count, err := tc.CountTokens(ctx, llm.TokenCountRequest{
Model: "gpt-4o",
Messages: messages,
Tools: tools,
})
if err == nil && count.InputTokens > maxTokens {
return fmt.Errorf("request too large: %d tokens (limit %d)", count.InputTokens, maxTokens)
}
}
type ToolCall ¶
ToolCall represents a request from the LLM to invoke a tool.
func (ToolCall) MarshalJSON ¶ added in v0.5.0
func (*ToolCall) UnmarshalJSON ¶ added in v0.5.0
type ToolCallResult ¶
ToolCallResult contains the result of executing a tool call.
func (*ToolCallResult) MarshalJSON ¶ added in v0.5.0
func (m *ToolCallResult) MarshalJSON() ([]byte, error)
func (*ToolCallResult) Role ¶ added in v0.5.0
func (m *ToolCallResult) Role() Role
func (*ToolCallResult) Validate ¶ added in v0.5.0
func (m *ToolCallResult) Validate() error
type ToolChoice ¶ added in v0.6.0
type ToolChoice interface {
// contains filtered or unexported methods
}
ToolChoice controls whether and which tools the model should call.
type ToolChoiceAuto ¶ added in v0.6.0
type ToolChoiceAuto struct{}
ToolChoiceAuto lets the model decide whether to call tools. This is the default behavior when ToolChoice is nil.
type ToolChoiceNone ¶ added in v0.6.0
type ToolChoiceNone struct{}
ToolChoiceNone prevents the model from calling any tools.
type ToolChoiceRequired ¶ added in v0.6.0
type ToolChoiceRequired struct{}
ToolChoiceRequired forces the model to call at least one tool.
type ToolChoiceTool ¶ added in v0.6.0
type ToolChoiceTool struct {
Name string
}
ToolChoiceTool forces the model to call a specific tool by name.
type ToolDefinition ¶
type ToolDefinition struct {
Name string `json:"name"`
Description string `json:"description"`
Parameters map[string]any `json:"parameters"`
}
ToolDefinition describes a tool that the model can invoke. This is used when sending tools to a provider's API.
func ToolDefinitionFor ¶
func ToolDefinitionFor[T any](name, description string) ToolDefinition
ToolDefinitionFor creates a ToolDefinition from a Go struct type using reflection. The struct's fields are converted to a JSON Schema that describes the tool's parameters.
Field tags:
- `json:"fieldName"` - Sets the parameter name (required)
- `jsonschema:"description=..."` - Describes the parameter
- `jsonschema:"required"` - Marks the parameter as required
- `jsonschema:"enum=val1,enum=val2"` - Restricts to specific values
Example:
type GetWeatherParams struct {
Location string `json:"location" jsonschema:"description=City name,required"`
Unit string `json:"unit" jsonschema:"description=Temperature unit,enum=celsius,enum=fahrenheit"`
}
tool := ToolDefinitionFor[GetWeatherParams]("get_weather", "Get current weather")
func (ToolDefinition) Validate ¶ added in v0.8.0
func (t ToolDefinition) Validate() error
Validate checks that the tool definition is valid.
type ToolDispatcher ¶ added in v0.23.0
type ToolDispatcher int
ToolDispatcher controls how tool calls are executed when multiple tools are emitted in a single response.
const ( // ToolDispatchSync executes tool handlers one at a time in emission order. // This is the default. ToolDispatchSync ToolDispatcher = iota // ToolDispatchAsync executes all tool handlers concurrently, one goroutine // per tool call. Results are collected in emission order. ToolDispatchAsync )
type ToolHandler ¶ added in v0.23.0
type ToolHandler interface {
// ToolName returns the name this handler is registered for.
ToolName() string
// Handle executes the tool call and returns its output as a string.
// The string is stored verbatim as the ToolCallResult content.
Handle(ctx context.Context, call ToolCall) (string, error)
}
ToolHandler is a self-describing executor for a single tool. It knows its own name (used for registration) and can execute a raw ToolCall.
func NewToolHandler ¶ added in v0.23.0
func NewToolHandler[In, Out any](name string, fn func(ctx context.Context, in In) (*Out, error)) ToolHandler
NewToolHandler creates a named ToolHandler from a strongly-typed function without requiring a ToolSpec. Use this when you don't need schema validation or when the spec is defined elsewhere.
Example:
proc.HandleTool(llm.NewToolHandler("get_weather", func(ctx context.Context, in GetWeatherParams) (*GetWeatherResult, error) {
return &GetWeatherResult{Temp: 22}, nil
}))
type ToolSet ¶
type ToolSet struct {
// contains filtered or unexported fields
}
ToolSet manages a collection of tool specifications. It provides tool definitions for sending to providers and parses raw tool calls into strongly-typed results with validation.
func NewToolSet ¶
func NewToolSet(tools ...toolRegistration) *ToolSet
NewToolSet creates a ToolSet from one or more tool specs.
Example:
tools := NewToolSet(
NewToolSpec[GetWeatherParams]("get_weather", "Get weather"),
NewToolSpec[SearchParams]("search", "Search the web"),
)
func (*ToolSet) Definitions ¶
func (ts *ToolSet) Definitions() []ToolDefinition
Definitions returns all tool definitions for sending to providers.
func (*ToolSet) Parse ¶
func (ts *ToolSet) Parse(calls []ToolCall) ([]ParsedToolCall, error)
Parse converts raw ToolCalls (from stream events) into typed ParsedToolCalls. Each tool call's arguments are validated against its JSON Schema before parsing.
Successfully parsed calls are always returned. Errors from unknown tool names or validation/parse failures are collected and returned as a joined error. The error is non-fatal - you get all successfully parsed calls.
Example:
calls, err := tools.Parse(rawToolCalls)
if err != nil {
log.Printf("parse warnings: %v", err)
}
for _, call := range calls {
switch c := call.(type) {
case *TypedToolCall[GetWeatherParams]:
fmt.Println(c.Params.Location)
}
}
type ToolSpec ¶
type ToolSpec[T any] struct { // contains filtered or unexported fields }
ToolSpec is a type-safe tool specification that pairs a tool name/description with a Go struct that defines the parameter schema. It includes a compiled JSON Schema for runtime validation.
func NewToolSpec ¶
NewToolSpec creates a typed tool specification from a parameter struct. The struct's fields define the JSON Schema for the tool's parameters. Field tags are the same as ToolDefinitionFor: json, jsonschema.
Example:
type GetWeatherParams struct {
Location string `json:"location" jsonschema:"description=City name,required"`
}
spec := NewToolSpec[GetWeatherParams]("get_weather", "Get current weather")
func (*ToolSpec[T]) Definition ¶
func (s *ToolSpec[T]) Definition() ToolDefinition
Definition returns the ToolDefinition for sending to providers.
type TypedToolCall ¶
type TypedToolCall[T any] struct { ID string // Original tool call ID (for sending results back) Name string // Tool name Params T // Parsed, validated parameters }
TypedToolCall holds a parsed tool call with strongly-typed parameters.
func (*TypedToolCall[T]) ToolCallID ¶
func (c *TypedToolCall[T]) ToolCallID() string
ToolCallID returns the tool call ID.
func (*TypedToolCall[T]) ToolName ¶
func (c *TypedToolCall[T]) ToolName() string
ToolName returns the tool name.
type Usage ¶
type Usage struct {
// InputTokens is the total number of input tokens processed, including
// tokens served from cache (CacheReadTokens) and tokens written to cache
// (CacheWriteTokens). Callers can use this as the single "how many input
// tokens did this request consume" figure.
InputTokens int `json:"input_tokens"`
// OutputTokens is the number of tokens generated in the response.
OutputTokens int `json:"output_tokens"`
// TotalTokens is InputTokens + OutputTokens.
TotalTokens int `json:"total_tokens"`
// Cost is the total request cost in USD.
// For Anthropic, Bedrock, and OpenAI this is locally calculated from
// provider pricing tables and equals the sum of the breakdown fields below.
// For OpenRouter this is API-reported by the proxy (already includes cache pricing).
Cost float64 `json:"cost"`
// Detailed token breakdown (provider-specific, may be zero).
CacheReadTokens int `json:"cache_read_tokens,omitempty"` // Input tokens served from an existing cache entry (all providers).
CacheWriteTokens int `json:"cache_write_tokens,omitempty"` // Input tokens written to a new cache entry (Anthropic, Bedrock).
ReasoningTokens int `json:"reasoning_tokens,omitempty"` // Output tokens consumed by model reasoning (e.g. extended thinking).
// Granular cost breakdown in USD (zero if provider/model pricing is unknown).
// Sum of InputCost + CacheReadCost + CacheWriteCost + OutputCost == Cost.
// Not populated for OpenRouter (API-reported cost is used instead).
//
// InputCost covers only the non-cached, non-write portion:
// InputTokens - CacheReadTokens - CacheWriteTokens tokens at the regular input rate.
InputCost float64 `json:"input_cost,omitempty"` // Cost of non-cached, non-write input tokens.
CacheReadCost float64 `json:"cache_read_cost,omitempty"` // Cost of cache-read tokens.
CacheWriteCost float64 `json:"cache_write_cost,omitempty"` // Cost of cache-write tokens.
OutputCost float64 `json:"output_cost,omitempty"` // Cost of output tokens.
}
Usage holds token counts and cost from a provider response.
type UserMsg ¶ added in v0.5.0
UserMsg contains user input.
func (*UserMsg) MarshalJSON ¶ added in v0.5.0
Source Files
¶
Directories
¶
| Path | Synopsis |
|---|---|
|
cmd
|
|
|
llmcli
command
llmcli is a command-line tool for testing LLM providers.
|
llmcli is a command-line tool for testing LLM providers. |
|
llmcli/cmds
Package cmds provides CLI commands for llmcli.
|
Package cmds provides CLI commands for llmcli. |
|
llmcli/store
Package store provides token storage implementations.
|
Package store provides token storage implementations. |
|
Package llmtest provides helpers for testing code that consumes llm.StreamEvent channels, following the convention of packages like net/http/httptest.
|
Package llmtest provides helpers for testing code that consumes llm.StreamEvent channels, following the convention of packages like net/http/httptest. |
|
Package modeldb provides access to the models.dev model database.
|
Package modeldb provides access to the models.dev model database. |
|
provider
|
|
|
anthropic/claude
Package claude provides an Anthropic provider using Claude OAuth tokens.
|
Package claude provides an Anthropic provider using Claude OAuth tokens. |
|
auto
Package auto provides zero-config multi-provider setup for LLM providers.
|
Package auto provides zero-config multi-provider setup for LLM providers. |
|
Package tokencount provides a shared offline tiktoken wrapper for LLM token estimation.
|
Package tokencount provides a shared offline tiktoken wrapper for LLM token estimation. |