DEV Community

Anton Illarionov
Anton Illarionov

Posted on

Building Safe Autonomous AI Agents in Rust with Constitutional Validation

Building Safe Autonomous AI Agents in Rust

Rust's memory safety combines naturally with constitutional AI validation. Here is the pattern.

The Problem

Rust prevents memory errors at compile time. But it cannot prevent logical errors in autonomous agents:

  • Executing the same action twice
  • Acting on hallucinated references
  • Exceeding authorized scope

Constitutional validation catches these at runtime.

The Pattern

use std::collections::HashMap;
use reqwest::Client;
use serde::{Deserialize, Serialize};

#[derive(Deserialize)]
struct GuardrailResult {
    verdict: String,  // APPROVED / REJECTED / ESCALATE
    reasoning: String,
}

async fn check_before_acting(
    client: &Client,
    action: &str,
    token: &str,
) -> Result<bool, reqwest::Error> {
    let result: GuardrailResult = client
        .post("https://api.odei.ai/api/v2/guardrail/check")
        .bearer_auth(token)
        .json(&serde_json::json!({
            "action": action,
            "severity": "medium"
        }))
        .send()
        .await?
        .json()
        .await?;

    Ok(result.verdict == "APPROVED")
}

async fn safe_execute(
    client: &Client,
    token: &str,
    action: &str,
    execute_fn: impl Fn() -> (),
) {
    match check_before_acting(client, action, token).await {
        Ok(true) => execute_fn(),
        Ok(false) => eprintln!("Action blocked"),
        Err(e) => eprintln!("Validation error: {}", e),
    }
}
Enter fullscreen mode Exit fullscreen mode

Why Rust + Constitutional AI

Protection Mechanism
Memory safety Rust borrow checker
Race conditions Rust ownership system
Duplicate actions ODEI deduplication layer
Unauthorized ops ODEI authority layer
Hallucinated refs ODEI integrity layer

Together: the safest autonomous agent stack.

Production

ODEI has been running since January 2026 with zero critical failures.

API: https://api.odei.ai | MCP: npx @odei/mcp-server | Research: github.com/odei-ai/research

Top comments (0)