Skip to main content

llm_kernel/llm/
mod.rs

1//! Async LLM client with OpenAI and Anthropic backends.
2//!
3//! The [`LLMClient`] trait provides a unified interface for chat completion
4//! and SSE streaming. Implementations: [`OpenAIClient`], [`AnthropicClient`].
5//!
6//! Resilience composes from focused decorators that all implement
7//! [`LLMClient`]: [`RetryClient`] (rate-limit / 5xx backoff), [`RouterClient`]
8//! (cost-aware routing + cross-provider fallback), [`MiddlewareClient`]
9//! (request/response observation), and [`CacheClient`] (response cache). See
10//! [`router`] for the composition table and a worked stack example.
11//!
12//! The [`json_extract`] module handles extracting structured JSON from
13//! raw LLM text output (code fences, raw JSON, etc.).
14//!
15//! Requires the `client-async` feature.
16
17/// Response cache wrapper for [`LLMClient`] over a [`KvStore`].
18///
19/// [`KvStore`]: crate::store::KvStore
20#[cfg(feature = "cache")]
21pub mod cache;
22/// Async LLM client implementations (OpenAI, Anthropic).
23#[cfg(feature = "client-async")]
24pub mod client;
25/// Conversation history with token-budget-aware truncation.
26#[cfg(feature = "tokens")]
27pub mod history;
28/// JSON extraction from raw LLM text output.
29pub mod json_extract;
30/// Middleware hooks for [`LLMClient`] request/response lifecycle.
31#[cfg(feature = "client-async")]
32pub mod middleware;
33/// Prompt template rendering.
34pub mod prompt;
35/// Exponential backoff retry wrapper for [`LLMClient`].
36#[cfg(feature = "client-async")]
37pub mod retry;
38/// Cost-aware routing and cross-provider fallback chain for [`LLMClient`].
39#[cfg(feature = "client-async")]
40pub mod router;
41/// Prompt templates with variable substitution and few-shot examples.
42pub mod template;
43/// Tool/function calling types.
44pub mod tool;
45/// Core LLM request/response types.
46pub mod types;
47
48#[cfg(feature = "cache")]
49pub use cache::CacheClient;
50#[cfg(feature = "client-async")]
51pub use client::{AnthropicClient, LLMClient, OpenAIClient};
52#[cfg(feature = "tokens")]
53pub use history::ConversationHistory;
54pub use json_extract::{JsonExtractor, extract_json, parse_json};
55#[cfg(feature = "client-async")]
56pub use middleware::{LLMClientMiddleware, MiddlewareClient, NoopMiddleware};
57pub use prompt::render_prompt;
58#[cfg(feature = "client-async")]
59pub use retry::{RetryClient, RetryConfig};
60#[cfg(feature = "client-async")]
61pub use router::{Backend, RouterClient, RoutingStrategy};
62pub use template::PromptTemplate;
63pub use tool::{ToolCall, ToolDefinition, ToolResult};
64#[cfg(feature = "client-async")]
65pub use types::LLMStream;
66pub use types::{
67    ChatMessage, ContentPart, LLMRequest, LLMRequestBuilder, LLMResponse, MessageRole, ModelConfig,
68    ResponseFormat, StreamEvent, TokenUsage,
69};