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

model

package
v0.4.0 Latest Latest
Warning

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

Go to latest
Published: Oct 24, 2026 License: MIT Imports: 19 Imported by: 0

Documentation

Overview

Package model talks to LLM providers using a single OpenAI-shaped message format.

Index

Constants

This section is empty.

Variables

View Source
var ContextWindows = map[string]int{
	"gpt-4o":       128_000,
	"gpt-4o-mini":  128_000,
	"gpt-4.1":      1_047_576,
	"gpt-4.1-mini": 1_047_576,
	"gpt-4.1-nano": 1_047_576,
	"claude-":      200_000,
}

ContextWindows maps a model name prefix to its context window in tokens. The longest matching prefix wins. Unknown models return 0, meaning the window isn't known.

View Source
var PriceOverride [2]float64

PriceOverride, when non-zero, replaces the price table's input and output prices. The table's cache-read multiplier still applies.

View Source
var Prices = map[string][3]float64{
	"gpt-4o":            {2.50, 10.00},
	"gpt-4o-mini":       {0.15, 0.60},
	"gpt-4.1":           {2.00, 8.00},
	"gpt-4.1-mini":      {0.40, 1.60},
	"gpt-4.1-nano":      {0.10, 0.40},
	"claude-sonnet-4":   {3.00, 15.00},
	"claude-opus-4":     {15.00, 75.00},
	"claude-haiku-4-5":  {1.00, 5.00},
	"claude-3-5-haiku":  {0.80, 4.00},
	"claude-3-7-sonnet": {3.00, 15.00},
	"claude-sonnet-5-5": {2.00, 10.00, 0.05},
	"claude-opus-5-5":   {4.00, 20.00, 0.05},
	"claude-fable-5-1":  {10.00, 50.00, 0.025},
}

Prices maps a model name prefix to USD per million input/output tokens and, optionally, the cache-read price as a multiple of the input price (0 means the standard 0.1). The longest matching prefix wins. Unknown models cost 0 unless overridden.

Functions

func ContextWindow added in v0.4.0

func ContextWindow(spec string) int

ContextWindow returns the context window for spec, a "<provider>/<model>" name as given to New, or 0 when it isn't in ContextWindows. For a nested name such as openrouter/anthropic/claude-x, the last segment is tried too.

func Hint

func Hint(spec, errMsg string) string

Hint returns a one-line setup fix for a failed run of the model spec, given the error text, or "" for errors that aren't a common setup problem. It names environment variables but never prints their values.

func IsContextOverflow added in v0.4.0

func IsContextOverflow(err error) bool

IsContextOverflow reports whether err is a provider's "request is larger than the context window" error.

func Unpriced added in v0.4.0

func Unpriced(spec string) bool

Unpriced reports whether spec, as passed to New, names a model billed per token whose cost tinyagent can't estimate: no Prices entry and no PriceOverride, so its cost always reads 0. Ollama and Codex are free by design and never unpriced.

Types

type Anthropic

type Anthropic struct {
	BaseURL   string
	APIKey    string
	Model     string
	MaxTokens int          // defaults to 8192
	OnText    func(string) // if set, stream and report text deltas
}

Anthropic speaks the native Messages API.

func (*Anthropic) Query

func (a *Anthropic) Query(ctx context.Context, messages []Message, tools []ToolSpec) (Message, Cost, error)

type Codex

type Codex struct {
	BaseURL  string
	TokenURL string
	AuthFile string
	Model    string
	OnText   func(string)
	// contains filtered or unexported fields
}

Codex uses a ChatGPT subscription instead of an API key. It signs in with the tokens `codex login` saves in $CODEX_HOME/auth.json, refreshes them when they expire (writing them back so the Codex CLI stays signed in), and speaks the Responses API that the Codex backend serves.

func NewCodex

func NewCodex(model string, onText func(string)) (*Codex, error)

func (*Codex) Query

func (c *Codex) Query(ctx context.Context, messages []Message, tools []ToolSpec) (Message, Cost, error)

type Cost

type Cost struct {
	USD              float64 `json:"usd"`
	InputTokens      int     `json:"input_tokens"`
	OutputTokens     int     `json:"output_tokens"`
	CacheReadTokens  int     `json:"cache_read_tokens,omitempty"`
	CacheWriteTokens int     `json:"cache_write_tokens,omitempty"`
}

Cost is one response's usage. InputTokens counts every input token, including the cache reads and writes broken out after it.

type FunctionCall

type FunctionCall struct {
	Name      string `json:"name"`
	Arguments string `json:"arguments"`
}

type FunctionSpec

type FunctionSpec struct {
	Name        string         `json:"name"`
	Description string         `json:"description"`
	Parameters  map[string]any `json:"parameters"`
}

type Message

type Message struct {
	Role       string         `json:"role"`
	Content    string         `json:"content,omitempty"`
	ToolCalls  []ToolCall     `json:"tool_calls,omitempty"`
	ToolCallID string         `json:"tool_call_id,omitempty"`
	Extra      map[string]any `json:"extra,omitempty"`
}

type Model

type Model interface {
	Query(ctx context.Context, messages []Message, tools []ToolSpec) (Message, Cost, error)
}

func New

func New(spec string, onText func(string)) (Model, error)

New resolves "<provider>/<model>" to a Model. Without a "/", the provider is openai. If onText is non-nil, responses are streamed and each text delta is passed to it as it arrives.

type OpenAI

type OpenAI struct {
	BaseURL string
	APIKey  string
	Model   string
	OnText  func(string) // if set, stream and report text deltas
}

OpenAI speaks the Chat Completions API, which also covers OpenRouter, Ollama, and other OpenAI-compatible endpoints.

func NewOllama

func NewOllama(model string) *OpenAI

NewOllama returns a client for a local Ollama server's OpenAI-compatible endpoint.

func (*OpenAI) Query

func (o *OpenAI) Query(ctx context.Context, messages []Message, tools []ToolSpec) (Message, Cost, error)

type ToolCall

type ToolCall struct {
	ID       string       `json:"id"`
	Type     string       `json:"type"`
	Function FunctionCall `json:"function"`
}

type ToolResult

type ToolResult struct {
	ToolCallID string `json:"tool_call_id"`
	Content    string `json:"content"`
}

type ToolSpec

type ToolSpec struct {
	Type     string       `json:"type"`
	Function FunctionSpec `json:"function"`
}

ToolSpec is an OpenAI function-calling tool definition.

Jump to

Keyboard shortcuts

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