llm

package
v0.14.0 Latest Latest
Warning

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

Go to latest
Published: Nov 8, 2026 License: MIT Imports: 14 Imported by: 0

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

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

DefaultModel is the model "haiku" and an empty name mean.

View Source
const MessagesURL = "https://api.anthropic.com/v1/messages"

MessagesURL is the Anthropic Messages API endpoint.

Variables

View Source
var (
	// ErrModelNotFound is the API's answer for a model id it does not know.
	ErrModelNotFound = errors.New("model not found")
	ErrUnauthorized  = errors.New("API key rejected")
	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

func APIKey(override string) string

APIKey is the key to use: override (sessions.api_key) when set, else ANTHROPIC_API_KEY. Returns the key; never log it.

func ResolveModel

func ResolveModel(name string) (id string, known bool)

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

type API struct {
	Key    string
	URL    string
	Client *http.Client
}

API calls the Anthropic Messages API.

func NewAPI

func NewAPI(key, url string, timeout time.Duration) *API

NewAPI is the Messages API at url with key and a per-call timeout.

func (*API) Complete

func (b *API) Complete(ctx context.Context, req Request) (Reply, error)

Complete sends one user message (and the system prompt when set). Errors keep only the status and the API's error type: its message may echo input.

type Backend

type Backend interface {
	Complete(ctx context.Context, req Request) (Reply, error)
}

Backend makes one model call. Errors carry no prompt or reply text.

func Backends

func Backends(apiKey, url string) (primary, fallback Backend)

Backends picks how a caller reaches a model: the Messages API at url when an API key is set (apiKey, else ANTHROPIC_API_KEY), with claude -p as the fallback for "model not found"; otherwise claude -p alone. Both time out after 60 s.

type CLI

type CLI struct {
	Bin     string
	Timeout time.Duration
}

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.

func (*CLI) Complete

func (b *CLI) Complete(ctx context.Context, req Request) (Reply, error)

Complete runs claude -p once and reads its JSON result.

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.

func (*Client) Complete

func (c *Client) Complete(ctx context.Context, req Request) (Reply, error)

Complete makes the call and records its usage. A failed call records nothing; a failed record is logged, not returned.

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.

type Request

type Request struct {
	Model     string
	System    string
	Prompt    string
	MaxTokens int
}

Request is one model call: an optional system prompt, a user prompt and an output cap. Model is a full id (or "haiku" for claude -p).

Jump to

Keyboard shortcuts

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