Rust SDK: Setup and type-safe Agent builder

Build your first async Rust agent

The praisonai Rust crate uses a fluent builder pattern (Agent::new()…build()?) with compile-time type safety and native async/await on Tokio — ideal for ultra-fast CLI tools, embedded services, and edge workers.

Example

use praisonai::Agent;

#[tokio::main]
async fn main() -> anyhow::Result<()> {
    let agent = Agent::new()
        .name("assistant")
        .instructions("You are a concise Rust engineering assistant.")
        .model("gpt-4o-mini")
        .build()?;

    let response = agent.chat("Why does Rust avoid garbage collection pauses?").await?;
    println!("{}", response);
    Ok(())
}

Example explained

  • Agent::new()…build()? — Validates configuration cleanly and returns a Result instead of panicking.
  • agent.chat(…).await? — Runs the async LLM call on Tokio with minimal memory footprint.

Configure output and execution limits idiomatically

PraisonAI Rust mirrors the Python config dataclasses with ergonomic builders like OutputConfig::new().json() and ExecutionConfig.

Example

use praisonai::{OutputConfig, MemoryConfig};

let output = OutputConfig::new().json().file("report.json");
let memory = MemoryConfig::new()
    .with_long_term()
    .provider("memory")
    .max_messages(50);

println!("Output mode: {} | Max messages: {}", output.mode, memory.max_messages);

Example explained

  • OutputConfig::new().json() — Configures structured JSON output cleanly using Rust's builder API.
  • MemoryConfig::new().with_long_term() — Enables bounded conversation history with compile-time field checking.

Common pitfall: missing tokio runtime macros

Every async agent call requires a Tokio runtime. Make sure Cargo.toml includes tokio = { version = "1", features = ["full"] } and decorate main with #[tokio::main].

Reference: documentation