Skip to content

toolnexus.client/list-models

Clojure (JVM) + cljgo · package net.clojars.muthuishere/toolnexus · SPEC §8 · clojure/src/toolnexus/client.cljc

(list-models opts) ; opts: the same map create-client takes (:base-url :style :api-key :api-key-env :headers)
;; => [{:id "anthropic/claude-sonnet-4.5" :name "Claude Sonnet 4.5" :context-length 200000} …]
;; sorted by :id; :name and :context-length only when the provider reports them

One GET, no retries. openai style: GET {base}/models (OpenRouter’s context_length is read when present). anthropic style: GET {base}/v1/models (or {base}/models when the base already ends in /v1), following has_more / last_id across pages. The key follows the client’s rules exactly. A non-2xx throws ex-info with {:toolnexus/error :provider :status n}.

list-models uses koine’s HTTP client directly, not :http-client (which only posts).

  • A model picker — list what the configured provider serves and let the user switch.
  • Sizing a context budget — :context-length feeds :tool-tiers :context-window or a compactor.

1. A local server without a key is refused before any request

Section titled “1. A local server without a key is refused before any request”
(require '[toolnexus.client :as client])
(println)
(def err (try (client/list-models {:base-url "http://localhost:11434/v1"}) nil
(catch Exception e e)))
(assert (= :config (:toolnexus/error (ex-data err))))
(println "OK")
(require '[toolnexus.client :as client])
;; key from OPENROUTER_API_KEY — the host's own env var
(doseq [m (client/list-models {:base-url "https://openrouter.ai/api/v1"})]
(println (:id m) (or (:context-length m) "?")))

3. Anthropic, paginated, with an explicit key variable

Section titled “3. Anthropic, paginated, with an explicit key variable”
(def models (client/list-models {:base-url "https://api.anthropic.com" :style "anthropic"
:api-key-env "TEAM_ANTHROPIC_KEY"}))
(def llm (client/create-client {:base-url "https://api.anthropic.com" :style "anthropic"
:api-key-env "TEAM_ANTHROPIC_KEY"
:model (:id (first models))}))
  • api-key — the key rules list-models shares with the client.