Introduction
Rig is a Rust library for building LLM-powered applications and agents. It gives you one API over 20+ model providers (OpenAI, Anthropic, Gemini, Cohere, Ollama, and more), vector stores, and tools, so you can wire up an agent in a few lines and grow it into a production system without changing frameworks.
Why Rig
Section titled “Why Rig”- Type-safe by construction: tool arguments, structured outputs, and generation options are ordinary Rust types, so the compiler checks them before you hit the network.
- One API across providers: the same model, agent, and embedding types work for every provider. Switching backends means changing a client, not your code.
- Portable core, optional runtime: provider, tool, and vector-store contracts live in a small core; the agent loop sits on top of it. Use the agent, or call models directly and write your own loop.
- Runs where Rust runs: ship one native binary, or compile the core and agent runtime to WebAssembly for the browser.
- Stay in your stack: if your codebase is already Rust, build AI features with the same toolchain and type system you use everywhere else.
Your first agent
Section titled “Your first agent”An agent is a model plus a system prompt (a “preamble”) and, optionally, tools and context. Here’s a complete program that creates one and prompts it:
use rig::prelude::*;use rig::providers::openai::{self, OpenAI};
#[tokio::main]async fn main() -> Result<(), anyhow::Error> { // Reads the OPENAI_API_KEY environment variable. let model = OpenAI::from_env()?.completion(openai::GPT_5_5);
// Build an agent from a model and a system prompt. let agent = AgentBuilder::new(model) .preamble("You are a helpful assistant.") .build();
// Send a prompt and await the response. let answer = agent.prompt("Who are you?").await?; println!("{}", answer.output());
Ok(())}What you get
Section titled “What you get”- Completions and embeddings across providers, with typed generation options (reasoning effort, prompt caching, service tier) that each provider maps or refuses explicitly.
- Agents with preambles, tools, dynamic context, conversation memory, and lifecycle hooks, plus a serializable run state you can step, pause, and resume.
- Retrieval-augmented generation (RAG) over an in-memory store or one of a dozen vector-database integrations.
- Structured extraction of typed data from unstructured text.
- Streaming of text, reasoning, and tool-call arguments as they arrive.
- Production tooling: typed errors, token usage and cost accounting, OpenTelemetry-compatible tracing, recording and replay, and mock models for offline testing.
Where to next
Section titled “Where to next”InstallationAdd Rig to your project and pick its features.
QuickstartBuild and run your first agent end-to-end.
ArchitectureHow the rig facade, rig-core, and rig-agent fit together.
Core ConceptsProviders, models, agents, tools, RAG, and workflows.
See also
Section titled “See also”- Guides: task-focused tutorials for real applications
- API reference on docs.rs
- Rig on GitHub
- awesome-rig examples
