Configure an AI provider¶
rtb-chat (formerly rtb-ai) gives one client across providers.
Pick a Provider, a model, and supply an API key (resolved through
the credential chain).
Which providers exist¶
Provider has six variants: Anthropic, AnthropicLocal, OpenAi,
OpenAiCompatible, Gemini, Ollama. Anthropic and AnthropicLocal
go through the direct Anthropic Messages path — which is what makes
prompt caching, extended thinking and citations available — and the rest
go through genai.
One client holds one provider. There is no per-call override and no fallback chain: a tool that wants two providers builds two clients.
Construct a client¶
use rtb_chat::{AiClient, Config, Provider};
use secrecy::SecretString;
# fn build(key: SecretString) -> Result<AiClient, rtb_chat::AiError> {
AiClient::new(Config {
provider: Provider::Anthropic,
model: "claude-opus-4-7".into(),
api_key: key,
..Config::default()
})
# }
Config is a plain struct with a Default impl — set what differs,
spread the rest. base_url defaults to the provider's standard
endpoint; override it for OpenAI-compatible or self-hosted gateways.
Default to Claude 4.7 + caching¶
New AI code should default to Claude 4.7 and enable prompt
caching at every stable point. Set cache_control: true on the
ChatRequest to cache the system prompt + first user message on the
Anthropic-direct path:
use rtb_chat::{ChatRequest, Message};
let req = ChatRequest {
system: Some("You are a release-notes assistant.".into()),
messages: vec![Message::user("Summarise these commits.")],
cache_control: true,
..ChatRequest::default()
};
Endpoint safety¶
A custom base_url must pass rtb_chat::validate_base_url: HTTPS only, no
userinfo in the URL, no placeholder hosts. Tests against a mock server
set Config::allow_insecure_base_url (which is #[serde(skip)], so a
config file can never downgrade HTTPS enforcement).
Structured responses¶
Use chat_structured::<T>() for a typed result: the schemars-derived
JSON Schema is sent with the request and the response is validated
before deserialising. See the
rtb-chat component page.