# ragen-ai/ragen architecture

> Architecture diagram of the GitHub repository ragen-ai/ragen, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2025-03-29). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/ragen-ai/ragen
- Repository: https://github.com/ragen-ai/ragen

## Overview

##### Major Components
- Environment Modules:
– Under the “ragen/env” directory, you have submodules for different tasks (bandit, countdown, frozenlake, sokoban). These simulate various stochastic environments.
– Each environment has scripts for dataset creation, environment configuration, and reward functions.
- LLM Agent & Policy:
– The “ragen/llm_agent” and “ragen/policy” directories hold modules for generating actions and reasoning.
– They incorporate the chain-of-thought mechanism (e.g., generating “<think> … </think><ans> action </ans>”) and integrate policy decision-making.
- Evaluators:
– “ragen/evaluators” implements trajectory and performance evaluation logic, crucial in the rollout stage to gauge the quality of generated trajectories.
- Training & Update Pipeline:
– The “ragen/train.py” file orchestrates the RL training process which adheres to the RICO algorithm.
– The “verl” directory is a comprehensive training engine covering configuration, model definitions (via “verl/models”), trainer scripts (under “verl/trainer”), distributed setup, and additional utilities (logging, optimizer support, etc.).
– It also includes modules for the update stage (multi-turn trajectory optimization) and reward normalization (ARPO, BRPO, GRPO).
- Supervised Fine-Tuning (SFT):
– The “sft” directory shows tools and scripts to generate supervised data, run the finetuning pipeline (with LoRA), and merge model weights.
- Configuration & Scripts:
– The “config” folder holds YAML files for various configurations of environments and training (such as bandit, countdown, etc.).
– The “scripts” folder includes CLI scripts for data creation, hyperparameter search, setup, and analysis, which are useful for orchestration and experiments.
- Data Visualization:
– Modules in “ragen/utils/plot.py” and log directories (accessed with the scripts) support visualizing trajectories and reward curves.

##### Out the Relationships and Interactions
- The Environment Module (simulated through the “ragen/env/*” components) should show:
– How an initial state is provided to the LLM agent.
– That the environment returns feedback (reward and updated state) after an agent action.
- The LLM Agent, built from modules in “ragen/llm_agent” and “ragen/policy”, should interact with the environment via “rollout” where it generates reasoning-guided actions.
– Indicate the iterative loop where the agent takes the trajectory history and outputs decisions.
- The Evaluators assess the generated trajectories and assist the “update” phase when the RL algorithm uses importance sampling to update the LLM’s parameters.
- The Training Pipeline orchestrates both the Rollout (trajectory generation) stage and the Update (multi-turn trajectory optimization) stage.
- The “verl” module acts as the backend training engine:
– It integrates distributed training, reward normalization, and model orchestration.
- The SFT route (if used) branches off to a supervised model finetuning component, interacting with its own data generation and merging utilities.

##### Architectural Patterns and Design Principles
- Modularization: Emphasize the separation of concerns by grouping environment interactions, policy logic, evaluation, and model training into distinct modules.
- Iterative Loop (MDP Import): Use the MDP representation as a design pattern – states, actions, rewards flow continuously through the system.
- Distributed/Scalable Training: Indicate usage of distributed training (note the “verl/single_controller”, “ray”, and other distributed utilities) to underline scalability and performance.

##### Final Elements
- Clear labels for each of the major architectural sections: Environment, Agent, Evaluator, Training Engine, Supervised Finetuning, and Configuration/Scripts.

## Diagram (Mermaid)

```mermaid
flowchart TD
    subgraph "Configuration & Scripts"
        Config["Configuration (YAML)"]:::config
        Scripts["Setup Scripts"]:::scripts
    end

    subgraph "Environment Modules"
        Env_Bandit["Bandit Env"]:::env
        Env_Countdown["Countdown Env"]:::env
        Env_Frozen["FrozenLake Env"]:::env
        Env_Sokoban["Sokoban Env"]:::env
        EnvAggregate["Environment Simulator"]:::env
    end

    subgraph "LLM Agent & Policy"
        Agent["LLM Agent"]:::agent
        Policy["Policy Module"]:::agent
    end

    Evaluators["Evaluators"]:::evaluator

    subgraph "Training & Update Pipeline"
        Train["Train Orchestrator (ragen/train.py)"]:::training
        Verl["Training Engine (Verl)"]:::training
    end

    subgraph "Supervised Fine-Tuning (SFT)"
        SFT_Pipeline["SFT Pipeline"]:::sft
        SFT_Utils["SFT Utilities"]:::sft
    end

    Visualization["Data Visualization"]:::visualization

    %% Connections
    Scripts -->|"configure"| Config
    Config -->|"init_env"| EnvAggregate
    Env_Bandit --> EnvAggregate
    Env_Countdown --> EnvAggregate
    Env_Frozen --> EnvAggregate
    Env_Sokoban --> EnvAggregate

    EnvAggregate -->|"initial_state"| Agent
    Agent -->|"action_generation"| Policy
    Policy -->|"send_action"| EnvAggregate
    EnvAggregate -->|"feedback"| Evaluators

    Evaluators -->|"evaluation"| Train
    Evaluators -->|"logging"| Visualization
    Train -->|"distributed_update"| Verl
    Train -->|"update_policy"| Agent
    Train -->|"logging"| Visualization
    Config -->|"train_config"| Train
    SFT_Pipeline -->|"finetuning"| Train
    SFT_Utils --> SFT_Pipeline

    %% Click Events
    click Env_Bandit "https://github.com/ragen-ai/ragen/tree/main/ragen/env/bandit"
    click Env_Countdown "https://github.com/ragen-ai/ragen/tree/main/ragen/env/countdown"
    click Env_Frozen "https://github.com/ragen-ai/ragen/tree/main/ragen/env/frozen_lake"
    click Env_Sokoban "https://github.com/ragen-ai/ragen/tree/main/ragen/env/sokoban"
    click Agent "https://github.com/ragen-ai/ragen/tree/main/ragen/llm_agent"
    click Policy "https://github.com/ragen-ai/ragen/tree/main/ragen/policy"
    click Evaluators "https://github.com/ragen-ai/ragen/tree/main/ragen/evaluators"
    click Train "https://github.com/ragen-ai/ragen/blob/main/ragen/train.py"
    click Verl "https://github.com/ragen-ai/ragen/tree/main/verl"
    click SFT_Pipeline "https://github.com/ragen-ai/ragen/blob/main/sft/sft_pipeline.py"
    click SFT_Utils "https://github.com/ragen-ai/ragen/tree/main/sft/utils"
    click Config "https://github.com/ragen-ai/ragen/tree/main/config"
    click Scripts "https://github.com/ragen-ai/ragen/tree/main/scripts"
    click Visualization "https://github.com/ragen-ai/ragen/blob/main/ragen/utils/plot.py"

    %% Styles
    classDef config fill:#f9e79f,stroke:#b7950b,stroke-width:2px;
    classDef scripts fill:#f9e79f,stroke:#b7950b,stroke-width:2px;
    classDef env fill:#aed6f1,stroke:#2471a3,stroke-width:2px;
    classDef agent fill:#d5f5e3,stroke:#27ae60,stroke-width:2px;
    classDef evaluator fill:#f5cba7,stroke:#d35400,stroke-width:2px;
    classDef training fill:#fadbd8,stroke:#e74c3c,stroke-width:2px;
    classDef sft fill:#e8daef,stroke:#8e44ad,stroke-width:2px;
    classDef visualization fill:#d6eaf8,stroke:#3498db,stroke-width:2px;
```
