llm-stack
Production-ready Rust SDK for LLM providers
Quick Start • Features • Documentation • Examples • Contributing
Overview
llm-stack is a unified Rust interface for building LLM-powered applications. Write against one set of types, swap providers without changing application code.
use ;
use AnthropicProvider;
async
Why llm-stack?
- 🔌 Provider Agnostic — Same code works with Anthropic, OpenAI, Ollama, or any custom provider
- 🛠️ Batteries Included — Tool execution, structured output, streaming, retry logic out of the box
- 🎯 Type Safe —
generate_object::<T>()returnsT, notserde_json::Value - ⚡ Production Ready — Comprehensive error handling, token tracking, cost calculation
- 🧪 Testable —
MockProviderfor unit tests, no network mocks needed - 🦀 Rust Native — Async/await, strong typing, zero-cost abstractions
Quick Start
Add to your Cargo.toml:
[]
= "0.1"
= "0.1" # or llm-stack-openai, llm-stack-ollama
= { = "1", = ["rt-multi-thread", "macros"] }
Set your API key:
# or OPENAI_API_KEY for OpenAI
Run:
use ;
use AnthropicProvider;
async
Features
Core Capabilities
| Feature | Description |
|---|---|
| Unified Provider Trait | Two methods define a provider: generate() + stream() |
| Streaming | First-class async streaming with StreamEvent types |
| Tool Execution | Register handlers, validate inputs, execute in agentic loops |
| Structured Output | generate_object::<T>() with JSON Schema validation |
| Interceptors | Composable retry, timeout, logging, approval gates |
| Usage Tracking | Token counts, cost calculation in microdollars |
| Context Management | Token budget tracking, message truncation |
Provider Support
| Provider | Crate | Models |
|---|---|---|
| Anthropic | llm-stack-anthropic |
Claude 3.5 Sonnet, Claude 3 Opus/Haiku |
| OpenAI | llm-stack-openai |
GPT-4o, GPT-4 Turbo, GPT-3.5 |
| Ollama | llm-stack-ollama |
Llama 3, Mistral, CodeLlama, any local model |
Tool Execution Engine
Build agentic applications with the tool loop:
use ;
use json;
// Define a tool
let mut registry: = new;
registry.register;
// Run the agentic loop
let result = tool_loop.await?;
println!;
Structured Output
Get typed responses with schema validation:
use generate_object;
use Deserialize;
let review: MovieReview = generate_object.await?;
println!;
Interceptors
Add cross-cutting concerns without modifying provider code:
use ;
use Duration;
let registry = new
.with_interceptors;
Documentation
| Guide | Description |
|---|---|
| Quick Start | Get up and running in 5 minutes |
| Architecture | Design principles and module overview |
| Providers | Provider configuration and selection |
| Tools | Tool execution and agentic loops |
| Structured Output | Type-safe LLM responses |
| Interceptors | Retry, timeout, logging, approval |
| Context Window | Token management and truncation |
| Migration Guide | Coming from the llm crate? |
API Reference
Or view on docs.rs.
Examples
Streaming
use StreamExt;
use StreamEvent;
let mut stream = provider.stream.await?;
while let Some = stream.next.await
Multi-Provider Setup
use ;
let registry = new
.register
.register
.register;
// Select at runtime
let provider = registry.get?;
Testing with MockProvider
use mock_for;
async
Crate Map
| Crate | Purpose |
|---|---|
llm-stack |
Traits, types, errors, streaming, tools, interceptors |
llm-stack-anthropic |
Anthropic Claude provider |
llm-stack-openai |
OpenAI GPT provider |
llm-stack-ollama |
Ollama local provider |
Development
Prerequisites
- Rust 1.85+ (2024 edition)
- just — Command runner
Commands
Running Tests
# All tests
# Specific test
# With output
Contributing
We welcome contributions! Please see CONTRIBUTING.md for guidelines.
Quick Checklist
-
just gatepasses (fmt, clippy, tests, docs) - New features have tests
- Public APIs have documentation
- Commit messages follow Conventional Commits
License
Licensed under the Apache License, Version 2.0. See LICENSE for details.
Acknowledgments
Built on the shoulders of giants:
- Tokio — Async runtime
- reqwest — HTTP client
- serde — Serialization
- jsonschema — JSON Schema validation