Skip to main content

Provider

Trait Provider 

Source
pub trait Provider: Send + Sync {
    // Required methods
    fn name(&self) -> &str;
    fn map_tool_choice(&self, choice: ToolChoice) -> Value;
    fn extract_usage(&self, response: &ChatResponse) -> TokenUsage;
    fn supports_native_tools(&self) -> bool;
    fn supports_prompt_caching(&self) -> bool;
    fn wants_detailed_usage(&self) -> bool;
}
Expand description

A backend that normalises tool-choice and usage for one family of models.

Why: The agent loop must stay backend-agnostic. By depending on dyn Provider it can drive OpenRouter today and Bedrock later without changing the loop — the factory (crate::provider::provider_for) picks the impl from the model slug. What: Object-safe (Send + Sync) trait with the four normalisation hooks the loop needs. Implementations live in openrouter and bedrock. Test: openrouter::tests::*, bedrock::tests::*.

Required Methods§

Source

fn name(&self) -> &str

Stable provider name for logging and diagnostics.

Why: Telemetry and error messages need a human-readable backend label that does not change across releases. What: Returns a short identifier such as "openrouter" or "bedrock". Test: openrouter::tests::name_is_stable, bedrock::tests::name_is_bedrock.

Source

fn map_tool_choice(&self, choice: ToolChoice) -> Value

Translate a neutral ToolChoice into this backend’s wire JSON.

Why: OpenAI-style backends use "none"/"auto"/"required" strings and a {type:function, function:{name}} object for a specific call; other backends differ. Centralising the mapping keeps the loop clean. What: Returns the serde_json::Value to place in ChatRequest.tool_choice. Test: openrouter::tests::map_tool_choice_*.

Source

fn extract_usage(&self, response: &ChatResponse) -> TokenUsage

Extract canonical token usage from a chat response.

Why: Cost accounting needs a single TokenUsage shape regardless of how the backend reports prompt/completion/cache tokens. What: Reads the provider-specific usage block out of response and maps it to TokenUsage. Test: openrouter::tests::extract_usage_maps_fields.

Source

fn supports_native_tools(&self) -> bool

Whether the backend/model supports native function-calling.

Why: Models without native tool support need prompt-injected fallback guidance instead of the tools array; this flag drives that selection in #1032/#1023. What: Returns true when the model can be sent the native tools array, false when the loop must fall back to prompt-based tool emulation. Test: openrouter::tests::supports_native_tools_*, bedrock::tests::bedrock_supports_native_tools.

Source

fn supports_prompt_caching(&self) -> bool

Whether this backend/model honours Anthropic-style cache_control prompt-cache breakpoints (#2156).

Why: Emitting a cache_control marker to a backend/model that ignores it is harmless (the field is simply dropped), but gating on this flag keeps the wire payload minimal for models that can never benefit and documents, in one place, exactly which backends this has been verified against — rather than scattering model.starts_with(...) checks at every call site. What: Returns true when agent_loop::build_request should mark the static tools+system-prompt prefix with an ephemeral cache breakpoint; false otherwise. Test: openrouter::tests::supports_prompt_caching_*, bedrock::tests::bedrock_does_not_support_prompt_caching.

Source

fn wants_detailed_usage(&self) -> bool

Whether this backend should be asked for detailed usage accounting (response-side cache-usage fix).

Why: OpenRouter’s detailed-usage directive (RequestUsageConfig::detailed, usage: {"include": true}) is what makes OpenRouter return its authoritative, cache-discount-aware usage.cost and the nested prompt_tokens_details cache counters — without it only the bare prompt/completion/total counts are guaranteed. This is an OpenRouter-specific wire directive with no equivalent on the direct/Bedrock path, so it must stay gated per backend rather than sent unconditionally. What: Returns true when agent_loop::build_request should attach ChatRequest.usage = Some(RequestUsageConfig::detailed()); false otherwise. Test: openrouter::tests::wants_detailed_usage_is_true, bedrock::tests::bedrock_does_not_want_detailed_usage.

Dyn Compatibility§

This trait is dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety".

Implementors§