Husk
GitHub

Models

The model router

One surface over eleven providers — how an alias becomes a model, what happens when that model says no, and which code path your command actually takes.

Husk talks to eleven model providers through one interface. ModelRouter in @husk-ai/models answers four questions and nothing else:

  1. What is reachable right now? — detect()
  2. Which model does this alias mean? — resolve()
  3. Can we afford it? — the budget check
  4. What do we do when the provider says no? — the fallback chain
husk models

A provider is only listed once it is reachable and reports at least one model. Providers has the environment variable and the sign-up path for each one.

Aliases

Nine aliases resolve to a fixed model id. Three resolve to a strategy, because their honest answer depends on what is running on this machine right now.

AliasResolves to
opusanthropic/claude-opus-5
sonnetanthropic/claude-sonnet-5
haikuanthropic/claude-haiku-4-5-20251001
gptopenai/gpt-4.1
geminigoogle/gemini-2.5-pro
flashgoogle/gemini-2.5-flash
gemmaollama/gemma3
llamaollama/llama3.2
qwenollama/qwen2.5-coder
localstrategy: the best Ollama or LM Studio model that is running
freestrategy: best free model that is actually reachable
autostrategy: best available model, by tool support and then quality

resolveAlias tries five things, in this order, and stops at the first that answers:

  1. A user override from modelAliases in ~/.husk/config.json.
  2. A dynamic alias — local, free, auto.
  3. A canonical alias from the table above.
  4. An explicit provider/model id. Split at the first slash, so together/meta-llama/Llama-3.3-70B-Instruct-Turbo is one model, not two.
  5. A bare model name, but only when exactly one provider in the catalog offers it.

Anything else throws E_MODEL_UNAVAILABLE and lists the aliases it does know.

Pinning your own

~/.husk/config.json
{
  "modelAliases": {
    "fast": "groq/llama-3.3-70b-versatile",
    "sonnet": "anthropic/claude-sonnet-5"
  }
}

"model": "free" in the same file is shorthand for an auto override: it is what auto means on this machine, unless modelAliases has an auto entry of its own.

An override may point at another alias. The resolver follows up to eight hops and refuses a cycle rather than hanging:

error Model alias "a" loops back on itself
hint:  Fix the modelAliases entry for "a" in ~/.husk/config.json.

What auto, free and local actually pick

The router pools every reachable model, drops those the prompt would price over the ceiling, then sorts:

StrategyPoolSort
autoevery reachable modeltool support, then quality, then size, then context window
freeevery reachable modelfree models first, then input price, then as auto
localonly ollama and lmstudiofree first, then input price, then as auto

"Quality" is a maintainer's judgement number in the catalog, 0–100. It is not a benchmark score. A model nobody has catalogued — a fresh ollama pull — scores 40 if it is free and 50 if it is not. Size settles a tie: a model whose parameter count is known comes first, 7B or more beats a smaller one, and within that tier the smaller one wins. Provider priority plays no part, and nor does price for auto.

husk run picks its model before the run starts, so its header can name it, and it resolves the three its own way, walking the providers by priority, highest first:

  • auto is the best model of the first reachable provider that lists one. With ANTHROPIC_API_KEY set, that is anthropic/claude-opus-5, whatever else is reachable.
  • free is the first free model of the first provider that has one, so Gemini Flash wins over a local Ollama when GOOGLE_API_KEY is set.
  • local tries LM Studio before Ollama.

Under a ceiling of $0, each of the three considers only models priced at zero, as the router does.

Detection

detect() probes every registered provider in parallel and caches the answer for 30 seconds. Each probe is capped at 3 seconds; a provider that hangs is reported as unavailable with the reason rather than stalling the report, and so is one that answers but lists no model. Nothing here throws. husk doctor does not call it: doctor asks each provider itself and shows that provider's own reason and fix.

Providers with a key requirement answer instantly from the environment. Keyless local servers — Ollama, LM Studio — are probed over the network with their own 2-second timeout.

Fallback

RouterRequest.fallbacks and a husk's fallbackModels build an ordered candidate list: the requested model first, then each declared fallback, then whatever the strategy ordering turns up. Each candidate is tried in turn.

Two rules shape the behaviour:

  • A downgrade is never silent. The caller gets a warning event naming what failed and what replaced it. A run that quietly finished on an 8B local model when it asked for Opus is worse than a run that failed.
  • A 400 is never retried, anywhere, and nor is a 422. The request is malformed; shopping a malformed request around six providers wastes six round trips.

Within one candidate, the router makes up to 3 attempts. The wait before a retry is random, up to 400 ms before the second attempt and up to 800 ms before the third. When a candidate has failed for good, the router moves on to the next, unless the failure was an abort, a 400 or a 422. A provider's status becomes an error code:

StatusCodeRetried
401, 403E_NO_CREDENTIALSno
404E_MODEL_UNAVAILABLEno
400, 422E_MODEL_ERRORno, and no fallback
429E_QUOTAyes
408, 409, 425, 5xxE_MODEL_ERRORyes
any otherE_MODEL_ERRORno

A network failure is E_MODEL_ERROR and is retried. An abort is E_ABORTED, and E_BUDGET_EXCEEDED is not retried either.

On a stream, a candidate can only be abandoned before it has produced content. Once a token has reached the caller there is nothing honest to do with a mid-stream failure except report it, so that case ends in an error event rather than a silent restart on a different model.

Both husk run and POST /v1/husks/:name/run honour fallbackModels. husk run first finds the first reachable model of [model, ...fallbackModels], so its header names what will run, then calls a ModelRouter with the list on every request. A run over the API hands the router the same list.

For that first step the CLI keeps its own alias table, resolveModel() in packages/cli/src/lib/models.ts, which resolves auto, free and local as above. After each fixed alias's own id it lists older ones: anthropic/claude-3-opus-20240229 for opus, anthropic/claude-3-7-sonnet-20250219 and anthropic/claude-3-5-sonnet-20241022 for sonnet, anthropic/claude-3-5-haiku-20241022 for haiku, and openai/gpt-4o for gpt. Anthropic's provider lists only the catalog, which has no Claude 3 model, and openai/gpt-4.1 is tried first and is listed whenever OpenAI is, so those never change what is picked. It also matches an Ollama model without its :latest, so llama finds llama3.2:latest.

Cost

Every response comes back with a costUsd, worked out from the catalog price and the reported token counts; no provider's own figure is read. A model the catalog does not price therefore comes to $0. A local model is genuinely $0.00, not a rounded-down estimate.

Cost and budgets covers the estimate, the maxCostUsd ceiling, and which prices in the catalog are published figures rather than conservative guesses.

Where to go next

  • Providers — every provider, its environment variable, and how to get a key. Four have a real free tier.
  • Local models — Ollama and LM Studio end to end, and an honest account of what a 7B model can and cannot do as an agent.
  • Cost and budgets — estimation, the ceiling, and why local is zero.