agentix 0.3.0

Multi-provider LLM agent framework for Rust — streaming, tool calls, DeepSeek, OpenAI, Anthropic, Gemini
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
# agentix

[![crates.io](https://img.shields.io/crates/v/agentix.svg)](https://crates.io/crates/agentix)
[![docs.rs](https://img.shields.io/docsrs/agentix)](https://docs.rs/agentix)
[![license](https://img.shields.io/crates/l/agentix.svg)](https://github.com/ozongzi/agentix/blob/main/LICENSE-MIT)

A Rust framework for building LLM agents and multi-agent pipelines. Supports DeepSeek, OpenAI, Anthropic (Claude), and Google Gemini out of the box — plus any OpenAI-compatible endpoint.

Agents are **actor-style**: send a message, observe a stream of events. Multiple agents wire together into a [`Graph`](#graph--multi-agent-pipelines) via typed channels.

---

## Quickstart

```bash
export DEEPSEEK_API_KEY="sk-..."
```

```toml
[dependencies]
agentix = "0.3"
tokio   = { version = "1", features = ["full"] }
```

```rust
use agentix::Msg;

#[tokio::main]
async fn main() {
    let agent = agentix::deepseek(std::env::var("DEEPSEEK_API_KEY").unwrap())
        .system_prompt("You are a helpful assistant.")
        .max_tokens(1024);

    let mut rx = agent.subscribe();
    agent.send("What is the capital of France?").await;

    while let Ok(msg) = rx.recv().await {
        match msg {
            Msg::Token(t) => print!("{t}"),
            Msg::Done     => break,
            _             => {}
        }
    }
    println!();
}
```

---

## Providers

Four built-in providers, all using the same builder API:

```rust
// DeepSeek  (default model: deepseek-chat)
let agent = agentix::deepseek("sk-...")
    .model("deepseek-reasoner");

// OpenAI  (default model: gpt-4o)
let agent = agentix::openai("sk-...");

// Anthropic / Claude  (default model: claude-opus-4-5)
let agent = agentix::anthropic("sk-ant-...");

// Gemini  (default model: gemini-2.0-flash)
let agent = agentix::gemini("AIza...");

// Any OpenAI-compatible endpoint
use agentix::{Agent, LlmClient};
let agent = Agent::new(LlmClient::openai_compatible(
    "sk-...",
    "https://openrouter.ai/api/v1",
    "meta-llama/llama-3.3-70b-instruct:free",
));
```

---

## Builder chain

All configuration methods return `Self`, so the whole setup is one expression:

```rust
let agent = agentix::deepseek("sk-...")
    .model("deepseek-chat")
    .system_prompt("You are a code reviewer.")
    .temperature(0.2)
    .max_tokens(4096)
    .tool(MyTool)
    .memory(agentix::SlidingWindow::new(20));
```

---

## Msg — the event type

Every event that flows through an [`EventBus`] is a `Msg`:

| Variant | When |
|---------|------|
| `TurnStart` | Generation turn begins |
| `Done` | Turn (including all tool rounds) complete |
| `User(Vec<UserContent>)` | User message submitted — text and/or images |
| `Token(String)` | LLM output — one chunk in streaming, full text in assembled view |
| `Reasoning(String)` | Reasoning trace (e.g. DeepSeek-R1) — same streaming/assembled duality |
| `ToolCall { id, name, args }` | Complete tool invocation request |
| `ToolResult { call_id, name, result }` | Tool execution result |
| `Error(String)` | Error during generation |
| `Custom(Arc<dyn CustomMsg>)` | Application-defined payload |

### Streaming vs assembled

Subscribe to an [`EventBus`] in two ways:

```rust
// Raw streaming — Token arrives as individual chunks
let mut rx = agent.subscribe();          // broadcast::Receiver<Msg>

// Assembled — Token chunks folded into one Token(full_text) before Done
let stream = agent.event_bus().subscribe_assembled();  // impl Stream<Item = Msg>
```

The assembled view looks identical to what a non-streaming provider emits — same variant names, just complete content.

---

## Defining tools

Annotate `impl Tool for YourStruct` with `#[tool]`. Each method becomes a callable tool:

```rust
use agentix::tool;
use serde_json::{Value, json};

struct Calculator;

#[tool]
impl agentix::Tool for Calculator {
    /// Add two numbers.
    /// a: first number
    /// b: second number
    async fn add(&self, a: f64, b: f64) -> Value {
        json!({ "result": a + b })
    }

    /// Multiply two numbers.
    /// a: first number
    /// b: second number
    async fn multiply(&self, a: f64, b: f64) -> Value {
        json!({ "result": a * b })
    }
}

let agent = agentix::deepseek("sk-...")
    .tool(Calculator);
```

- Doc comment → tool description
- `/// param: description` lines → argument descriptions
- Return type just needs to implement `serde::Serialize`

---

## Memory

Two built-in memory backends, or implement [`Memory`] yourself:

```rust
use agentix::{InMemory, SlidingWindow};

// Keep all history (default)
let agent = agentix::deepseek("sk-...").memory(InMemory::new());

// Keep only the last N turns
let agent = agentix::deepseek("sk-...").memory(SlidingWindow::new(20));
```

---

## EventBus — observability

Every agent publishes all events to its [`EventBus`]. Tap any bus without affecting the agent:

```rust
// Subscribe (get a Receiver)
let mut rx = agent.subscribe();

// Tap with an async callback (spawns a background task)
agent.event_bus().tap(|msg| async move {
    if let Msg::Token(t) = msg { print!("{t}"); }
});

// Assembled stream — one Token per turn instead of many chunks
use futures::StreamExt;
let mut stream = agent.event_bus().subscribe_assembled();
while let Some(msg) = stream.next().await {
    match msg {
        Msg::Token(full) => println!("Response: {full}"),
        Msg::Done        => break,
        _                => {}
    }
}
```

---

## Graph — multi-agent pipelines

Wire [`Node`]s together with [`Graph`]. Each agent is a `Node` (has `input()` and `output()`).

`Graph::edge(&from, &to)` reads `from`'s assembled output and feeds it as a user message into `to`'s input:

```rust
use agentix::{Graph, PromptTemplate, OutputParser};

// Simple two-agent chain
let summariser  = agentix::deepseek("sk-...").system_prompt("Summarise in one sentence.");
let translator  = agentix::deepseek("sk-...").system_prompt("Translate to French.");

Graph::new()
    .edge(&summariser, &translator);

summariser.send("Long article text…").await;
// translator automatically receives the summarised text
```

### PromptTemplate

A lightweight [`Node`] that renders a template before forwarding:

```rust
let prompt = PromptTemplate::new("Translate the following to {lang}:\n{input}")
    .var("lang", "Japanese");

let agent = agentix::deepseek("sk-...");

Graph::new().edge(&prompt, &agent);

// Send a raw user message into the template
prompt.input().send(Msg::User(vec![UserContent::Text("Hello world".into())])).await.unwrap();
// agent receives: "Translate the following to Japanese:\nHello world"
```

Variables: `{input}` is replaced by the incoming `Msg::User` text; other `{key}` placeholders are pre-set with `.var(key, value)`.

### OutputParser

A lightweight [`Node`] that transforms assembled text before forwarding:

```rust
let agent  = agentix::deepseek("sk-...")
    .system_prompt("Respond with only JSON: {\"score\": <0-10>}");
let parser = OutputParser::new(|s| {
    serde_json::from_str::<serde_json::Value>(&s)
        .ok()
        .and_then(|v| v["score"].as_i64().map(|n| n.to_string()))
        .unwrap_or("0".into())
});

Graph::new().edge(&agent, &parser);
// parser.output() emits Msg::User(vec!["7".into()]) (or whatever the model returned)
```

### Middleware

Middlewares run on every message crossing any edge. Return `None` to drop:

```rust
Graph::new()
    .middleware(|msg| {
        println!("[graph] {msg:?}");
        Some(msg)
    })
    .middleware(|msg| {
        // drop empty messages
        if let Msg::User(ref parts) = msg {
            let empty = parts.iter().all(|p| matches!(p, agentix::UserContent::Text(t) if t.trim().is_empty()));
            if empty { return None; }
        }
        Some(msg)
    })
    .edge(&a, &b)
    .edge(&b, &c);
```

### Full pipeline

```rust
let prompt  = PromptTemplate::new("Score this review (0-10):\n{input}");
let scorer  = agentix::deepseek("sk-...").system_prompt("Return only JSON: {\"score\": N}");
let parser  = OutputParser::new(extract_score);
let logger  = agentix::deepseek("sk-...").system_prompt("Log: score received was {input}");

Graph::new()
    .middleware(|msg| { log::debug!("{msg:?}"); Some(msg) })
    .edge(&prompt,  &scorer)
    .edge(&scorer,  &parser)
    .edge(&parser,  &logger);

prompt.input().send(Msg::User(vec!["Great product!".into()])).await.unwrap();
```

---

## Custom Node

Implement [`Node`] to plug any async processor into a graph:

```rust
use agentix::{Node, EventBus, Msg};
use tokio::sync::mpsc;

struct UpperCaseNode { tx: mpsc::Sender<Msg>, bus: EventBus }

impl UpperCaseNode {
    fn new() -> Self {
        let (tx, mut rx) = mpsc::channel(64);
        let bus = EventBus::new(512);
        let bus_c = bus.clone();
        tokio::spawn(async move {
            while let Some(msg) = rx.recv().await {
                let out = match msg {
                    Msg::User(parts) => Msg::User(
                        parts.into_iter()
                            .map(|p| match p {
                                agentix::UserContent::Text(t) => agentix::UserContent::Text(t.to_uppercase()),
                                other => other,
                            })
                            .collect()
                    ),
                    other        => other,
                };
                bus_c.send(out);
            }
        });
        Self { tx, bus }
    }
}

impl Node for UpperCaseNode {
    fn input(&self)  -> mpsc::Sender<Msg> { self.tx.clone() }
    fn output(&self) -> EventBus           { self.bus.clone() }
}
```

---

## Multimodal (vision)

Send images alongside text using `send_parts`:

```rust
use agentix::{ImageContent, ImageData, UserContent};

// URL image
agent.send_parts(vec![
    UserContent::Image(ImageContent {
        data: ImageData::Url("https://example.com/chart.png".into()),
        mime_type: "image/png".into(),
    }),
    UserContent::Text("Describe this chart.".into()),
]).await;

// Base64 image
let bytes = std::fs::read("photo.jpg").unwrap();
agent.send_parts(vec![
    UserContent::Image(ImageContent {
        data: ImageData::Base64(base64::encode(&bytes)),
        mime_type: "image/jpeg".into(),
    }),
    UserContent::Text("What's in this photo?".into()),
]).await;
```

For plain text, `agent.send("…")` still works unchanged.

---

## MCP tools

Use external processes as tools via the Model Context Protocol:

```toml
[dependencies]
agentix = { version = "0.3", features = ["mcp"] }
```

```rust
use agentix::McpTool;

let agent = agentix::deepseek("sk-...")
    .tool(McpTool::stdio("npx", &["-y", "@playwright/mcp"]).await?);
```

---

## Exposing tools as an MCP server

```toml
[dependencies]
agentix = { version = "0.3", features = ["mcp-server"] }
```

### Stdio (Claude Desktop / MCP Studio)

```rust
use agentix::{McpServer, ToolBundle, tool};

struct Calculator;

#[tool]
impl agentix::Tool for Calculator {
    /// Add two numbers.
    /// a: first operand   b: second operand
    async fn add(&self, a: f64, b: f64) -> f64 { a + b }
}

#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
    McpServer::new(ToolBundle::new().with(Calculator))
        .with_name("my-calc")
        .serve_stdio()
        .await
}
```

### HTTP (Streamable HTTP transport)

```rust
McpServer::new(ToolBundle::new().with(MyTools))
    .serve_http("0.0.0.0:3000")
    .await?;
```

### Custom Axum routing

```rust
use agentix::{McpServer, ToolBundle};

let service = McpServer::new(ToolBundle::new().with(MyTools))
    .into_http_service(Default::default());

let router = axum::Router::new().nest_service("/mcp", service);
axum::serve(tokio::net::TcpListener::bind("0.0.0.0:3000").await?, router).await?;
```

---

## Contributing

PRs welcome. Keep changes focused; update public API docs when behaviour changes.

## License

MIT OR Apache-2.0