Flows Flows — Rust examples backed by real provider calls. rust examples examples/flows src/examples/rust/flows example Flows

These Rust examples are real runnable files. Edit the source file first; this page is rebuilt from the checked-in example and its metadata header.

Rust Sequential Flow

Runs a two-step Ax flow against OpenAI.

Rust
use axllm::{ax, flow, AxResult, OpenAICompatibleClient};
use serde_json::json;
use std::env;


fn openai_client() -> AxResult<OpenAICompatibleClient> {
    let api_key = env::var("OPENAI_API_KEY").or_else(|_| env::var("OPENAI_APIKEY")).map_err(|_| axllm::AxError::runtime("Set OPENAI_API_KEY or OPENAI_APIKEY to run this example."))?;
    let model = env::var("AX_OPENAI_MODEL").unwrap_or_else(|_| "gpt-5.4-mini".to_string());
    Ok(OpenAICompatibleClient::new(api_key, model).with_model_config(json!({"temperature": 0})))
}

fn main() -> AxResult<()> {
    let mut client = openai_client()?;
    let step = ax("documentText:string -> summaryText:string")?;
    let mut program = axllm::flow("examples.sequentialFlow").execute("step", step).returns(json!({"step": "step"}));
    let output = program.forward(&mut client, json!({"documentText": "Ax gives developers signatures, provider clients, agents, flows, tracing, and optimization."}))?;
    println!("{}", serde_json::to_string_pretty(&output)?);
    Ok(())
}

Rust Branching Flow

Routes a classification through follow-up flow logic backed by OpenAI.

Rust
use axllm::{ax, flow, AxResult, OpenAICompatibleClient};
use serde_json::json;
use std::env;

fn openai_client() -> AxResult<OpenAICompatibleClient> {
    let api_key = env::var("OPENAI_API_KEY")
        .or_else(|_| env::var("OPENAI_APIKEY"))
        .map_err(|_| {
            axllm::AxError::runtime("Set OPENAI_API_KEY or OPENAI_APIKEY to run this example.")
        })?;
    let model = env::var("AX_OPENAI_MODEL").unwrap_or_else(|_| "gpt-5.4-mini".to_string());
    Ok(OpenAICompatibleClient::new(api_key, model).with_model_config(json!({"temperature": 0})))
}

fn main() -> AxResult<()> {
    let mut client = openai_client()?;
    let classifier = ax("request:string -> route:class \"support, sales, engineering\"")?;
    let responder = ax("request:string, route:string -> response:string")?;
    let mut program = flow("examples.branchFlow")
        .execute_with_options(
            "classifier",
            classifier,
            &json!({"reads": ["request"], "writes": ["classifierResult", "route"]}),
        )
        .execute_with_options(
            "responder",
            responder,
            &json!({
                "reads": ["request", "route"],
                "writes": ["responderResult", "response"]
            }),
        )
        .returns(json!({"route": "route", "response": "response"}));
    let output = program.forward(
        &mut client,
        json!({"request": "A customer says checkout is down for their enterprise account."}),
    )?;
    println!("{}", serde_json::to_string_pretty(&output)?);
    Ok(())
}

Rust Parallel Flow

Runs two independent OpenAI-backed steps in parallel before joining their results.

Rust
use axllm::{ax, flow, AxResult, OpenAICompatibleClient};
use serde_json::json;
use std::env;

fn openai_client() -> AxResult<OpenAICompatibleClient> {
    let api_key = env::var("OPENAI_API_KEY")
        .or_else(|_| env::var("OPENAI_APIKEY"))
        .map_err(|_| {
            axllm::AxError::runtime("Set OPENAI_API_KEY or OPENAI_APIKEY to run this example.")
        })?;
    let model = env::var("AX_OPENAI_MODEL").unwrap_or_else(|_| "gpt-5.4-mini".to_string());
    Ok(OpenAICompatibleClient::new(api_key, model).with_model_config(json!({"temperature": 0})))
}

