pub struct GrokLlm { /* private fields */ }Expand description
XAI Grok LLM implementation.
Provides access to Grok models through the XAI API. The API is OpenAI-compatible with some differences in token counting.
§Authentication
The API key can be provided explicitly or loaded from the XAI_API_KEY
environment variable via from_env.
§Model Selection
Common model names:
grok-beta- Grok Betagrok-vision-beta- Grok Vision Beta
§Token Counting Bug
Important: Grok has a known bug where reasoning_tokens in
completion_tokens_details is NOT included in the top-level completion_tokens.
This implementation automatically corrects this by adding reasoning_tokens
to completion_tokens for accurate usage tracking.
§Examples
use radkit::models::providers::GrokLlm;
use radkit::models::{BaseLlm, Thread};
// From environment variable
let llm = GrokLlm::from_env("grok-beta")?;
// With explicit API key
let llm = GrokLlm::new("grok-beta", "xai-...");
// Generate content
let thread = Thread::from_user("Explain quantum computing");
let response = llm.generate_content(thread, None).await?;
println!("{}", response.content().first_text().unwrap_or("No response"));Implementations§
Source§impl GrokLlm
impl GrokLlm
Sourcepub const API_KEY_ENV: &str = "XAI_API_KEY"
pub const API_KEY_ENV: &str = "XAI_API_KEY"
Environment variable name for the XAI API key.
Sourcepub fn from_env(model_name: impl Into<String>) -> AgentResult<Self>
pub fn from_env(model_name: impl Into<String>) -> AgentResult<Self>
Creates a new Grok LLM instance loading API key from environment.
Reads the API key from the XAI_API_KEY environment variable.
§Arguments
model_name- The model to use (e.g., “grok-beta”)
§Errors
Returns an error if the environment variable is not set or is empty.
§Examples
let llm = GrokLlm::from_env("grok-beta")?;Sourcepub fn with_base_url(self, base_url: impl Into<String>) -> Self
pub fn with_base_url(self, base_url: impl Into<String>) -> Self
Sets a custom base URL for the API endpoint.
Sourcepub const fn with_max_tokens(self, max_tokens: u32) -> Self
pub const fn with_max_tokens(self, max_tokens: u32) -> Self
Sets the maximum number of tokens to generate.
Sourcepub const fn with_temperature(self, temperature: f32) -> Self
pub const fn with_temperature(self, temperature: f32) -> Self
Sets the temperature for generation (0.0 to 2.0).
Higher values produce more random outputs.