llm-cascade 0.1.2

Resilient cascading LLM inference with automatic failover across multiple providers
Documentation
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
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
> **⚠️ Early Stage / Alpha Software**
>
> This project is in active early development. The API surface may change, and not all edge cases are handled yet. Contributions are welcome — see [Contributing]#contributing below. Use at your own risk.

---

# llm-cascade

**Resilient, cascading LLM inference across multiple providers — failover, circuit breaking, and retry cooldowns built in.**

`llm-cascade` is a Rust library and CLI that sends prompts to an ordered list of LLM providers (OpenAI, Anthropic, Google Gemini, Ollama, and any OpenAI-compatible endpoint like Groq or Together). If one provider is rate-limited or down, it automatically falls through to the next, tracks per-entry cooldowns in SQLite, and persists failed prompts as JSON files.

---

## Features

- **Cascading failover** — define ordered provider/model lists; the first successful response wins
- **OpenAI-compatible providers** — point any `openai`-type provider at a custom `base_url` (Groq, Together, Z.AI, vLLM, etc.)
- **Per-entry circuit breaker** — cooldowns tracked per `provider/model` pair in SQLite
- **429-aware backoff** — parses `retry-after` headers; falls back to exponential backoff (30 s base, 1 h cap)
- **Cross-process state** — cooldown state persists across CLI invocations via SQLite
- **Secret management** — OS keyring (via `keyring`) with environment variable fallback
- **Failure persistence** — total cascade failures saved as timestamped `.json` files
- **Full audit log** — every attempt logged with timestamp, status, latency, and token counts
- **Dual interface** — use as a CLI tool or as an async library in your own Rust projects

---

## How It Works

```
┌──────────┐       ┌─────────────────────────────────────────────┐
│  CLI /   │       │              Cascade Engine                  │
│  Library │──────▶│                                             │
│  Caller  │       │  ┌───────────┐  ┌───────────┐  ┌─────────┐ │
└──────────┘       │  │ openai/   │─▶│ anthropic/│─▶│ ollama/ │ │
                   │  │ gpt-4o    │  │ claude…   │  │ llama3  │ │
                   │  └────┬──────┘  └────┬──────┘  └────┬────┘ │
                   │       │              │              │       │
                   │  ┌────▼──────────────▼──────────────▼────┐  │
                   │  │           SQLite Database              │  │
                   │  │  • attempt_log (audit trail)           │  │
                   │  │  • cooldown    (circuit breaker state)  │  │
                   │  └───────────────────────────────────────┘  │
                   │                                             │
                   │  On total failure:                          │
                   │  ┌───────────────────────────────────────┐  │
                    │  │  failed_prompts/cascade_20260414.json │  │
                   │  └───────────────────────────────────────┘  │
                   └─────────────────────────────────────────────┘
```

1. **Load config** from `~/.config/llm-cascade/config.toml` (or a custom path).
2. **Initialize SQLite** — creates `attempt_log` and `cooldown` tables if missing.
3. **Iterate cascade entries** — for each `provider/model` in the named cascade:
   - Check if the entry is on cooldown in the DB → skip if so.
   - Resolve the API key (keyring → env var).
   - Send the `Conversation` to the provider's API.
   - Log the attempt (status, latency, tokens).
   - On success → return `LlmResponse` immediately.
   - On failure → set cooldown (from `retry-after` header or exponential backoff) and continue.
4. **Total failure** → persist the `Conversation` as a `.json` file, return `CascadeError` with the file path.

---

## Installation

### From source

```sh
git clone https://github.com/paluigi/llm-cascade.git
cd llm-cascade
cargo install --path .
```

### As a library dependency

```toml
# Cargo.toml
[dependencies]
llm-cascade = "0.1"
```

> Requires Rust **1.85+** (edition 2024).

---

## Configuration

Run the setup command to scaffold the default configuration:

```sh
llm-cascade setup
```