fn main() -> AxResult<()> {
    let mut client = openai_client()?;
    let research = ax("topicText:string -> factList:string[]")?;
    let audience = ax("topicText:string -> audienceAngle:string")?;
    let join = ax("factList:string[], audienceAngle:string -> briefText:string")?;
    let mut program = flow("examples.parallelFlow")
        .execute_with_options(
            "research",
            research,
            &json!({"reads": ["topicText"], "writes": ["researchResult", "factList"]}),
        )
        .execute_with_options(
            "audience",
            audience,
            &json!({"reads": ["topicText"], "writes": ["audienceResult", "audienceAngle"]}),
        )
        .execute_with_options(
            "join",
            join,
            &json!({
                "reads": ["factList", "audienceAngle"],
                "writes": ["joinResult", "briefText"]
            }),
        )
        .returns(json!({"briefText": "briefText"}));
    let output = program.forward(
        &mut client,
        json!({"topicText": "Why typed contracts make multi-step LLM systems easier to maintain"}),
    )?;
    println!("{}", serde_json::to_string_pretty(&output)?);
    Ok(())
}

Rust Composed Flow

Composes multiple typed programs into one OpenAI-backed flow.

Rust
use axllm::{ax, flow, AxResult, OpenAICompatibleClient};
use serde_json::json;
use std::env;


fn openai_client() -> AxResult<OpenAICompatibleClient> {
    let api_key = env::var("OPENAI_API_KEY").or_else(|_| env::var("OPENAI_APIKEY")).map_err(|_| axllm::AxError::runtime("Set OPENAI_API_KEY or OPENAI_APIKEY to run this example."))?;
    let model = env::var("AX_OPENAI_MODEL").unwrap_or_else(|_| "gpt-5.4-mini".to_string());
    Ok(OpenAICompatibleClient::new(api_key, model).with_model_config(json!({"temperature": 0})))
}

fn main() -> AxResult<()> {
    let mut client = openai_client()?;
    let step = ax("topic:string -> outline:string[]")?;
    let mut program = axllm::flow("examples.composedFlow").execute("step", step).returns(json!({"step": "step"}));
    let output = program.forward(&mut client, json!({"topic": "How Ax moves from typed generation to agents, flows, and optimization"}))?;
    println!("{}", serde_json::to_string_pretty(&output)?);
    Ok(())
}

Rust Refinement Flow

Drafts, critiques, and revises an answer through three OpenAI-backed steps.

Rust
use axllm::{ax, flow, AxResult, OpenAICompatibleClient};
use serde_json::json;
use std::env;

fn openai_client() -> AxResult<OpenAICompatibleClient> {
    let api_key = env::var("OPENAI_API_KEY")
        .or_else(|_| env::var("OPENAI_APIKEY"))
        .map_err(|_| {
            axllm::AxError::runtime("Set OPENAI_API_KEY or OPENAI_APIKEY to run this example.")
        })?;
    let model = env::var("AX_OPENAI_MODEL").unwrap_or_else(|_| "gpt-5.4-mini".to_string());
    Ok(OpenAICompatibleClient::new(api_key, model).with_model_config(json!({"temperature": 0})))
}

fn main() -> AxResult<()> {
    let mut client = openai_client()?;
    let draft = ax("topicText:string -> draftText:string")?;
    let critique = ax("draftText:string -> critiqueText:string")?;
    let revise = ax("draftText:string, critiqueText:string -> revisedText:string")?;
    let mut program = flow("examples.refineFlow")
        .execute_with_options(
            "draft",
            draft,
            &json!({"reads": ["topicText"], "writes": ["draftResult", "draftText"]}),
        )
        .execute_with_options(
            "critique",
            critique,
            &json!({"reads": ["draftText"], "writes": ["critiqueResult", "critiqueText"]}),
        )
        .execute_with_options(
            "revise",
            revise,
            &json!({
                "reads": ["draftText", "critiqueText"],
                "writes": ["reviseResult", "revisedText"]
            }),
        )
        .returns(json!({"revisedText": "revisedText"}));
    let output = program.forward(
        &mut client,
        json!({"topicText": "Explain automatic flow parallelism to a backend engineer."}),
    )?;
    println!("{}", serde_json::to_string_pretty(&output)?);
    Ok(())
}
Docs