Documentation
¶
Overview ¶
Package llm is aimux's one model client: the Anthropic Messages API when an API key is set, else `claude -p` in safe mode with the owner's Claude Code login; the short model names config accepts; and a usage line in ~/.aimux/data/usage.jsonl for every call, labelled by the caller ("naming", "insight"). API keys are never logged, and errors carry no prompt or reply text.
Index ¶
Constants ¶
const DefaultModel = "claude-haiku-5-5"
DefaultModel is the model "haiku" and an empty name mean.
const MessagesURL = "https://api.anthropic.com/v1/messages"
MessagesURL is the Anthropic Messages API endpoint.
Variables ¶
var ( // ErrModelNotFound is the API's answer for a model id it does not know. ErrModelNotFound = errors.New("model not found") ErrBadReply = errors.New("unreadable model reply") ErrNoBackend = errors.New("no API key and no claude CLI") ErrModelCall = errors.New("model call failed") ErrTimeout = errors.New("timed out") )
Functions ¶
func APIKey ¶
APIKey is the key to use: override (sessions.api_key) when set, else ANTHROPIC_API_KEY. Returns the key; never log it.
func ResolveModel ¶
ResolveModel maps a configured model name ("haiku", "sonnet", "opus", or one of their full ids; empty means haiku) to a full model id. Any other name gives DefaultModel and known=false, so the caller can log it once.
Types ¶
type API ¶
API calls the Anthropic Messages API.
type Backend ¶
Backend makes one model call. Errors carry no prompt or reply text.
type CLI ¶
CLI runs `claude -p` with the owner's Claude Code login, isolated: flags confirmed with `claude --help` (Claude Code 2.1.314):
-p --model <id> print mode with the requested model
--safe-mode no CLAUDE.md, rules, memory, skills, plugins, hooks,
MCP servers or custom commands; the login still works
--output-format json one JSON result carrying usage and cost
--no-session-persistence the run is not saved, so it is never listed or named
--tools "" no built-in tools
--strict-mcp-config no MCP servers (none given with --mcp-config)
--settings {"disableAllHooks":true} no hooks from settings or plugins
--disable-slash-commands no skills or commands
--system-prompt <system prompt> replaces Claude Code's long default prompt (fewer tokens)
--bare would skip hooks too but reads only ANTHROPIC_API_KEY, never the owner's login, so it cannot serve as the no-key fallback; --safe-mode keeps the login. The flags after it stay as a second fence. The prompt goes on stdin, never on the command line where ps would show it. Each run uses a new empty temporary folder (removed after; a temp-folder cwd also marks the run automated if anything were saved) and Timeout. It runs in its own process group, killed whole on timeout, and Wait gives up on its output 5 s later, so a child holding stdout cannot hang the caller. There is no output-token flag: the system prompt keeps the answer short.
type Client ¶
type Client struct {
Primary Backend
Fallback Backend // optional
FallbackModel string // the model Fallback is asked for (default "haiku")
Label string // the usage line's label, e.g. "naming", "insight"
Record func(cost.Usage) error // default: cost.AppendUsage to cost.DefaultUsagePath()
Now func() time.Time // default: time.Now
Logf func(format string, args ...any) // default: log.Printf
}
Client calls Primary, once more through Fallback with FallbackModel when the API does not know the model, and records each successful call's usage under Label.
type Reply ¶
type Reply struct {
Text string
Model string // the model the backend reports, "" when it does not
InputTokens int64
OutputTokens int64
CostUSD *float64 // when the backend reports it; else from pricing, or unknown
Estimated bool // tokens guessed from text length
}
Reply is a model's answer with the tokens it cost.