grok_api
A Rust client library for the Grok AI API (xAI). Simple, robust, and production-ready.
Features
- ๐ Easy to use โ Simple async API with builder pattern
- ๐ Automatic retries โ Built-in retry logic for transient failures
- ๐ก๏ธ Robust error handling โ Comprehensive error types with detailed messages
- ๐ Network resilient โ Optimised for challenging conditions (Starlink, satellite connections)
- ๐ง Flexible configuration โ Customise timeouts, retries, and more
- ๐ ๏ธ Tool / Function calling โ Full support for function calling and agentic workflows
- ๐ง Reasoning-aware โ Capability helpers guard against sending unsupported params to reasoning models
- ๐ฆ Rust 2024 edition โ Built on the latest edition with MSRV 1.85
Quick Start
Add this to your Cargo.toml:
[]
= "0.1"
= { = "1", = ["full"] }
Simple Example
use ;
async
Conversation with History
use ;
async
Advanced Configuration
use ;
async
API Key
Get your API key from x.ai/api.
Set it as an environment variable:
Or pass it directly to the client:
let client = new?;
Available Models
Last synced with xAI API โ May 2026 Call
GET https://api.x.ai/v1/modelswith your key to see your account's live list.
โ ๏ธ Retirements effective 2026-05-15 12:00 PT โ the following API strings will stop working:
grok-4-1-fast-reasoning,grok-4-1-fast-non-reasoning,grok-4-0709,grok-code-fast-1,grok-3,grok-imagine-image-pro. See the Migration Guide below.
๐ Grok 4.3 โ New Flagship (1 M token context, May 2026)
| API string | Model variant |
Best for |
|---|---|---|
grok-4.3 |
Model::Grok4_3 |
Recommended default โ fastest & most intelligent; replaces all retiring 4.1/4/3 models |
Grok 4.3 highlights:
- 1,000,000 token context window
- Configurable
reasoning_effort:"low"/"medium"/"high"- $1.25 / 1 M input ยท $2.50 / 1 M output
- For lower latency without reasoning, set
reasoning_effort = "low"
๐ Grok 4.20 โ Flagship (2 M token context)
| API string | Model variant |
Best for |
|---|---|---|
grok-4.20-0309-reasoning |
Model::Grok4_20_0309Reasoning |
Complex reasoning, maths, science, multi-step agentic tasks |
grok-4.20-non-reasoning |
Model::Grok4_20NonReasoning |
Fast non-reasoning, high-throughput, lower latency |
grok-4.20-0309-non-reasoning |
Model::Grok4_20_0309NonReasoning |
Dated variant of the 4.20 standard model |
grok-4.20-multi-agent-0309 |
Model::Grok4_20MultiAgent0309 |
Deep research, complex workflows, multi-agent pipelines |
Note:
Grok4_20_0309Reasoning/Grok4_20MultiAgent0309do not supportpresence_penalty,frequency_penalty,stop, orreasoning_effort.logprobsis silently ignored by all Grok 4.20 models.
๐๏ธ Legacy (Grok 3 Mini โ still active)
| API string | Model variant |
Notes |
|---|---|---|
grok-3-mini |
Model::Grok3Mini |
Efficient smaller model; 131 K context; not retiring May 2026 |
๐ผ๏ธ Image Generation
| API string | Model variant |
Notes |
|---|---|---|
grok-imagine-image |
Model::GrokImagineImage |
Standard image generation |
๐ฅ Video Generation
| API string | Model variant |
Notes |
|---|---|---|
grok-imagine-video |
Model::GrokImagineVideo |
Video generation |
๐จ Deprecated models (retire 2026-05-15)
These variants still compile but emit a Rust deprecation warning. Migrate before 2026-05-15.
| Deprecated API string | Model variant |
Use instead |
|---|---|---|
grok-4-1-fast-reasoning |
Model::Grok4_1FastReasoning |
Model::Grok4_3 |
grok-4-1-fast-non-reasoning |
Model::Grok4_1FastNonReasoning |
Model::Grok4_20NonReasoning |
grok-4-0709 |
Model::Grok4_0709 |
Model::Grok4_3 |
grok-3 |
Model::Grok3 |
Model::Grok4_3 |
grok-code-fast-1 |
Model::GrokCodeFast1 |
Model::Grok4_3 |
grok-imagine-image-pro |
Model::GrokImagineImagePro |
Model::GrokImagineImage |
Recommended models at a glance
| Use case | Recommended model |
|---|---|
| Everyday / CLI default | grok-4.3 |
| Heavy coding tasks | grok-4.3 |
| Maximum reasoning | grok-4.20-0309-reasoning |
| Speed + high volume | grok-4.20-non-reasoning |
| Agentic / multi-step | grok-4.20-multi-agent-0309 |
| Image generation | grok-imagine-image |
Using the Model enum
use Model;
// Type-safe โ no typos
let model = Grok4_3; // new recommended default
println!; // "grok-4.3"
// Guard pure reasoning models against unsupported params
if model.is_reasoning_model
// grok-4.3 supports configurable reasoning effort
if model.supports_reasoning_effort
// Check context window
if let Some = model.context_window
// Check capabilities
println!;
println!;
println!;
println!;
// Parse from a string (e.g. from config / env var)
let m = parse.expect;
// All active (non-deprecated) models
for m in all
// All models including deprecated ones (for migration tooling)
for m in all_including_deprecated
Error Handling
use ;
match client.chat.await
Retry Logic
Network errors are automatically retried with exponential backoff:
let client = builder
.api_key
.max_retries // retry up to 5 times
.build?;
Retryable errors include:
- Network timeouts
- Connection failures
- Server errors (5xx)
- Starlink / satellite network drops
Function Calling / Tools
Full support for Grok's function calling and agentic tool use:
use ;
use json;
let tools = vec!;
let messages = vec!;
let response = client
.chat_with_history
.model
.tools
.send
.await?;
if response.has_tool_calls
Starlink Optimisation
The library includes special handling for Starlink and other satellite connections:
- Automatic detection of connection drops
- Exponential backoff with jitter
- Extended timeout handling
use ;
let client = builder
.api_key
.timeout_secs // longer timeout for satellite latency
.max_retries // more retries for intermittent drops
.build?;
match client.chat.await
Examples
# Set your API key
# Simple single-turn chat
# Multi-turn conversation
# Streaming responses
# Function / tool calling
# Video / multimodal
Cargo Features
| Feature | Default | Description |
|---|---|---|
retry |
โ yes | Automatic retry with exponential backoff |
starlink |
โ no | Extra optimisations for satellite connections |
[]
= { = "0.1", = ["starlink"] }
Testing
# Unit + doc tests (no API key needed)
# With debug logging
RUST_LOG=debug
# Clippy
Minimum Supported Rust Version (MSRV)
This crate requires Rust 1.85 or later (Rust 2024 edition).
Update your toolchain with:
Documentation
Full API documentation is available at docs.rs/grok_api.
Contributing
Contributions are welcome! Please feel free to submit a Pull Request.
- Fork the repository
- Create your feature branch (
git checkout -b feature/amazing-feature) - Commit your changes (
git commit -m 'Add some amazing feature') - Push to the branch (
git push origin feature/amazing-feature) - Open a Pull Request
License
This project is licensed under either of:
- Apache License, Version 2.0 (LICENSE-APACHE or http://www.apache.org/licenses/LICENSE-2.0)
- MIT License (LICENSE-MIT or http://opensource.org/licenses/MIT)
at your option.
Migration Guide: 0.1.6 โ 0.1.7
Six models retire on 2026-05-15 12:00 PT. After that date, the xAI API will return an error for any request that names a retired model.
Quick replacement table
| Old model string | Use this instead |
|---|---|
grok-4-1-fast-reasoning |
grok-4.3 |
grok-4-1-fast-non-reasoning |
grok-4.20-non-reasoning |
grok-4-0709 |
grok-4.3 |
grok-code-fast-1 |
grok-4.3 |
grok-3 |
grok-4.3 |
grok-imagine-image-pro |
grok-imagine-image |
Rust code migration
// Before (0.1.6)
let model = Grok4_1FastReasoning; // โ ๏ธ deprecated in 0.1.7
// After (0.1.7)
let model = Grok4_3; // โ
new recommended default
// Before (0.1.6)
let model = Grok4_1FastNonReasoning; // โ ๏ธ deprecated in 0.1.7
// After (0.1.7)
let model = Grok4_20NonReasoning; // โ
stable 4.20 non-reasoning alias
// Before (0.1.6)
let model = GrokImagineImagePro; // โ ๏ธ deprecated in 0.1.7
// After (0.1.7)
let model = GrokImagineImage; // โ
standard image generation
Deprecated variants still compile (with a warning) and still work until 2026-05-15. This gives you time to migrate at your own pace.
Disclaimer
This is an unofficial library and is not affiliated with, endorsed by, or sponsored by xAI or X Corp.
Links
Changelog
See CHANGELOG.md for full release notes and version history.
Support
For bugs and feature requests, please open an issue.
Made with โค๏ธ and Rust โ John McConnell