1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
//! # limit-llm
//!
//! [](https://crates.io/crates/limit-llm)
//! [](https://docs.rs/limit-llm)
//! [](https://opensource.org/licenses/MIT)
//!
//! **Multi-provider LLM client for Rust with streaming support.**
//!
//! Unified API for Anthropic Claude, OpenAI, z.ai, and local LLMs with built-in
//! token tracking, state persistence, and automatic model handoff.
//!
//! ## Features
//!
//! - **Multi-provider support**: Anthropic Claude, OpenAI GPT, z.ai GLM, and local LLMs
//! - **Streaming responses**: Async streaming with `futures::Stream`
//! - **Token tracking**: SQLite-based usage tracking and cost estimation
//! - **State persistence**: Serialize/restore conversation state with JSON
//! - **Model handoff**: Automatic fallback between providers on failure
//! - **Tool calling**: Full function/tool support for all compatible providers
//! - **Thinking mode**: Extended reasoning support (Claude, z.ai)
//!
//! ## Quick Start
//!
//! ```rust,no_run
//! use limit_llm::{AnthropicClient, Message, Role, LlmProvider};
//! use futures::StreamExt;
//!
//! #[tokio::main]
//! async fn main() -> Result<(), Box<dyn std::error::Error>> {
//! // Create client from environment variable ANTHROPIC_API_KEY
//! let client = AnthropicClient::new(
//! std::env::var("ANTHROPIC_API_KEY")?,
//! None, // default base URL
//! 60, // timeout in seconds
//! "claude-sonnet-4-6-20260217",
//! 4096, // max tokens
//! );
//!
//! let messages = vec![
//! Message {
//! role: Role::User,
//! content: Some("Hello, Claude!".to_string()),
//! tool_calls: None,
//! tool_call_id: None,
//! cache_control: None,
//! }
//! ];
//!
//! // Stream the response
//! let mut stream = client.send(messages, vec![]).await;
//!
//! while let Some(chunk) = stream.next().await {
//! match chunk {
//! Ok(limit_llm::ProviderResponseChunk::ContentDelta(text)) => {
//! print!("{}", text);
//! }
//! Ok(limit_llm::ProviderResponseChunk::Done(usage)) => {
//! println!("\n\nTokens: {} in, {} out",
//! usage.input_tokens, usage.output_tokens);
//! }
//! Err(e) => eprintln!("Error: {}", e),
//! _ => {}
//! }
//! }
//!
//! Ok(())
//! }
//! ```
//!
//! ## Providers
//!
//! | Provider | Client | Streaming | Tools | Thinking |
//! |----------|--------|-----------|-------|----------|
//! | Anthropic Claude | [`AnthropicClient`] | ✓ | ✓ | ✓ |
//! | OpenAI | [`OpenAiProvider`] | ✓ | ✓ | — |
//! | z.ai GLM | [`ZaiProvider`] | ✓ | ✓ | ✓ |
//! | Local/Ollama | [`LocalProvider`] | ✓ | — | — |
//!
//! ## Configuration
//!
//! ### Environment Variables
//!
//! ```bash
//! ANTHROPIC_API_KEY=your-key # For Claude
//! OPENAI_API_KEY=your-key # For GPT
//! ZAI_API_KEY=your-key # For z.ai
//! ```
//!
//! ### Programmatic Configuration
//!
//! ```rust,no_run
//! use limit_llm::{Config, ProviderFactory};
//!
//! # fn main() -> Result<(), Box<dyn std::error::Error>> {
//! // Load from ~/.limit/config.toml
//! let config = Config::load()?;
//!
//! // Create provider from config
//! let provider = ProviderFactory::create_provider(&config)?;
//! # Ok(())
//! # }
//! ```
//!
//! ## Token Tracking
//!
//! ```rust,no_run
//! use limit_llm::TrackingDb;
//!
//! # fn main() -> Result<(), Box<dyn std::error::Error>> {
//! let tracking = TrackingDb::new()?;
//!
//! // Track a request
//! tracking.track_request(
//! "claude-sonnet-4-6-20260217",
//! 100, // input tokens
//! 50, // output tokens
//! 0, // cache read tokens
//! 0, // cache write tokens
//! 0.001, // cost in USD
//! 1500, // duration in ms
//! )?;
//!
//! // Get statistics for last 7 days
//! let stats = tracking.get_usage_stats(7)?;
//! println!("Total cost: ${:.4}", stats.total_cost);
//! # Ok(())
//! # }
//! ```
//!
//! ## State Persistence
//!
//! ```rust,no_run
//! use limit_llm::{StatePersistence, Message};
//!
//! # fn main() -> Result<(), Box<dyn std::error::Error>> {
//! let persistence = StatePersistence::new("~/.limit/state/session.json");
//!
//! // Save conversation
//! let messages: Vec<Message> = vec![];
//! persistence.save(&messages)?;
//!
//! // Restore later
//! let restored = persistence.load()?;
//! # Ok(())
//! # }
//! ```
//!
//! ## Model Handoff
//!
//! The `ModelHandoff` type provides token counting and message compaction
//! for transitioning between models with different context windows:
//!
//! ```rust,no_run
//! use limit_llm::ModelHandoff;
//!
//! # fn main() {
//! let handoff = ModelHandoff::new();
//!
//! // Count tokens in a message
//! let tokens = handoff.count_tokens("Hello, world!");
//! println!("Token count: {}", tokens);
//!
//! // Compact messages to fit a target context window
//! // let compacted = handoff.handoff_to_model("claude-3-5-sonnet", "claude-3-5-haiku", &messages);
//! # }
//! ```
pub use apply_cache_control;
pub use AnthropicClient;
pub use ;
pub use LlmError;
pub use ModelHandoff;
pub use LocalProvider;
pub use OpenAiProvider;
pub use StatePersistence;
pub use ProviderFactory;
pub use ;
pub use ;
pub use TrackingDb;
pub use ;
pub use ;