molo_core/provider/mod.rs
1//! Provider: the interface for communicating with an LLM.
2//!
3//! This module defines the `Provider` trait and its companion data types:
4//! requests ([`ChatRequest`]), responses ([`ChatResponse`] / [`StreamEvent`]),
5//! errors ([`ProviderError`]), capabilities ([`ProviderCapabilities`]),
6//! request context ([`ProviderRequestContext`]), and usage ([`Usage`]). The
7//! trait itself is vendor-agnostic; the lightweight implementations
8//! ([`FakeProvider`] and [`RetryProvider`]) live here. Concrete network
9//! providers live in optional adapter crates such as `molo-openai`.
10//!
11//! The provider contract is intentionally explicit: successful `chat`
12//! returns exactly one assistant message, successful streams terminate with
13//! one `Done`, usage is reported only when the provider supplies it, and
14//! local decode/protocol/size-limit failures are distinct from vendor API
15//! errors. Context-aware methods are the runtime boundary: runtimes pass run
16//! ids, model request ids, deadlines, cancellation, and sanitized metadata to
17//! providers explicitly. The plain `chat` and `stream_chat` methods remain
18//! convenience entry points for direct provider use.
19
20mod fake;
21mod retry;
22
23pub use fake::{FakeProvider, FakeReply};
24pub use retry::{Backoff, RetryPolicy, RetryProvider, Retryable};
25
26use crate::message::Message;
27use crate::run::{RunContext, RunMetadata};
28use crate::tool::ToolSchema;
29use futures::stream::BoxStream;
30use serde::{Deserialize, Serialize};
31use std::collections::BTreeMap;
32use std::fmt;
33use std::time::{Duration, Instant};
34
35/// Provider capability metadata used by hosts and conformance tests.
36///
37/// Capabilities are descriptive, not a security boundary. When a provider
38/// declares support for an optional capability, it should pass the matching
39/// provider conformance cases. Unsupported direct calls should return
40/// [`ProviderError::Unsupported`] rather than panic.
41#[derive(Debug, Clone, Default, PartialEq, Eq, Serialize, Deserialize)]
42pub struct ProviderCapabilities {
43 /// Supports [`Provider::stream_chat`] / [`Provider::stream_chat_with_context`].
44 pub streaming: bool,
45 /// Maps provider reasoning/thinking output to [`Message`] or
46 /// [`StreamEvent::Reasoning`].
47 pub reasoning: bool,
48 /// Supports model tool-call requests.
49 pub tool_calls: bool,
50 /// Preserves multiple tool calls in one assistant turn.
51 pub parallel_tool_calls: bool,
52 /// Accepts [`ModelOptions::structured`] as a provider-side best-effort
53 /// constraint.
54 pub structured_output: bool,
55 /// Reports provider token usage when the backend supplies it.
56 pub usage: bool,
57 /// Cooperatively observes cancellation from [`ProviderRequestContext`].
58 pub context_cancellation: bool,
59 /// Cooperatively observes deadlines from [`ProviderRequestContext`].
60 pub context_deadline: bool,
61}
62
63impl ProviderCapabilities {
64 /// Baseline provider capabilities: non-streaming text only.
65 pub fn baseline() -> Self {
66 Self::default()
67 }
68}
69
70/// Request-scoped provider context.
71///
72/// This is the provider-boundary projection of [`RunContext`]: it carries
73/// correlation ids, cancellation/deadline controls, an optional timeout hint,
74/// and sanitized host metadata. Raw prompts, source code, auth headers, API
75/// keys, and environment values should not be placed in metadata by default.
76#[derive(Clone)]
77pub struct ProviderRequestContext {
78 /// Run id shared with run summaries, event records, and tracing spans.
79 pub run_id: String,
80 /// Model request id unique within the run.
81 pub model_request_id: String,
82 /// Cooperative cancellation source.
83 pub cancellation: tokio_util::sync::CancellationToken,
84 /// Optional absolute deadline.
85 pub deadline: Option<Instant>,
86 /// Optional per-provider-call timeout hint.
87 pub timeout: Option<Duration>,
88 /// Host/framework metadata for observability and routing.
89 pub metadata: RunMetadata,
90}
91
92impl fmt::Debug for ProviderRequestContext {
93 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
94 f.debug_struct("ProviderRequestContext")
95 .field("run_id", &self.run_id)
96 .field("model_request_id", &self.model_request_id)
97 .field("cancellation", &"CancellationToken")
98 .field("deadline", &self.deadline)
99 .field("timeout", &self.timeout)
100 .field("metadata", &self.metadata)
101 .finish()
102 }
103}
104
105impl ProviderRequestContext {
106 /// Builds provider context from a run context and model request id.
107 pub fn from_run_context(model_request_id: impl Into<String>, context: &RunContext) -> Self {
108 Self {
109 run_id: context.run_id.clone(),
110 model_request_id: model_request_id.into(),
111 cancellation: context.cancellation.clone(),
112 deadline: context.deadline,
113 timeout: context.remaining(),
114 metadata: context.metadata.clone(),
115 }
116 }
117
118 /// Sets a provider-call timeout hint.
119 pub fn with_timeout(mut self, timeout: Duration) -> Self {
120 self.timeout = Some(timeout);
121 self
122 }
123
124 /// Whether cancellation has already been requested.
125 pub fn is_cancelled(&self) -> bool {
126 self.cancellation.is_cancelled()
127 }
128
129 /// Whether the deadline has elapsed.
130 pub fn is_expired(&self) -> bool {
131 self.deadline
132 .is_some_and(|deadline| Instant::now() >= deadline)
133 }
134
135 /// Remaining time before the deadline.
136 pub fn remaining(&self) -> Option<Duration> {
137 self.deadline
138 .map(|deadline| deadline.saturating_duration_since(Instant::now()))
139 }
140}
141
142/// The interface for chatting with an LLM.
143///
144/// Implementations are responsible for communicating with a specific LLM
145/// service and mapping vendor responses back to this framework's [`Message`];
146/// [`chat`](Provider::chat) returns the full reply at once, while
147/// [`stream_chat`](Provider::stream_chat) returns the same reply incrementally
148/// as a stream of events. Both share the same semantics and differ only in
149/// delivery.
150///
151/// `Send + Sync` guarantees that `Box<dyn Provider>` can be held across
152/// awaits in Agent implementations (for the same reason as
153/// [`Tool`](crate::tool::Tool)).
154///
155/// # Examples
156///
157/// The calling convention is identical for every implementation; the example
158/// below uses [`FakeProvider`]:
159///
160/// ```rust
161/// # extern crate molo_core as molo;
162/// # #[tokio::main]
163/// # async fn main() -> Result<(), molo::ProviderError> {
164/// use molo::provider::{ChatRequest, FakeProvider, FakeReply, Provider};
165///
166/// let fake = FakeProvider::new([FakeReply::Text("hi".into())]);
167/// let response = fake.chat(ChatRequest::default()).await?;
168/// assert_eq!(response.message, molo::message::Message::assistant("hi"));
169/// # Ok(())
170/// # }
171/// ```
172#[async_trait::async_trait]
173pub trait Provider: Send + Sync {
174 /// Model identifier exposed by this provider, when known.
175 ///
176 /// Agents copy this value into run summaries for observability. Providers
177 /// that are not bound to one model can keep the default `None`.
178 fn model(&self) -> Option<&str> {
179 None
180 }
181
182 /// Capability metadata for this provider instance.
183 fn capabilities(&self) -> ProviderCapabilities {
184 ProviderCapabilities::baseline()
185 }
186
187 /// Sends one turn with request-scoped provider context.
188 ///
189 /// # Errors
190 ///
191 /// Network failures / timeouts / rate limits / vendor business errors are
192 /// all returned as [`ProviderError`]; see that type's docs for error
193 /// classification and retry guidance.
194 async fn chat_with_context(
195 &self,
196 request: ChatRequest,
197 context: &ProviderRequestContext,
198 ) -> Result<ChatResponse, ProviderError>;
199
200 /// Sends one turn of conversation and returns the model's reply (text, or
201 /// a request to call tools).
202 ///
203 /// This direct-use convenience wrapper creates a generated run context.
204 /// Runtimes that already have a [`RunContext`] should call
205 /// [`chat_with_context`](Provider::chat_with_context).
206 async fn chat(&self, request: ChatRequest) -> Result<ChatResponse, ProviderError> {
207 let run = RunContext::generated();
208 let context = ProviderRequestContext::from_run_context("direct-chat", &run);
209 self.chat_with_context(request, &context).await
210 }
211
212 /// Streams one turn of conversation for direct provider use.
213 ///
214 /// Semantically identical to [`chat`](Provider::chat), except that the
215 /// reply is returned as a stream of events: several [`StreamEvent::Delta`]
216 /// items concatenated in order form the full reply, and the stream ends
217 /// with [`StreamEvent::Done`].
218 ///
219 /// # Errors
220 ///
221 /// Failures during request setup (connection / timeout / vendor rejection)
222 /// are returned as `Err`; event errors after the stream is established are
223 /// produced as `Err` items in the stream, and no success events are
224 /// produced after an error item (see [`StreamEvent`] for termination
225 /// semantics).
226 async fn stream_chat(
227 &self,
228 request: ChatRequest,
229 ) -> Result<BoxStream<'static, Result<StreamEvent, ProviderError>>, ProviderError> {
230 let run = RunContext::generated();
231 let context = ProviderRequestContext::from_run_context("direct-stream", &run);
232 self.stream_chat_with_context(request, &context).await
233 }
234
235 /// Streams one turn with request-scoped provider context.
236 ///
237 /// Runtimes should call this method so providers can observe cancellation,
238 /// deadlines, request ids, and sanitized metadata.
239 async fn stream_chat_with_context(
240 &self,
241 request: ChatRequest,
242 context: &ProviderRequestContext,
243 ) -> Result<BoxStream<'static, Result<StreamEvent, ProviderError>>, ProviderError>;
244}
245
246/// `Box<dyn Provider>` is itself a Provider: re-exposes the trait object as a
247/// value, for assembly patterns that need to "hold an instance and create a
248/// new loop per call" (e.g., a sub-agent factory that captures a provider and
249/// constructs a fresh loop for each invocation).
250#[async_trait::async_trait]
251impl Provider for Box<dyn Provider> {
252 fn model(&self) -> Option<&str> {
253 self.as_ref().model()
254 }
255
256 fn capabilities(&self) -> ProviderCapabilities {
257 self.as_ref().capabilities()
258 }
259
260 async fn chat(&self, request: ChatRequest) -> Result<ChatResponse, ProviderError> {
261 self.as_ref().chat(request).await
262 }
263
264 async fn chat_with_context(
265 &self,
266 request: ChatRequest,
267 context: &ProviderRequestContext,
268 ) -> Result<ChatResponse, ProviderError> {
269 self.as_ref().chat_with_context(request, context).await
270 }
271
272 async fn stream_chat(
273 &self,
274 request: ChatRequest,
275 ) -> Result<BoxStream<'static, Result<StreamEvent, ProviderError>>, ProviderError> {
276 self.as_ref().stream_chat(request).await
277 }
278
279 async fn stream_chat_with_context(
280 &self,
281 request: ChatRequest,
282 context: &ProviderRequestContext,
283 ) -> Result<BoxStream<'static, Result<StreamEvent, ProviderError>>, ProviderError> {
284 self.as_ref()
285 .stream_chat_with_context(request, context)
286 .await
287 }
288}
289
290/// A single conversation request.
291///
292/// # Examples
293///
294/// ```rust
295/// # extern crate molo_core as molo;
296/// use molo::message::Message;
297/// use molo::provider::ChatRequest;
298///
299/// let request = ChatRequest {
300/// messages: vec![Message::user("hi")],
301/// ..Default::default()
302/// };
303/// # let _ = request;
304/// ```
305#[derive(Debug, Clone, Default, PartialEq, Serialize, Deserialize)]
306pub struct ChatRequest {
307 /// Conversation history (in order of occurrence, assembled by the caller).
308 pub messages: Vec<Message>,
309 /// Tool definitions offered to the model; when empty, the model sees no
310 /// tools.
311 pub tools: Vec<ToolSchema>,
312 /// Model options; `Default` means all vendor defaults.
313 pub options: ModelOptions,
314}
315
316/// Model options for one conversation.
317///
318/// Common parameters are provided as typed fields (temperature / max tokens,
319/// where `None` means vendor default); **vendor-specific or framework-unknown
320/// parameters go into [`extra`](ModelOptions::extra)** and are passed through
321/// to the vendor verbatim under their wire field names — so users can use new
322/// parameters without waiting for a framework update:
323///
324/// ```rust
325/// # extern crate molo_core as molo;
326/// use molo::ModelOptions;
327///
328/// let mut options = ModelOptions::default();
329/// options.extra.insert("top_p".into(), serde_json::json!(0.9));
330/// ```
331///
332/// Extra keys that collide with framework-managed fields are ignored in favor
333/// of the typed fields.
334#[derive(Debug, Clone, Default, PartialEq, Serialize, Deserialize)]
335pub struct ModelOptions {
336 /// Sampling temperature; `None` means vendor default.
337 pub temperature: Option<f32>,
338 /// Maximum number of tokens for the reply; `None` means vendor default.
339 pub max_tokens: Option<u32>,
340 /// Vendor extension parameters: keys are wire request field names,
341 /// serialized into the request body verbatim.
342 pub extra: BTreeMap<String, serde_json::Value>,
343 /// Structured output: the final answer must be JSON conforming to this
344 /// **JSON Schema document** (the serialized `RootSchema` produced by
345 /// schemars, or a hand-written schema).
346 ///
347 /// Two layers of semantics:
348 /// - Provider side: compatible endpoints receive it via `response_format`
349 /// to best-effort constrain the model (the OpenAI-compatible
350 /// `json_schema` shape; unsupported endpoints ignore it or error);
351 /// - Agent side: the final answer is **validated framework-side**, and on
352 /// mismatch the validation error is fed back to the model for a retry
353 /// (counted against the turn budget by the agent runtime's structured
354 /// output support).
355 ///
356 /// `None` = free-form text reply.
357 pub structured: Option<serde_json::Value>,
358}
359
360/// Token usage for one conversation.
361///
362/// Field names match the OpenAI wire format; `total_tokens` follows the
363/// vendor's convention (not necessarily the sum of the other two). `Default`
364/// = all zeros.
365///
366/// Presence is carried by the enclosing type: [`ChatResponse::usage`] /
367/// [`StreamEvent::Done::usage`] are `Option<Usage>` — `None` means the
368/// endpoint did not report usage for this turn, `Some` means the reported
369/// values. The distinction matters for observability: "not reported" is not
370/// the same as "reportedly zero" (the Agent layer also tracks it in
371/// [`RunSummary::usage_omitted`](crate::run::RunSummary)).
372#[derive(Debug, Clone, Copy, Default, PartialEq, Eq, Serialize, Deserialize)]
373pub struct Usage {
374 /// Input tokens for this turn.
375 pub prompt_tokens: u32,
376 /// Output tokens for this turn.
377 pub completion_tokens: u32,
378 /// Total for this turn (vendor convention).
379 pub total_tokens: u32,
380}
381
382impl Usage {
383 /// Constructs from input / output counts; the total is summed
384 /// automatically.
385 pub fn new(prompt_tokens: u32, completion_tokens: u32) -> Self {
386 Self {
387 prompt_tokens,
388 completion_tokens,
389 total_tokens: prompt_tokens + completion_tokens,
390 }
391 }
392}
393
394/// Usage accumulates per turn (the Agent layer sums tokens across turns).
395impl std::ops::AddAssign for Usage {
396 fn add_assign(&mut self, rhs: Self) {
397 self.prompt_tokens += rhs.prompt_tokens;
398 self.completion_tokens += rhs.completion_tokens;
399 self.total_tokens += rhs.total_tokens;
400 }
401}
402
403/// The reply to one conversation.
404#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
405pub struct ChatResponse {
406 /// This turn's reply: a single [`Message::Assistant`] message
407 /// (text + reasoning + any tool requests from the same turn stay together,
408 /// one-to-one with the vendor wire structure);
409 /// when the model produces nothing it is still an empty Assistant.
410 ///
411 /// The Agent loop executes tool requests when it sees them and appends the
412 /// results as [`Message::ToolResult`] before continuing the conversation.
413 pub message: Message,
414 /// Why the model ended its reply; vendor-specific reasons are surfaced via
415 /// [`FinishReason::Other`].
416 pub finish_reason: FinishReason,
417 /// Token usage for this turn; `None` when the endpoint did not return it
418 /// (compatible endpoints may omit usage; see [`Usage`] for the presence
419 /// semantics shared with [`StreamEvent::Done::usage`]).
420 pub usage: Option<Usage>,
421}
422
423/// Why the model ended its reply.
424///
425/// Common reasons are typed (Stop / Length); vendor-specific or
426/// framework-unknown reasons are surfaced via [`Other`](FinishReason::Other)
427/// carrying the vendor's raw string — users can recognize new reasons without
428/// waiting for a framework update. `#[non_exhaustive]` guarantees that adding
429/// new common categories in the future is not a breaking change.
430#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
431#[non_exhaustive]
432pub enum FinishReason {
433 /// The model ended naturally (including ending the turn by requesting
434 /// tool calls).
435 Stop,
436 /// Truncated by hitting the max_tokens limit.
437 Length,
438 /// A vendor-specific or framework-unknown reason, carrying the vendor's
439 /// raw string.
440 Other(String),
441}
442
443/// An event in a streamed conversation reply.
444///
445/// One streamed reply = several [`StreamEvent::Delta`] /
446/// [`StreamEvent::ToolCall`] increments + one closing [`StreamEvent::Done`];
447/// the caller concatenates the Deltas in order to get the full reply.
448///
449/// Stream termination semantics: on normal termination `Done` is always the
450/// last success event on the stream; errors terminate the stream with an
451/// `Err` item, and no events are produced after the error item.
452///
453/// The enum is `#[non_exhaustive]` (reserved for extension): matches must
454/// include a wildcard arm.
455#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
456#[non_exhaustive]
457pub enum StreamEvent {
458 /// An incremental fragment of the reply content.
459 Delta(String),
460 /// The model requests a tool call; argument fragments have already been
461 /// aggregated by the Provider into full JSON text, with fields matching
462 /// [`crate::ToolCall`] so the Agent can use them directly.
463 ToolCall {
464 /// Unique id of this call; the execution result is paired back to it
465 /// via this id.
466 id: String,
467 /// Tool name, corresponding to the name in
468 /// [`Tool::schema`](crate::tool::Tool::schema).
469 name: String,
470 /// Arguments generated by the model (JSON text).
471 arguments: String,
472 },
473 /// An incremental fragment of the model's reasoning (thinking); the
474 /// vendor delivers it in fragments during the stream, and the Provider
475 /// forwards each fragment as it arrives (like [`StreamEvent::Delta`]) —
476 /// consumers concatenate them in order to get the full text.
477 ///
478 /// Corresponds to the reasoning field of [`Message`]; the Agent must store
479 /// it in this turn's message and carry it back verbatim in the history
480 /// (otherwise thinking models like DeepSeek / Qwen3 reject the request).
481 Reasoning(String),
482 /// The model finished its reply; the stream produces no more events after
483 /// this.
484 Done {
485 /// Why the model ended its reply.
486 reason: FinishReason,
487 /// Token usage for this turn; `None` when the endpoint did not return
488 /// it (`include_usage` off or unsupported by the endpoint).
489 usage: Option<Usage>,
490 },
491}
492
493/// Why a Provider call failed.
494///
495/// The enum categories cover the cases that need distinguishing, with details
496/// carried by fields; vendor-specific errors are mapped into this type at the
497/// implementation boundary. `#[non_exhaustive]` guarantees that adding new
498/// categories in the future is not a breaking change.
499///
500/// Error classification is the basis for retry decisions (see the `Default`
501/// judgment of [`Retryable`]): Network / Timeout / RateLimited are worth
502/// retrying, while `Api` is judged by status (5xx retried, 4xx not — retrying
503/// would not change the outcome). Local decode/protocol errors are distinct
504/// from vendor API errors and are not retried by default.
505#[derive(Debug, Clone, PartialEq, Eq, thiserror::Error)]
506#[non_exhaustive]
507pub enum ProviderError {
508 /// A business/API error returned by the vendor (auth failure / invalid
509 /// arguments, quota exhaustion, server error), carrying the HTTP status
510 /// and optional vendor error code.
511 // No "provider " prefix: the type name already expresses the domain,
512 // avoiding a double prefix when wrapped by AgentError::Provider
513 // ("provider error: provider api error …").
514 #[error("api error (status {status}): {message}")]
515 Api {
516 /// HTTP status code returned by the vendor.
517 status: u16,
518 /// Optional vendor error code.
519 code: Option<String>,
520 /// Error description text.
521 message: String,
522 },
523 /// Provider response was syntactically decoded but violated either the
524 /// vendor contract or molo's provider contract.
525 #[error("provider protocol error: {message}")]
526 Protocol {
527 /// Error description text.
528 message: String,
529 },
530 /// Provider response body, SSE frame, or encoded payload could not be
531 /// decoded.
532 #[error("response decode error: {message}")]
533 Decode {
534 /// Error description text.
535 message: String,
536 },
537 /// Rate limited (HTTP 429): retrying is meaningful, and the default retry
538 /// policy waits before retrying.
539 ///
540 /// `retry_after`: the wait duration parsed from the vendor's
541 /// `Retry-After` response header (numeric seconds); `None` when absent
542 /// (HTTP date formats are not parsed).
543 #[error("rate limited")]
544 RateLimited {
545 /// The wait duration indicated by the vendor (numeric seconds);
546 /// `None` when missing / not numeric.
547 retry_after: Option<Duration>,
548 },
549 /// A network-layer failure (connection failure and other transport
550 /// errors).
551 ///
552 /// Implementations map concrete transport errors (e.g. reqwest's) into
553 /// carried text, so the error type does not depend on a concrete
554 /// implementation library's types.
555 #[error("network error: {0}")]
556 Network(String),
557 /// Request timeout, carrying the stage at which it occurred (see
558 /// [`TimeoutStage`]): distinguishes "cannot connect", "total duration
559 /// elapsed" and "event interval stalled" for easier diagnosis.
560 #[error("request timed out during {0:?}")]
561 Timeout(TimeoutStage),
562 /// Provider request was cancelled through provider context.
563 #[error("provider request cancelled")]
564 Cancelled,
565 /// Provider response exceeded a configured local size limit.
566 #[error("response exceeded configured limit ({limit_bytes} bytes)")]
567 ResponseTooLarge {
568 /// Configured limit that was exceeded.
569 limit_bytes: usize,
570 },
571 /// Capability was requested from a provider that does not support it.
572 #[error("unsupported provider capability: {capability}")]
573 Unsupported {
574 /// Unsupported capability name.
575 capability: &'static str,
576 },
577}
578
579/// The stage at which a timeout occurred: one-to-one with
580/// `OpenAiProvider`'s four timeouts (connect / non-streaming total /
581/// streaming event interval / streaming total), plus the error-response-body
582/// read timeout and a generic transport timeout.
583///
584/// The enum is `#[non_exhaustive]` (reserved for extension): matches must
585/// include a wildcard arm.
586#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
587#[non_exhaustive]
588pub enum TimeoutStage {
589 /// Connect timeout (Client-level connect timeout).
590 Connect,
591 /// Request timeout: for non-streaming, covers until the response body is
592 /// fully read; for streaming, covers the connect and response-header wait.
593 Request,
594 /// Streaming event interval exceeded: no data between two events (idle
595 /// timeout).
596 Idle,
597 /// Streaming total duration exceeded: the wall-clock deadline has elapsed
598 /// and an active but never-ending stream is terminated (stream timeout).
599 StreamTotal,
600 /// Total timeout for reading an error response body.
601 ResponseBody,
602 /// Generic transport timeout (reqwest cannot distinguish the stage, e.g.
603 /// during connect or read).
604 Transport,
605}