# nousresearch/atropos architecture

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

- Interactive diagram: https://gitdiagram.com/nousresearch/atropos
- Repository: https://github.com/nousresearch/atropos

## Overview

To illustrate Atropos’s architecture accurately, think of it as a microservice-based RL framework that decouples three roles: 1) environment services, 2) a central trajectory API (“run-api​” server), and 3) trainers/inference engines. Use the file tree and README to identify how code modules map to these roles, then represent them as distinct components with clear interfaces.

##### Main components
- Core Library (atroposlib/)
- atroposlib/api: Trajectory server client & server utils
- atroposlib/cli: CLI entrypoints (run-api, serve, process, view-run, sft/dpo-gen)
- atroposlib/envs: BaseEnv classes, reward functions, server handling, and front-end visualization
- atroposlib/utils & type_definitions: Shared helpers (config, I/O, metrics, batching)
- Environment Microservices (environments/)
- Each script (e.g. gsm8k_server.py, blackjack_local_server.py, tool_calling_server.py, rlaif_server.py, etc.) implements a BaseEnv subclass
- Local servers launched via CLI “serve”: communicate asynchronously with the central API
- Trajectory API Server (run-api)
- Central service that environments POST rollout data to, and trainers pull batches from
- Exposed via atroposlib/api/server.py
- Trainer & Inference Engines (external)
- Example trainer (example_trainer/) or third-party tools (Axolotl plugin) implement RL loop
- Inference-agnostic: can call OpenAI, vLLM, SGLang via atroposlib/api/utils
- Debug & Visualization Tools
- process subcommand: runs inference only, outputs JSONL + HTML via atroposlib/frontend
- view-run: Gradio UI for batch inspection
- atropos-sft-gen / atropos-dpo-gen: offline data generation

##### Interactions and data flows
- Environment → Trajectory API: environments asynchronously POST “step” and “episode” messages
- Trainer ← Trajectory API: trainers pull batched rollouts for learning
- Environment ↔ Inference Servers: each environment may call LLM endpoints per step
- CLI tools ↔ Central API: run-api, process, view-run, atropos-sft-gen interact with the API or run environment locally
- Telemetry → WandB or local logs: reward metrics and rollout statistics

##### Architectural patterns
- Microservice pattern: each environment runs as an independent service
- Publish/subscribe or queuing model: environments publish to central API, trainers subscribe
- Plugin/abstraction layers: atroposlib/api abstracts any OpenAI-compatible provider
- Async & decoupled: environments don’t block on trainer updates, support heterogeneous group sizes

##### Layer in key technologies
- Python 3.10+, FastAPI / Uvicorn (likely for run-api server)
- HTTP/REST or WebSocket interfaces between services
- YAML/CLI configs via BaseEnvConfig (CONFIG.md)
- JSONL + static HTML for offline diagnostics
- WandB for experiment tracking
- Docker (optional) for code_execution_server environments

- It’s better to show each environment script as an independent node
- Common utilities under “atroposlib/utils” rather than detail every file
- Async RL loop: Inference → Environment → Trajectory API → Trainer → updated policy back to Inference

## Diagram (Mermaid)

```mermaid
flowchart TB
    subgraph "Tier 1: Inference Providers"
        OAI["OpenAI API"]:::external
        VLM["vLLM"]:::external
        CAPI["Custom LLM APIs"]:::external
    end

    subgraph "Tier 2: Environment Services"
        GSM["GSM8K Env"]:::service
        BJ["Blackjack Env"]:::service
        TOOL["Tool-Calling Env"]:::service
        RLAIF["RL-AIF Env"]:::service
        CODE["Code Execution Env"]:::service
        DATA["Dataset Env"]:::service
        SERV["serve CLI"]:::cli
    end

    subgraph "Tier 3: Trajectory API Server"
        API["Trajectory API Server"]:::api
    end

    subgraph "Tier 4: Trainer Processes"
        TR["Example Trainer"]:::trainer
    end

    subgraph "Tier 5: Debug & Visualization Tools"
        RUNAPI["run-api CLI"]:::cli
        PROC["process (JSONL→HTML)"]:::cli
        VIEW["view-run"]:::cli
        SFT["sft-gen"]:::cli
        DPO["dpo-gen"]:::cli
        WATCH["inference watcher"]:::cli
    end

    OAI -->|"LLM requests/responses"| GSM
    OAI -->|"LLM requests/responses"| BJ
    OAI -->|"LLM requests/responses"| TOOL
    OAI -->|"LLM requests/responses"| RLAIF
    VLM -->|"LLM requests/responses"| GSM
    VLM -->|"LLM requests/responses"| BJ
    CAPI -->|"LLM requests/responses"| TOOL

    SERV -->|"launch Env service"| GSM
    SERV -->|"launch Env service"| BJ
    SERV -->|"launch Env service"| TOOL
    SERV -->|"launch Env service"| RLAIF
    SERV -->|"launch Env service"| CODE
    SERV -->|"launch Env service"| DATA

    GSM -->|"POST rollouts"| API
    BJ -->|"POST rollouts"| API
    TOOL -->|"POST rollouts"| API
    RLAIF -->|"POST rollouts"| API
    CODE -->|"POST rollouts"| API
    DATA -->|"POST rollouts"| API

    API -->|"GET batches"| TR

    RUNAPI -->|"launch API server"| API
    PROC -->|"read JSONL & write HTML"| API
    VIEW -->|"inspect rollouts"| API
    WATCH -->|"watch & log metrics"| API
    SFT -->|"generate SFT data"| API
    DPO -->|"generate DPO data"| API

    click API "https://github.com/nousresearch/atropos/blob/main/atroposlib/api/server.py"
    click RUNAPI "https://github.com/nousresearch/atropos/blob/main/atroposlib/cli/run_api.py"
    click SERV "https://github.com/nousresearch/atropos/tree/main/atroposlib/cli/serve"
    click PROC "https://github.com/nousresearch/atropos/blob/main/atroposlib/cli/inference_node_wandb_watcher.py"
    click PROC "https://github.com/nousresearch/atropos/blob/main/atroposlib/frontend/jsonl2html.py"
    click VIEW "https://github.com/nousresearch/atropos/blob/main/atroposlib/cli/view_run.py"
    click SFT "https://github.com/nousresearch/atropos/blob/main/atroposlib/cli/sft.py"
    click DPO "https://github.com/nousresearch/atropos/blob/main/atroposlib/cli/dpo.py"

    classDef external fill:#ffcc00,stroke:#333,stroke-width:1px
    classDef service fill:#a8f0c6,stroke:#333,stroke-width:1px
    classDef api fill:#85d7ff,stroke:#333,stroke-width:1px
    classDef trainer fill:#f9a8d4,stroke:#333,stroke-width:1px
    classDef cli fill:#fbdb84,stroke:#333,stroke-width:1px
```
