llmkit-rs 0.1.0

Unified multi-provider async LLM client for Rust — OpenAI, Anthropic, Ollama, with Tower middleware
Documentation
//! # llmkit
//!
//! A unified, async, multi-provider LLM client for Rust. One trait, one
//! streaming API, one Tower middleware stack — across OpenAI, Anthropic, and
//! local Ollama models, with no provider lock-in.
//!
//! ```no_run
//! use llmkit::prelude::*;
//! use std::time::Duration;
//!
//! # async fn run() -> llmkit::LlmResult<()> {
//! let client = LlmClientBuilder::new()
//!     .provider(AnthropicProvider::from_env()?.model("claude-opus-4-8"))
//!     .fallback(OpenAiProvider::from_env()?.model("gpt-4o-mini"))
//!     .layer(TracingLayer::new())
//!     .layer(RetryLayer::exponential(3, Duration::from_millis(200)))
//!     .build()?;
//!
//! let resp = client
//!     .chat(ChatRequest::builder().user("Hello!").build())
//!     .await?;
//! println!("{}", resp.text().unwrap_or_default());
//! # Ok(()) }
//! ```
//!
//! Provider adapters are feature-gated (`openai`, `anthropic`, `ollama`; all on
//! by default). Disable defaults and opt in to slim the dependency tree.

#![forbid(unsafe_code)]
#![deny(missing_docs)]

mod alias;
mod builder;
mod tool_loop;

pub use alias::ModelAliases;
pub use builder::{LlmClient, LlmClientBuilder};
pub use tool_loop::ChatBuilder;

// Core re-exports.
pub use llmkit_core::{
    pricing, ChatRequest, ChatRequestBuilder, ChatResponse, ChatStream, ContentPart, CostEstimate,
    EmbedRequest, EmbedResponse, FinishReason, LlmError, LlmProvider, LlmResult, Message,
    MessageContent, ModelPricing, Role, StreamDelta, TokenUsage, Tool, ToolCall, ToolChoice,
    ToolResult, ToolSchema,
};

/// `#[derive(ToolSchema)]` for typed tool inputs.
pub use llmkit_macros::ToolSchema;

// Tower middleware re-exports.
pub use llmkit_tower::{
    CostTracking, CostTrackingLayer, FallbackProvider, LlmLayer, RateLimit, RateLimitLayer, Retry,
    RetryLayer, SessionCost, Tracing, TracingLayer,
};

// Provider re-exports (feature-gated).
#[cfg(feature = "openai")]
pub use llmkit_openai::OpenAiProvider;

#[cfg(feature = "anthropic")]
pub use llmkit_anthropic::AnthropicProvider;

#[cfg(feature = "ollama")]
pub use llmkit_ollama::OllamaProvider;

/// Common imports for application code.
pub mod prelude {
    pub use crate::{
        ChatRequest, ChatResponse, CostTrackingLayer, LlmClient, LlmClientBuilder, LlmError,
        LlmProvider, LlmResult, Message, RateLimitLayer, RetryLayer, StreamDelta, Tool, ToolSchema,
        TracingLayer,
    };

    #[cfg(feature = "openai")]
    pub use crate::OpenAiProvider;
    #[cfg(feature = "anthropic")]
    pub use crate::AnthropicProvider;
    #[cfg(feature = "ollama")]
    pub use crate::OllamaProvider;
}