Or use the interactive wizard to configure providers and cascades step-by-step:

```sh
llm-cascade setup --interactive
```

This creates `~/.config/llm-cascade/config.toml` with sensible defaults. Edit it to customize:

```toml
# ── Provider Definitions ────────────────────────────────────
# Each block defines an endpoint (type, base_url, auth).
# Providers are referenced by name in cascades and can be
# reused with different models — no need to duplicate config.

[providers.openai]
type = "openai"
api_key_service = "openai"          # keyring entry name
api_key_env = "OPENAI_API_KEY"      # env var fallback
# base_url defaults to https://api.openai.com/v1

[providers.anthropic]
type = "anthropic"
api_key_service = "anthropic"
api_key_env = "ANTHROPIC_API_KEY"
# base_url defaults to https://api.anthropic.com

[providers.gemini]
type = "gemini"
api_key_service = "gemini"
api_key_env = "GOOGLE_API_KEY"
# base_url defaults to https://generativelanguage.googleapis.com

[providers.groq]
type = "openai"                     # reuse OpenAI-compatible protocol
base_url = "https://api.groq.com/openai/v1"
api_key_service = "groq"
api_key_env = "GROQ_API_KEY"

[providers.ollama]
type = "ollama"
base_url = "http://localhost:11434"
# No API key needed

# ── Cascades ───────────────────────────────────────────────
# Each entry references a provider by name and specifies a model.
# The same provider can appear multiple times with different models.

[cascades.creative_task]
entries = [
    { provider = "openai", model = "gpt-4o" },
    { provider = "anthropic", model = "claude-sonnet-4-20250514" },
    { provider = "gemini", model = "gemini-2.0-flash" },
]

[cascades.fast_task]
entries = [
    { provider = "ollama", model = "llama3" },
    { provider = "groq", model = "llama-3.3-70b-versatile" },
    { provider = "openai", model = "gpt-4o-mini" },
]

[cascades.resilient_task]
entries = [
    { provider = "openai", model = "gpt-4o" },
    { provider = "openai", model = "gpt-4o-mini" },       # same provider, different model
    { provider = "groq", model = "llama-3.3-70b-versatile" },
    { provider = "anthropic", model = "claude-sonnet-4-20250514" },
    { provider = "ollama", model = "llama3" },
]

# ── Persistence ────────────────────────────────────────────

[database]
path = "~/.local/share/llm-cascade/db.sqlite"

[failure_persistence]
dir = "~/.local/share/llm-cascade/failed_prompts"
```

### Provider types

| Type | Description | Default `base_url` |
|------|-------------|-------------------|
| `openai` | OpenAI Chat Completions API | `https://api.openai.com/v1` |
| `anthropic` | Anthropic Messages API | `https://api.anthropic.com` |
| `gemini` | Google Gemini generateContent API | `https://generativelanguage.googleapis.com` |
| `ollama` | Ollama local inference | `http://localhost:11434` |

Any provider with `type = "openai"` can be pointed at a custom `base_url` to use OpenAI-compatible services such as **Groq**, **Together AI**, **Z.AI**, **vLLM**, **LiteLLM**, etc.

### API Keys

| Method | How it works |
|--------|-------------|
| **OS Keyring** (preferred) | Set via `llm-cascade key set <provider>`. The `api_key_service` field is the keyring entry name. |
| **Environment variable** | Export the variable named in `api_key_env` (e.g., `export OPENAI_API_KEY=sk-...`). |
| **Ollama** | No API key needed for local models. |

The library tries the keyring first and falls back to the environment variable automatically. Use `llm-cascade key list` to check the status of all providers.

---

## CLI Usage

### Subcommands

`llm-cascade` uses subcommands for all operations:

```
llm-cascade run       -C <cascade> -p <prompt>   Run a cascade
llm-cascade setup     [--interactive]              Initialize configuration
llm-cascade key set   <provider>                   Store an API key
llm-cascade key get   <provider> [--show-full]     Retrieve an API key
llm-cascade key list                               Show key status for all providers
llm-cascade key delete <provider>                   Remove an API key
```

