Models and providers
Every agent needs a model, and every vendor speaks to its models a little differently: its own URL, its own auth header, its own prices, sometimes its own wire protocol. Troupekit hides that behind one short string. You name a model with a selector, vendor/model, such as openai/gpt-6-luna. At build, the selector is looked up in the provider catalog, which supplies the wire protocol, the base URL, the auth header and the price.
The built-in catalog
| Vendor id | Protocol | Auth |
|---|---|---|
anthropic | anthropic | x-api-key |
openai | openai | bearer |
google | gemini | x-goog-api-key |
openrouter | openai_compatible | bearer |
groq | openai_compatible | bearer |
deepseek | openai_compatible | bearer |
mistral | openai_compatible | bearer |
ollama | openai_compatible | none |
lmstudio | openai_compatible | none |
Each vendor lists its models with a context window and a price, in millionths of a dollar per million tokens. Troupekit uses the price to meter spend, which is what your spend limits are checked against.
ollama and lmstudio are servers you run on your own machine. They are marked open_models, so they accept model ids the catalog does not list, at a price of zero. You can use any model you have pulled into them.
A model row looks like this:
[[vendor.model]]
id = "gpt-6-luna"
context_window = 1050000
input_usd_micros_per_mtok = 100000
output_usd_micros_per_mtok = 500000
price_verified = true
long_context = { above_input_tokens = 272000, input_usd_micros_per_mtok = 200000, output_usd_micros_per_mtok = 750000 }
The catalog also has [[embedding]] rows for the embedding models used by Knowledge: openai/text-embedding-3-small, openai/text-embedding-3-large and google/gemini-embedding-001.
API keys
At build, the key is read from <VENDOR>_API_KEY, with the vendor id upper-cased and - read as _. For OpenAI that is OPENAI_API_KEY, for Anthropic ANTHROPIC_API_KEY. Vendors with auth = "none", such as the local servers, need no key.
If your key lives somewhere else, .api_key(secret) sets it directly and wins over any environment variable, and .api_key_env(name) reads a different variable. Either way, the key never appears in Debug output or in an error message.
Your own catalog
To reach a server the built-in catalog does not know, such as a company gateway that speaks the OpenAI protocol, write your own catalog. Catalog::load(text) parses catalog TOML in the same shape as crates/troupekit-providers/catalog/providers.toml, and .catalog(catalog) replaces the built-in one. Selectors and prices then resolve against yours. Configuration lists the keys.
Transports and test models
.transport(t) sets the HTTP transport for catalog models. The default is a reqwest transport, behind the reqwest feature; an app that already has an HTTP stack can pass its own.
.model_client(m) skips the catalog and serves the selector from any Arc<dyn Model>. It needs no key, and a scripted or local model needs no network; a model of your own can still make network calls. That is how the tests in these docs run with a scripted model, and how the local model examples plug in llama.cpp. .price(p) overrides the price that replies are charged at.
Retries and fallback
Hosted models fail now and then: a rate limit, a timeout, an overloaded server. troupekit-providers ships three middleware models that wrap another model to deal with that.
RetryModel retries transient failures and honours the retry delay a server asks for. By default it makes 3 tries, starting from a 500 ms wait, and never waits more than 30 s. A stream is retried only while no delta has reached the caller. FallbackModel tries an ordered list of models and returns the first success. MeteredModel fills in the cost of every reply from the catalog prices; Catalog::connect wraps each model in one, which is how every agent gets metered.
On an agent, .retries(n) sets how many transient failures are retried per run, 3 by default.
To switch between a local and a cloud model by policy, see Local models and routing.