Pre-requisites
Before installing rig, make sure you have the Rust programming language installed.
Installing Rig
To install rig, you can do so with the following command in a Rust project (in your terminal):
cargo add rig-core tokioThis will add the rig-core dependency in your Rust project, as well as Tokio which is an asynchronous Rust runtime which you will need to use rig.
Basic Usage
Below is a simple example of how you can use rig to prompt OpenAI’s GPT-4 model:
use rig::client::{CompletionClient, ProviderClient};
use rig::completion::Prompt;
use rig::providers::openai;
#[tokio::main]
async fn main() -> Result<(), anyhow::Error> {
// Create OpenAI client
let client = openai::Client::from_env();
// Create agent with a single context prompt
let comedian_agent = client
.agent("gpt-5.2")
.preamble("You are a comedian here to entertain the user using humour and jokes.")
.build();
// Prompt the agent and print the response
let response = comedian_agent.prompt("Entertain me!").await?;
println!("{response}");
Ok(())
}To run this, you will need an OpenAI API key as an environment variable (OPENAI_API_KEY). Then simply use cargo run and you’ll see your response!
Interested in using other providers like DeepSeek or Claude? Check out our list of providers that we support.