Error Handling

Handle errors gracefully in your agents

Handle errors gracefully to build robust agent applications.

Diagram

Error Handling

Yes

No

🤖 Agent

Error?

🛠️ Handle

✅ Success

🔄 Retry

📝 Log

Quick Start

  1. Handle Errors
    rust
    use praisonai::Agent;
    
    let agent = Agent::new()
        .name("Assistant")
        .build()?;
    
    match agent.chat("Hello").await {
        Ok(response) => println!("{}", response),
        Err(e) => println!("Error: {}", e),
    }
  2. With Retries
    rust
    use praisonai::{Agent, ExecutionConfig};
    
    let config = ExecutionConfig::new()
        .max_retries(3);
    
    let agent = Agent::new()
        .name("Reliable Bot")
        .execution(config)
        .build()?;

Error Types

ErrorCauseSolution
ApiErrorAPI call failedCheck API key, retry
TimeoutErrorRequest too slowIncrease timeout
ToolErrorTool execution failedCheck tool logic
ConfigErrorInvalid configurationCheck config values

Common Patterns

Graceful Fallback

rust
let response = agent.chat("Question").await
    .unwrap_or_else(|_| "Sorry, I encountered an error.".to_string());

Logging Errors

rust
if let Err(e) = agent.chat("Question").await {
    log::error!("Agent error: {}", e);
    // Handle gracefully
}

Best Practices

Always handle errors

Never use unwrap() in production code.

Set appropriate retries

3 retries is usually enough for transient failures.

Log errors for debugging

Keep detailed logs to diagnose issues later.


Related