grok_api 0.1.71

Rust client library for the Grok AI API (xAI)
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
# grok_api


[![Crates.io](https://img.shields.io/crates/v/grok_api.svg)](https://crates.io/crates/grok_api)
[![Documentation](https://docs.rs/grok_api/badge.svg)](https://docs.rs/grok_api)
[![License](https://img.shields.io/crates/l/grok_api.svg)](https://github.com/microtech/grok-api#license)
[![Buy Me a Coffee](https://img.shields.io/badge/Buy%20Me%20a%20Coffee-Cobble-yellow?logo=buy-me-a-coffee)](https://www.buymeacoffee.com/Cobble)

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`:

```toml
[dependencies]
grok_api = "0.1"
tokio = { version = "1", features = ["full"] }
```

### Simple Example


```rust
use grok_api::{GrokClient, Result};

#[tokio::main]

async fn main() -> Result<()> {
    let client = GrokClient::new("your-api-key")?;

    let response = client
        .chat("What is Rust?", None)
        .await?;

    println!("Response: {}", response);
    Ok(())
}
```

### Conversation with History


```rust
use grok_api::{GrokClient, ChatMessage, Result};

#[tokio::main]

async fn main() -> Result<()> {
    let client = GrokClient::new("your-api-key")?;

    let messages = vec![
        ChatMessage::system("You are a helpful Rust expert."),
        ChatMessage::user("How do I create a Vec?"),
    ];

    let response = client
        .chat_with_history(&messages)
        .temperature(0.7)
        .max_tokens(1000)
        .model("grok-4.3")   // recommended default (replaces grok-4-1-fast-reasoning)
        .send()
        .await?;

    println!("Response: {}", response.content().unwrap_or(""));
    println!("Tokens used: {}", response.usage.total_tokens);

    Ok(())
}
```

### Advanced Configuration


```rust
use grok_api::{GrokClient, Result};

#[tokio::main]

async fn main() -> Result<()> {
    let client = GrokClient::builder()
        .api_key("your-api-key")
        .timeout_secs(60)
        .max_retries(5)
        .base_url("https://custom-endpoint.com")  // optional
        .build()?;

    // Use client...

    Ok(())
}
```

---

## API Key


Get your API key from [x.ai/api](https://x.ai/api).

Set it as an environment variable:

```bash
export GROK_API_KEY="your-api-key-here"
```

Or pass it directly to the client:

```rust
let client = GrokClient::new("your-api-key")?;
```

---

## Available Models


> Last synced with xAI API โ€” **May 2026**
> Call `GET https://api.x.ai/v1/models` with 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]#migration-guide-016--017 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_20MultiAgent0309` do **not** support
> `presence_penalty`, `frequency_penalty`, `stop`, or `reasoning_effort`.
> `logprobs` is 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


```rust
use grok_api::models::Model;

// Type-safe โ€” no typos
let model = Model::Grok4_3;  // new recommended default
println!("{}", model.as_str()); // "grok-4.3"

// Guard pure reasoning models against unsupported params
if model.is_reasoning_model() {
    // Do NOT set frequency_penalty or presence_penalty
}

// grok-4.3 supports configurable reasoning effort
if model.supports_reasoning_effort() {
    println!("Can set reasoning_effort = low | medium | high");
}

// Check context window
if let Some(ctx) = model.context_window() {
    println!("Context: {} tokens", ctx);  // 1_000_000 for grok-4.3
}

// Check capabilities
println!("Language model:    {}", model.is_language_model());
println!("Image model:       {}", model.is_image_model());
println!("Video model:       {}", model.is_video_model());
println!("Supports logprobs: {}", model.supports_logprobs());

// Parse from a string (e.g. from config / env var)
let m = Model::parse("grok-4.3").expect("unknown model");

// All active (non-deprecated) models
for m in Model::all() {
    println!("{}", m);
}

// All models including deprecated ones (for migration tooling)
for m in Model::all_including_deprecated() {
    println!("{}", m);
}
```

---

## Error Handling


```rust
use grok_api::{GrokClient, Error};

match client.chat("Hello", None).await {
    Ok(response)               => println!("Success: {}", response),
    Err(Error::Authentication) => eprintln!("Invalid API key"),
    Err(Error::RateLimit)      => eprintln!("Rate limit exceeded โ€” back off and retry"),
    Err(Error::Network(msg))   => eprintln!("Network error: {}", msg),
    Err(Error::Timeout(secs))  => eprintln!("Timeout after {} seconds", secs),
    Err(e)                     => eprintln!("Other error: {}", e),
}
```

### Retry Logic


Network errors are automatically retried with exponential backoff:

```rust
let client = GrokClient::builder()
    .api_key("your-api-key")
    .max_retries(5)   // 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:

```rust
use grok_api::{GrokClient, ChatMessage};
use serde_json::json;

let tools = vec![
    json!({
        "type": "function",
        "function": {
            "name": "get_weather",
            "description": "Get current weather for a location",
            "parameters": {
                "type": "object",
                "properties": {
                    "location": {
                        "type": "string",
                        "description": "City name"
                    }
                },
                "required": ["location"]
            }
        }
    })
];

let messages = vec![
    ChatMessage::user("What's the weather in San Francisco?")
];

let response = client
    .chat_with_history(&messages)
    .model("grok-4.3")
    .tools(tools)
    .send()
    .await?;

if response.has_tool_calls() {
    for call in response.tool_calls().unwrap() {
        println!("Tool: {}", call.function.name);
        println!("Args: {}", call.function.arguments);
        // parse args โ†’ call your function โ†’ feed result back
        let result = "Sunny, 18 ยฐC";
        messages.push(ChatMessage::tool(result, &call.id));
    }
}
```

---

## 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

```rust
use grok_api::{GrokClient, Error};

let client = GrokClient::builder()
    .api_key("your-api-key")
    .timeout_secs(60)   // longer timeout for satellite latency
    .max_retries(5)     // more retries for intermittent drops
    .build()?;

match client.chat("Hello", None).await {
    Ok(response) => println!("Success: {}", response),
    Err(e) if e.is_starlink_drop() => {
        eprintln!("Starlink connection dropped โ€” all retries exhausted");
    }
    Err(e) => eprintln!("Error: {}", e),
}
```

---

## Examples


```bash
# Set your API key

export GROK_API_KEY="your-api-key"

# Simple single-turn chat

cargo run --example simple_chat

# Multi-turn conversation

cargo run --example conversation

# Streaming responses

cargo run --example streaming

# Function / tool calling

cargo run --example tools_example

# Video / multimodal

cargo run --example video_chat
```

---

## Cargo Features


| Feature | Default | Description |
|---|---|---|
| `retry` | โœ… yes | Automatic retry with exponential backoff |
| `starlink` | โŒ no | Extra optimisations for satellite connections |

```toml
[dependencies]
grok_api = { version = "0.1", features = ["starlink"] }
```

---

## Testing


```bash
# Unit + doc tests (no API key needed)

cargo test

# With debug logging

RUST_LOG=debug cargo test

# Clippy

cargo clippy -- -D warnings
```

---

## Minimum Supported Rust Version (MSRV)


This crate requires **Rust 1.85** or later (Rust 2024 edition).

Update your toolchain with:

```bash
rustup update stable
```

---

## Documentation


Full API documentation is available at [docs.rs/grok_api](https://docs.rs/grok_api).

---

## Contributing


Contributions are welcome! Please feel free to submit a Pull Request.

1. Fork the repository
2. Create your feature branch (`git checkout -b feature/amazing-feature`)
3. Commit your changes (`git commit -m 'Add some amazing feature'`)
4. Push to the branch (`git push origin feature/amazing-feature`)
5. Open a Pull Request

---

## License


This project is licensed under either of:

- Apache License, Version 2.0 ([LICENSE-APACHE]LICENSE-APACHE or <http://www.apache.org/licenses/LICENSE-2.0>)
- MIT License ([LICENSE-MIT]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


```rust
// Before (0.1.6)
let model = Model::Grok4_1FastReasoning; // โš ๏ธ deprecated in 0.1.7

// After (0.1.7)
let model = Model::Grok4_3;             // โœ… new recommended default
```

```rust
// Before (0.1.6)
let model = Model::Grok4_1FastNonReasoning; // โš ๏ธ deprecated in 0.1.7

// After (0.1.7)
let model = Model::Grok4_20NonReasoning;    // โœ… stable 4.20 non-reasoning alias
```

```rust
// Before (0.1.6)
let model = Model::GrokImagineImagePro; // โš ๏ธ deprecated in 0.1.7

// After (0.1.7)
let model = 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


- [Crates.io]https://crates.io/crates/grok_api
- [Documentation]https://docs.rs/grok_api
- [GitHub Repository]https://github.com/microtech/grok-api
- [xAI Official Site]https://x.ai
- [Buy Me a Coffee โ˜•]https://www.buymeacoffee.com/Cobble

---

## Changelog


See [CHANGELOG.md](CHANGELOG.md) for full release notes and version history.

---

## Support


For bugs and feature requests, please [open an issue](https://github.com/microtech/grok-api/issues).

---

*Made with โค๏ธ and Rust โ€” [John McConnell](mailto:john.microtech@gmail.com)*