### Running a cascade

**Basic prompt:**

```sh
llm-cascade run -C creative_task -p "Write a haiku about Rust"
```

**From a JSON conversation file:**

```sh
llm-cascade run -C creative_task -f conversation.json
```

The JSON file must match the `Conversation` schema:

```json
{
  "messages": [
    { "role": "system", "content": "You are a helpful assistant." },
    { "role": "user", "content": "What is 2 + 2?" }
  ],
  "tools": [
    {
      "name": "get_weather",
      "description": "Get the current weather",
      "parameters": {
        "type": "object",
        "properties": {
          "location": { "type": "string" }
        },
        "required": ["location"]
      }
    }
  ]
}
```

**Custom config path:**

```sh
llm-cascade run -c /path/to/my/config.toml -C fast_task -p "Hello"
```

### Setup

**Default setup** — scaffolds the example config, creates directories, and initializes the database:

```sh
llm-cascade setup
```

**Interactive setup** — wizard for selecting providers, defining cascades, and setting API keys:

```sh
llm-cascade setup --interactive
```

### Key management

**Store an API key** (prompts with hidden input):

```sh
llm-cascade key set openai
```

**Retrieve an API key** (masked by default; use `--show-full` to reveal):

```sh
llm-cascade key get openai
llm-cascade key get openai --show-full
```

**List key status** for all providers (checks both keyring and env vars):

```sh
llm-cascade key list
```

**Delete an API key** from the keyring:

```sh
llm-cascade key delete openai
```

### Output

- **Text responses** are printed to stdout.
- **Tool call responses** are printed as pretty JSON to stdout.
- **Errors** (including `CascadeError` with the `.json` file path) are printed to stderr with exit code 1.

### Verbosity

Control log output via the `RUST_LOG` environment variable:

```sh
RUST_LOG=debug llm-cascade run -C creative_task -p "Hello"
RUST_LOG=llm_cascade=trace llm-cascade run -C creative_task -p "Hello"
```

---

## Library Usage

Use `llm-cascade` as an async library in any Rust project:

```rust
use llm_cascade::{run_cascade, load_config, db, Conversation, Message, MessageRole};

#[tokio::main]
async fn main() {
    let config = load_config(&"config.toml".into()).expect("config");
    let conn = db::init_db(&config.database.path).expect("db");

    let conversation = Conversation::new(vec![
        Message::system("You are a concise assistant."),
        Message::user("What is the capital of France?"),
    ]);

    match run_cascade("creative_task", &conversation, &config, &conn).await {
        Ok(response) => {
            println!("Model: {}", response.model);
            println!("Response: {}", response.text_only());
            if let (Some(in), Some(out)) = (response.input_tokens, response.output_tokens) {
                println!("Tokens: {} in / {} out", in, out);
            }
        }
        Err(e) => {
            eprintln!("Cascade failed: {}", e);
        }
    }
}
```

### With tool definitions

```rust
use llm_cascade::{run_cascade, load_config, db, Conversation, Message, ToolDefinition};
use serde_json::json;

let conversation = Conversation::new(vec![
    Message::user("What's the weather in Tokyo?"),
]).with_tools(vec![
    ToolDefinition {
        name: "get_weather".into(),
        description: "Get current weather for a location".into(),
        parameters: json!({
            "type": "object",
            "properties": {
                "location": { "type": "string" }
            },
            "required": ["location"]
        }),
    },
]);

let response = run_cascade("creative_task", &conversation, &config, &conn).await?;
```

### Key types

