Expand description
§rust_llm
One API for chat, tools, agents, structured output, streaming, embeddings, images, batches, MCP, and judgments across LLM providers.
let mut chat = rust_llm::chat_with("claude-opus-5-5")?;
let answer = chat.ask("What's the best way to learn Rust?").await?;
println!("{}", answer.content());§Features
Chat: conversations with instructions, model switching, temperature, provider options, headers, request hooks, callbacks, fallbacks, and cancellation. Start withchat()orchat_with.Tool: tools the model can call, withParameterdeclarations or aschemarsschema (schema_for), approvals, and progress.ProviderTools run on the provider.Agent: a chat configuration declared once and reused.- Streaming (
Chat::ask_stream), structured output (Chat::with_schema_for), and extended thinking (ThinkingConfig). - Attachments (
Attachment) and provider Files APIs (upload,download). embed,paint,batch()andembed_later,judge()andJudge.- MCP servers (
mcp,Mcp). - Per-attempt
TokensandCoston every result.
Providers: OpenAI, Anthropic, Gemini, DeepSeek, Mistral, OpenRouter, xAI, Perplexity, Ollama,
Ollama Cloud, GPUStack, Hetzner, and TypeSafe. Configure them with configure; keys such as
OPENAI_API_KEY are read from the environment automatically.
§Guides
The user guides cover each
feature in depth. RustLLM is a port of RubyLLM 2.1, and
keeps its names where Rust allows (ask, ask_later, complete, with_tools,
before_tool_call, …), with ?-suffixed predicates spelled is_*;
RubyLLM vs RustLLM
maps one to the other.
Re-exports§
pub use agent::Agent;pub use attachment::Attachment;pub use attachment::Resolution;pub use batch::Batch;pub use batch::BatchResult;pub use batch::BatchStatus;pub use batch::EmbeddingRequest;pub use batch::batch;pub use batch::embed_later;pub use cached_content::CacheOptions;pub use cached_content::CachedContent;pub use cached_content::Ttl;pub use cached_content::cache;pub use chat::CancelHandle;pub use chat::Chat;pub use chat::Fallback;pub use chat::FallbackAttempt;pub use config::Config;pub use config::config;pub use config::configure;pub use context::Context;pub use context::context;pub use cost::Cost;pub use embedding::EmbedOptions;pub use embedding::Embedding;pub use embedding::SparseVectors;pub use embedding::Vectors;pub use embedding::embed;pub use error::Error;pub use error::ErrorKind;pub use error::Result;pub use files::DownloadedFile;pub use files::FileOptions;pub use files::UploadOptions;pub use files::UploadedFile;pub use files::download;pub use files::upload;pub use image::Image;pub use image::Images;pub use image::PaintOptions;pub use image::paint;pub use instrumentation::Instrumenter;pub use instrumentation::instrument;pub use judge::Answer;pub use judge::Dynamic;pub use judge::Judge;pub use judge::JudgeOptions;pub use judge::Judgment;pub use judge::QuestionType;pub use judge::judge;pub use judge::list_judgment_models;pub use mcp::InputRequest;pub use mcp::Mcp;pub use mcp::McpResult;pub use mcp::McpTool;pub use message::Chunk;pub use message::Citation;pub use message::FinishReason;pub use message::Message;pub use message::Role;pub use message::Thinking;pub use message::ToolCall;pub use message::UsageEntry;pub use message::UsageStatus;pub use model::Model;pub use models::models;pub use moderation::ModerateOptions;pub use moderation::Moderation;pub use moderation::ModerationInput;pub use moderation::ModerationResult;pub use moderation::moderate;pub use ocr::Ocr;pub use ocr::OcrOptions;pub use ocr::Page as OcrPage;pub use ocr::ocr;pub use progress::Progress;pub use prompt::Prompt;pub use prompt::render_prompt;pub use protocols::Caching;pub use protocols::ToolCalls;pub use protocols::ToolChoice;pub use provider_tools::ProviderTool;pub use providers::ProtocolName;pub use providers::Provider;pub use request_shape::RequestShape;pub use rerank::Rerank;pub use rerank::RerankOptions;pub use rerank::RerankResult;pub use rerank::rerank;pub use search_results::SearchResults;pub use speech::SpeakOptions;pub use speech::Speech;pub use speech::SpeechChunk;pub use speech::speak;pub use speech::speak_stream;pub use thinking::Display as ThinkingDisplay;pub use thinking::ThinkingConfig;pub use tokenization::Tokenization;pub use tokenization::TokenizeOptions;pub use tokenization::tokenize;pub use tokens::Tokens;pub use tool::FnTool;pub use tool::Parameter;pub use tool::Tool;pub use tool::ToolError;pub use tool::ToolResult;pub use tool::schema_for;pub use transcription::TranscribeOptions;pub use transcription::Transcription;pub use transcription::TranscriptionChunk;pub use transcription::transcribe;pub use transcription::transcribe_stream;pub use video::AnimateOptions;pub use video::Video;pub use video::VideoJob;pub use video::VideoSource;pub use video::VideoStatus;pub use video::animate;pub use video::animate_later;pub use workflow::Workflow;pub use workflow::workflow;
Modules§
- accounting
- Port of
lib/ruby_llm/accounting/usage.rb: the per-attempt usageTracker, theEntryreaders it relies on (theUsageEntrystruct itself lives inmessage.rs), and theAccounting::Usage::Resultstorage shared by results that carry attempt accounting. - agent
- Port of
lib/ruby_llm/agent.rb: reusable assistants declared once. - attachment
- Port of
lib/ruby_llm/attachment.rbandfiles/mime_type.rb. - batch
- Port of
lib/ruby_llm/batch.rb,lib/ruby_llm/embedding_request.rb, the batch methods oflib/ruby_llm/provider.rb, and the batch protocols:protocols/anthropic/batches.rb,protocols/openai/batches.rb(withresponses/batches.rb,chat_completions/batches.rb,chat_completions/embedding_batches.rb),protocols/gemini/batches.rb,protocols/gemini/embedding_batches.rb,protocols/openrouter/batches.rb,providers/mistral/chat_completions/batches.rb, andproviders/xai/chat_completions/batches.rb. - cached_
content - Port of
lib/ruby_llm/cached_content.rb,RubyLLM.cache, andlib/ruby_llm/protocols/gemini/caches.rb: provider-side prompt caches (Gemini’scachedContents). - chat
- Port of
lib/ruby_llm/chat.rb: the conversation loop. - config
- Port of
lib/ruby_llm/configuration.rb. - context
- Port of
lib/ruby_llm/context.rbandRubyLLM.context: an isolated configuration scope with the same entry points as the crate root, reading its ownConfigcopy instead of the global. - cost
- Port of
lib/ruby_llm/cost.rbfor text tokens. - embedding
- Port of
lib/ruby_llm/embedding.rbwith the OpenAI-compatible and Gemini embedding protocols. - error
- Port of
lib/ruby_llm/error.rband the status mapping inlib/ruby_llm/transport/error_middleware.rb. - evaluation
- Evaluations: run your application against a dataset of cases and grade each result. Port of
lib/ruby_llm/evaluation.rbandlib/ruby_llm/evaluation/*(case, dataset, evaluator, evidence, report, result, reviewer, runner, trial, assertions). - files
- Port of
lib/ruby_llm/uploaded_file.rb,downloaded_file.rb,protocols/files.rbwithprotocols/{openai,anthropic,gemini,mistral,xai,deepseek,openrouter,perplexity}/files.rb, and the auto-upload half ofprotocol.rb(preprocess_message,preprocess_attachment,provider_upload) with each protocol’s thresholds from itschat.rb. - image
- Port of
lib/ruby_llm/image.rb(RubyLLM.paint),Protocol#paint/post_image/parse_image_responsesinlib/ruby_llm/protocol.rb, and the image seams inprotocols/chat_completions/images.rb(OpenAI Images API),protocols/gemini/images.rb,providers/xai/images.rb, andproviders/openrouter/images.rb. - instrumentation
- Port of
lib/ruby_llm/support/instrumentation.rb,RubyLLM.instrument, andconfig.instrumenter. - judge
- Judgments: typed questions about your data, answered by a System One model such as TypeSafe’s
Jev, a local decision model through Ollama, or OpenAI Decisions. Port of
lib/ruby_llm/judge.rb,judge/question.rb,judgment.rb,probability.rb,choice.rb,score.rb,protocols/system_one/*.rb, andproviders/ollama/judgments.rb; OpenAI Decisions is indecisions. - mcp
- Port of
lib/ruby_llm/mcp.rb: a client for a Model Context Protocol server. - message
- Port of
message.rb,chunk.rb,tool_call.rb,thinking.rb,citation.rb,server_tool_call.rb, and the usageEntryfromaccounting/usage.rb. - model
- Port of
lib/ruby_llm/model.rbandlib/ruby_llm/model/pricing*.rb. - models
- Port of
lib/ruby_llm/models.rb,models/lookup.rb, andmodels/aliases.rb: the bundled registry, alias resolution, and provider preference. Refreshing lives inrefresh, registry files inregistry. - moderation
- Port of
lib/ruby_llm/moderation.rb(RubyLLM.moderate),Protocol#moderate, andlib/ruby_llm/protocols/chat_completions/moderation.rb, which every Chat Completions and Responses provider inherits. - ocr
- Port of
lib/ruby_llm/ocr.rb(RubyLLM.ocr),Protocol#ocr, andlib/ruby_llm/providers/mistral/ocr.rb, the only OCR seam among the providers RustLLM supports. - open_
telemetry - Port of
lib/ruby_llm/open_telemetry.rbandlib/ruby_llm/open_telemetry/attributes.rb: optional OpenTelemetry tracing through the instrumentation events. - progress
- Port of
lib/ruby_llm/progress.rb,support/progress_reporter.rb, andsupport/cancellation.rb. - prompt
- Port of
lib/ruby_llm/prompt.rbandlib/ruby_llm/prompt/context.rb: prompt templates underapp/prompts, rendered with locals. - protocols
- Port of
lib/ruby_llm/protocol.rb,protocol/streaming.rb, andprotocol/stream_accumulator.rb. - provider_
tools - Port of
lib/ruby_llm/tools/provider_tools.rb,Protocol#apply_provider_tools, and theSERVER_TOOL_ALIASEStables of the protocols this crate speaks: tools that run on the provider’s servers, such as web search, code execution, or a remote MCP server. - providers
- Port of
lib/ruby_llm/provider.rbandlib/ruby_llm/providers/*.rb: where each service lives, how it authenticates, and which wire protocol it speaks for a model. - request_
shape - Port of
lib/ruby_llm/request_shape.rbandlib/ruby_llm/request_shape/{part,turn,tool_round,problem}.rb. - rerank
- Port of
lib/ruby_llm/rerank.rb(RubyLLM.rerank),Protocol#rerank, andlib/ruby_llm/protocols/chat_completions/rerank.rb, the Jina-style endpoint OpenRouter and GPUStack serve. Cohere’s and Bedrock’s rerank protocols belong to providers RustLLM does not port. - search_
results - Port of
lib/ruby_llm/search_results.rb: documents a tool returns so the model can cite each source. Protocols that support it (Anthropic) render them in their citation format; cited passages come back onMessage::citations. - speech
- Port of
lib/ruby_llm/speech.rb(RubyLLM.speak),speech_chunk.rb,Protocol#speak/stream_speech/stream_speech_responseinlib/ruby_llm/protocol.rb,protocol/binary_streaming.rb, and the speech seams inprotocols/chat_completions/speech.rb(OpenAI and every OpenAI-compatible provider),protocols/gemini/speech.rb,providers/mistral/speech.rb,providers/xai/speech.rb,providers/openrouter/speech.rb, andproviders/gpustack/speech.rb. - support
- Port of
lib/ruby_llm/support/process_cache.rb. - thinking
- Port of
Thinking::ConfigandThinking::Controlsfromlib/ruby_llm/thinking.rb:with_thinkingoptions resolved against what the model’s registry entry accepts. - tokenization
- Port of
lib/ruby_llm/tokenization.rb(RubyLLM.tokenize) withprotocols/xai/tokenization.rbandprotocols/gpustack/tokenization.rb, plus the input-token counting seams behindChat#count_tokens:render_count_tokens_payload/count_tokens_url/parse_count_tokens_responseinprotocols/anthropic/chat.rb,protocols/gemini/chat.rb, andprotocols/responses/token_counting.rb. - tokens
- Port of
lib/ruby_llm/tokens.rb. - tool
- Port of
lib/ruby_llm/tool.rb. - tool_
search - Tool search with deferred tool loading:
lib/ruby_llm/chat/tool_search.rb, theapply_tool_search/tool_search_block?halves ofprotocols/anthropic/tools.rbandprotocols/responses/tools.rb, and each protocol’sportable_raw_content(protocols/anthropic/chat.rb,protocols/responses/chat.rb). - transcription
- Port of
lib/ruby_llm/transcription.rb(RubyLLM.transcribe),transcription_chunk.rb,Provider#transcribeandProtocol#transcribe/build_audio_file_part/audio_file_nameinlib/ruby_llm/protocol.rb, and the transcription seams inprotocols/chat_completions/transcription.rb(OpenAI and every OpenAI-compatible provider),protocols/gemini/transcription.rb(generateContent),protocols/gemini/file_transcription.rbwithprotocols/interactions/transcription.rb(Gemini’s dedicated transcription models),protocols/openrouter/transcription.rb,providers/mistral/transcription.rb,providers/xai/transcription.rb, andproviders/gpustack/transcription.rb. WebSocket transcription lives ingemini_live(protocols/gemini/live_transcription.rb) andxai_streaming(protocols/xai/streaming_transcription.rb), overWebsocketConnection, withwav_audio(transcription/wav_audio.rb) reading the PCM format. - transport
- Port of
lib/ruby_llm/transport/connection.rb,error_middleware.rb, and the SSE handling inprotocol/streaming.rb. - video
- Port of
lib/ruby_llm/video.rb(RubyLLM.animate),lib/ruby_llm/video_job.rb(RubyLLM.animate_later),Protocol#animate_later/refresh_video_job, and the video seams inproviders/xai/videos.rb,providers/openrouter/videos.rb,protocols/gemini/videos.rb, andprotocols/gpustack/videos.rb. - workflow
- Port of
lib/ruby_llm/workflow.rbandRubyLLM.workflow: named, instrumented regions of ordinary async code.
Macros§
- evaluates
- One
#[test]per case:evaluates!(formatting_evaluation(); correct => "correct", regression => "regression");where the first expression builds theEvaluation. Each failing test prints the report.
Constants§
- VERSION
RubyLLM::VERSIONof the port’s source.
Functions§
- chat
RubyLLM.chatwith the configured default model.- chat_
with RubyLLM.chat(model:).- count_
tokens RubyLLM.count_tokens(text, model:, provider:): input tokens for one user message.