Skip to main content

llmkit/
lib.rs

1//! # llmkit
2//!
3//! A unified, async, multi-provider LLM client for Rust. One trait, one
4//! streaming API, one Tower middleware stack — across OpenAI, Anthropic, and
5//! local Ollama models, with no provider lock-in.
6//!
7//! ```no_run
8//! use llmkit::prelude::*;
9//! use std::time::Duration;
10//!
11//! # async fn run() -> llmkit::LlmResult<()> {
12//! let client = LlmClientBuilder::new()
13//!     .provider(AnthropicProvider::from_env()?.model("claude-opus-4-8"))
14//!     .fallback(OpenAiProvider::from_env()?.model("gpt-4o-mini"))
15//!     .layer(TracingLayer::new())
16//!     .layer(RetryLayer::exponential(3, Duration::from_millis(200)))
17//!     .build()?;
18//!
19//! let resp = client
20//!     .chat(ChatRequest::builder().user("Hello!").build())
21//!     .await?;
22//! println!("{}", resp.text().unwrap_or_default());
23//! # Ok(()) }
24//! ```
25//!
26//! Provider adapters are feature-gated (`openai`, `anthropic`, `ollama`; all on
27//! by default). Disable defaults and opt in to slim the dependency tree.
28
29#![forbid(unsafe_code)]
30#![deny(missing_docs)]
31
32mod alias;
33mod builder;
34mod tool_loop;
35
36pub use alias::ModelAliases;
37pub use builder::{LlmClient, LlmClientBuilder};
38pub use tool_loop::ChatBuilder;
39
40// Core re-exports.
41pub use llmkit_core::{
42    pricing, ChatRequest, ChatRequestBuilder, ChatResponse, ChatStream, ContentPart, CostEstimate,
43    EmbedRequest, EmbedResponse, FinishReason, LlmError, LlmProvider, LlmResult, Message,
44    MessageContent, ModelPricing, Role, StreamDelta, TokenUsage, Tool, ToolCall, ToolChoice,
45    ToolResult, ToolSchema,
46};
47
48/// `#[derive(ToolSchema)]` for typed tool inputs.
49pub use llmkit_macros::ToolSchema;
50
51// Tower middleware re-exports.
52pub use llmkit_tower::{
53    CostTracking, CostTrackingLayer, FallbackProvider, LlmLayer, RateLimit, RateLimitLayer, Retry,
54    RetryLayer, SessionCost, Tracing, TracingLayer,
55};
56
57// Provider re-exports (feature-gated).
58#[cfg(feature = "openai")]
59pub use llmkit_openai::OpenAiProvider;
60
61#[cfg(feature = "anthropic")]
62pub use llmkit_anthropic::AnthropicProvider;
63
64#[cfg(feature = "ollama")]
65pub use llmkit_ollama::OllamaProvider;
66
67/// Common imports for application code.
68pub mod prelude {
69    pub use crate::{
70        ChatRequest, ChatResponse, CostTrackingLayer, LlmClient, LlmClientBuilder, LlmError,
71        LlmProvider, LlmResult, Message, RateLimitLayer, RetryLayer, StreamDelta, Tool, ToolSchema,
72        TracingLayer,
73    };
74
75    #[cfg(feature = "openai")]
76    pub use crate::OpenAiProvider;
77    #[cfg(feature = "anthropic")]
78    pub use crate::AnthropicProvider;
79    #[cfg(feature = "ollama")]
80    pub use crate::OllamaProvider;
81}