| Type | Description |
|------|-------------|
| `Conversation` | Holds `messages: Vec<Message>` and optional `tools: Vec<ToolDefinition>` |
| `Message` | A single message with `role` (`System`/`User`/`Assistant`/`Tool`), `content`, and optional `tool_call_id` |
| `ToolDefinition` | Tool name, description, and JSON Schema parameters |
| `LlmResponse` | Response with `content: Vec<ContentBlock>`, token counts, and model name |
| `ContentBlock` | Either `Text { text }` or `ToolCall { id, name, arguments }` |
| `CascadeError` | Contains cascade name, error message, and absolute path to the persisted `.json` file |
| `ProviderError` | HTTP status, body, optional `retry_after` seconds |

---

## API Reference

### `run_cascade`

```rust
pub async fn run_cascade(
    cascade_name: &str,
    conversation: &Conversation,
    config: &AppConfig,
    conn: &Connection,
) -> Result<LlmResponse, CascadeError>
```

The core entry point. Iterates through the named cascade's provider entries, skipping those on cooldown, and returns the first successful `LlmResponse`.

### `db::init_db`

```rust
pub fn init_db(path: &str) -> Result<Connection, String>
```

Opens (or creates) the SQLite database and ensures the schema exists. Expands `~` in the path.

### `db::log_attempt`

```rust
pub fn log_attempt(
    conn: &Connection,
    cascade_name: &str,
    provider_model: &str,
    http_status: Option<u16>,
    latency_ms: u64,
    input_tokens: Option<u32>,
    output_tokens: Option<u32>,
)
```

Inserts a row into the `attempt_log` table.

### `db::is_on_cooldown` / `db::set_cooldown`

```rust
pub fn is_on_cooldown(conn: &Connection, provider_model: &str) -> bool
pub fn set_cooldown(conn: &Connection, provider_model: &str, cooldown_until: &str)
```

Query and update the `cooldown` table. Timestamps are RFC 3339 strings.

### `load_config`

```rust
pub fn load_config(path: &Path) -> Result<AppConfig, String>
```

Reads and parses the TOML configuration file.

---

## Cooldown & Backoff Behavior

| Scenario | Cooldown Duration |
|----------|------------------|
| HTTP 429 with `retry-after` header | Value from header (seconds) |
| HTTP 429 without header | 30 s (doubles per consecutive failure, max 1 h) |
| Other HTTP error (4xx/5xx) | 30 s base, exponential doubling |
| Successful response | No cooldown set |

Cooldowns are **per entry** (e.g., `openai/gpt-4o` can be on cooldown while `openai/gpt-3.5-turbo` stays active) and **persisted in SQLite** so separate CLI invocations share the same state.

---

## Roadmap

- [ ] Streaming response support
- [ ] Configurable per-provider timeouts
- [ ] Token budget limits per cascade
- [ ] Retry with modified parameters (e.g., lower temperature)
- [ ] Prometheus metrics export
- [ ] Web dashboard for cooldown/attempt monitoring
- [ ] Additional native providers (Mistral, Cohere, AWS Bedrock, Azure OpenAI)
- [x] Published crate on crates.io

---

## Contributing

Contributions are welcome! This is an open-source project under the MIT license.

### Getting started

1. Fork the repository
2. Clone your fork: `git clone https://github.com/paluigi/llm-cascade.git`
3. Create a branch: `git checkout -b feature/your-feature`
4. Build and test: `cargo build && cargo clippy -- -D warnings`

### Making changes

- Follow the existing code style (no comments unless necessary, concise naming)
- Ensure `cargo clippy -- -D warnings` passes with zero warnings
- Update this README if you change public API or configuration

### Submitting

1. Push to your fork: `git push origin feature/your-feature`
2. Open a Pull Request against the `main` branch
3. Describe your changes and the motivation behind them

### Reporting issues

Use the [GitHub issue tracker](https://github.com/paluigi/llm-cascade/issues) to report bugs, request features, or ask questions. Please include:

- Rust version (`rustc --version`)
- OS and version
- Minimal reproduction steps
- Relevant log output (with `RUST_LOG=debug`)

---

## License

[MIT](LICENSE) — Copyright (c) 2026 Luigi Palumbo