Skip to content

Configure an AI provider

rtb-ai gives one client across providers. Pick a [Provider], a model, and supply an API key (resolved through the credential chain).

Construct a client

use rtb_ai::{AiClient, Config, Provider};
use secrecy::SecretString;

# fn build(key: SecretString) -> Result<AiClient, rtb_ai::AiError> {
AiClient::new(Config {
    provider: Provider::Claude,
    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_ai::{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 [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-ai component page.