Rust: Custom #[tool] macros, memory, and AgentTeam

Define type-safe tools with #[tool]

Decorate any async Rust function with #[tool(description = "…")] and PraisonAI generates the tool struct and JSON parameter schema at compile time — zero runtime reflection overhead.

Example

use praisonai::{tool, Agent};

#[tool(description = "Calculate the sum of two integers")]
async fn add_numbers(a: i32, b: i32) -> i32 {
    a + b
}

#[tokio::main]
async fn main() -> anyhow::Result<()> {
    let agent = Agent::new()
        .name("math-agent")
        .instructions("Use add_numbers for arithmetic.")
        .tool(AddNumbersTool::new())
        .build()?;

    println!("{}", agent.chat("What is 19 + 23?").await?);
    Ok(())
}

Example explained

  • #[tool(description = …)] — Procedural macro generating AddNumbersTool and its typed JSON schema.
  • .tool(AddNumbersTool::new()) — Attaches the compiled tool directly to the agent builder.

Orchestrate multi-agent teams with AgentTeam

Chain specialized Rust agents using AgentTeam::new() so research, drafting, and review run inside one compiled binary.

Example

use praisonai::{Agent, AgentTeam};

let researcher = Agent::new()
    .name("researcher")
    .instructions("Extract 3 metrics from the log snippet.")
    .build()?;

let reviewer = Agent::new()
    .name("reviewer")
    .instructions("Flag any metric that breaches SLA.")
    .build()?;

let team = AgentTeam::new()
    .agent(researcher)
    .agent(reviewer);
// Call team.start().await? inside #[tokio::main]

Example explained

  • AgentTeam::new().agent(…) — Composes multiple Rust agents into a sequential pipeline.

Common pitfall: blocking the Tokio executor inside a tool

Never call blocking std::fs or heavy CPU loops directly inside an async #[tool] function — use tokio::fs or tokio::task::spawn_blocking so concurrent agent tasks stay responsive.

Reference: documentation