# thudm/agentrl architecture

> Architecture diagram of the GitHub repository thudm/agentrl, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2026-07-18). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/thudm/agentrl
- Repository: https://github.com/thudm/agentrl

## Overview

#### Purpose and Top-Level Entry Points
AgentRL is a distributed framework for multi-turn, multi-task agent reinforcement learning, plus deployment and evaluation of task environments. It comprises a Go control plane, Python task workers, a Ray-based training library, and an evaluation CLI. Primary entry points are `controller/main.go`, `worker/src/agentrl/worker/__main__.py`, `examples/training/async_trainer.py`, and `eval/src/agentrl/eval/__main__.py`.

#### Controller: Environment Control Plane
The Go controller is the ingress and coordinator for agent sessions and registered task workers. It routes task/session requests, exposes HTTP and gRPC APIs, manages worker/session/task state, and embeds a monitoring dashboard.
- `controller/cmd/controller.go`
- `controller/internal/controller/controller.go`
- `controller/internal/controller/grpc.go`
- `controller/internal/server/serve.go`

Its protocol contracts are defined centrally in `proto/controller_v1/controller.proto` and `proto/common.proto`.

#### Cluster Membership and Worker Routing
Controller instances use cluster membership and node tracking to coordinate distributed deployment. Worker state and task assignment are kept behind controller-level abstractions, separating client-facing request handling from worker transport.
- `controller/internal/cluster/memberlist.go`
- `controller/internal/cluster/nodes.go`
- `controller/internal/controller/worker.go`
- `controller/internal/controller/session.go`

The dashboard is a separate embedded Vue frontend served through controller middleware: `controller/dashboard/embed.go` and `controller/internal/middlewares/dashboard.go`.

#### Task Worker and Environment Boundary
Each Python task worker hosts concrete task environments: lifecycle management, prompt construction, agent-response parsing/execution, and reward/evaluation. `TaskWorker` is the core service boundary; tasks remain deployment-specific behind this API.
- `worker/src/agentrl/worker/task_worker.py`
- `worker/src/agentrl/worker/task.py`
- `worker/src/agentrl/worker/task_worker_grpc.py`

Workers can communicate with the controller through gRPC, avoiding direct controller-to-worker addressability requirements. Generated/packaged protocol bindings reside under `worker/src/agentrl/worker/pb/`.

#### Deployment and Durable State
The worker environment layer abstracts how task services are started: Docker, Kubernetes, or manually managed processes. A separate state-provider interface supports local state and external Consul, etcd, or Redis backends.
- `worker/src/agentrl/worker/environment/docker.py`
- `worker/src/agentrl/worker/environment/k8s.py`
- `worker/src/agentrl/worker/environment/state/consul.py`
- `worker/src/agentrl/worker/environment/state/redis.py`

Controller snapshot services provide persistent snapshot metadata/storage APIs over HTTP and gRPC: `controller/internal/snapshots/manager.go`, `controller/internal/snapshots/store.go`, and `proto/snapshots_v1/snapshots.proto`.

#### Distributed Training Pipeline
Training composes Ray actor pools: SGLang rollout workers generate trajectories; FSDP actor workers train the policy; FSDP reference workers provide the frozen KL baseline. The collective handle creates and addresses distributed Ray workers, while NCCL plugins stream updated actor parameters to rollout replicas.
- `trainer/src/agentrl/trainer/workers/collective_handle.py`
- `trainer/src/agentrl/trainer/workers/async_sglang_worker.py`
- `trainer/src/agentrl/trainer/workers/fsdp_worker.py`
- `trainer/src/agentrl/trainer/components/nccl_tensor_comm.py`

The primary flow is task definitions → asynchronous rollout generation → shared trajectory buffer → GRPO/PPO loss and FSDP update → NCCL parameter refresh. Scheduling and buffering are owned by `trainer/src/agentrl/trainer/components/task_manager.py` and `trainer/src/agentrl/trainer/components/buffer.py`; algorithms live in `trainer/src/agentrl/trainer/algorithms/`.

#### Evaluation
The experimental `agentrl-eval` package runs/resumes multi-model, multi-task evaluations against controller-served tasks. It isolates model-provider clients, session orchestration, event streaming, and result storage.
- `eval/src/agentrl/eval/cli/main.py`
- `eval/src/agentrl/eval/session/runner.py`
- `eval/src/agentrl/eval/session/controller.py`
- `eval/src/agentrl/eval/client/openai.py`

Material runtimes/services are Go, Python, Ray, PyTorch/FSDP, NCCL, SGLang, FastAPI, Docker/Kubernetes, gRPC/HTTP, and optional Consul/etcd/Redis/OpenAI- or Anthropic-compatible model APIs.

Main entry points: [`controller/internal/controller/controller.go`](https://github.com/thudm/agentrl/blob/main/controller/internal/controller/controller.go) (Session & worker coordinator), [`worker/src/agentrl/worker/task_worker.py`](https://github.com/thudm/agentrl/blob/main/worker/src/agentrl/worker/task_worker.py) (TaskWorker), [`trainer/src/agentrl/trainer/workers/async_sglang_worker.py`](https://github.com/thudm/agentrl/blob/main/trainer/src/agentrl/trainer/workers/async_sglang_worker.py) (SGLang rollout pool), [`controller/internal/controller/grpc.go`](https://github.com/thudm/agentrl/blob/main/controller/internal/controller/grpc.go) (Controller APIs)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_control["Control Plane"]
  node_controller_entry["Controller entry<br/>Go service<br/>[main.go]"]
  node_controller_core["Session &amp; worker coordinator<br/>controller<br/>[controller.go]"]
  node_controller_grpc["Controller APIs<br/>gRPC/HTTP API<br/>[grpc.go]"]
  node_cluster["Cluster membership<br/>memberlist<br/>[memberlist.go]"]
  node_dashboard["Embedded dashboard<br/>Vue frontend<br/>[embed.go]"]
  node_snapshots[("Snapshot manager<br/>persistent state<br/>[manager.go]")]
  node_protocols["Protocol contracts<br/>protobuf<br/>[controller.proto]"]
end

subgraph group_worker["Task Workers"]
  node_worker_entry["Worker entry<br/>Python service<br/>[__main__.py]"]
  node_task_worker["TaskWorker<br/>task service<br/>[task_worker.py]"]
  node_worker_grpc["Worker gRPC transport<br/>gRPC client"]
  node_task_environment["Task environments<br/>deployment-specific tasks<br/>[task.py]"]
  node_environment_runtime["Environment deployment<br/>Docker/Kubernetes<br/>[docker.py]"]
  node_state_backends[("External state providers<br/>Consul/etcd/Redis<br/>[consul.py]")]
end

subgraph group_training["Training Pipeline"]
  node_trainer_entry["Async trainer<br/>Ray training entry<br/>[async_trainer.py]"]
  node_task_manager["Task scheduler<br/>training component<br/>[task_manager.py]"]
  node_rollout_workers["SGLang rollout pool<br/>Ray actors"]
  node_trajectory_buffer["Trajectory buffer<br/>shared buffer<br/>[buffer.py]"]
  node_fsdp_workers["FSDP policy &amp; reference pool<br/>Ray/PyTorch actors<br/>[fsdp_worker.py]"]
  node_nccl_refresh["NCCL parameter refresh<br/>tensor transport"]
end

subgraph group_evaluation["Evaluation"]
  node_eval_cli["Evaluation CLI<br/>Python CLI<br/>[__main__.py]"]
  node_eval_sessions["Evaluation sessions<br/>session orchestration<br/>[runner.py]"]
  node_model_clients["Model-provider clients<br/>OpenAI-compatible API<br/>[openai.py]"]
end

node_controller_entry -->|"starts"| node_controller_core
node_controller_core -->|"serves"| node_controller_grpc
node_controller_core -->|"coordinates through"| node_cluster
node_controller_core -->|"serves"| node_dashboard
node_controller_core -->|"persists snapshots"| node_snapshots
node_controller_grpc -->|"implements"| node_protocols
node_worker_entry -->|"starts"| node_task_worker
node_task_worker -->|"hosts"| node_task_environment
node_task_worker -->|"uses"| node_worker_grpc
node_worker_grpc -->|"registers and exchanges sessions"| node_controller_grpc
node_worker_grpc -->|"uses bindings"| node_protocols
node_task_environment -->|"deploys via"| node_environment_runtime
node_task_environment -.->|"stores state in"| node_state_backends
node_trainer_entry -->|"configures"| node_task_manager
node_task_manager -->|"schedules"| node_rollout_workers
node_rollout_workers -->|"runs tasks against"| node_task_worker
node_rollout_workers -->|"writes trajectories"| node_trajectory_buffer
node_trajectory_buffer -->|"feeds GRPO/PPO updates"| node_fsdp_workers
node_fsdp_workers -->|"publishes parameters"| node_nccl_refresh
node_nccl_refresh -->|"refreshes replicas"| node_rollout_workers
node_eval_cli -->|"runs"| node_eval_sessions
node_eval_sessions -->|"creates task sessions"| node_controller_grpc
node_eval_sessions -->|"requests agent responses"| node_model_clients

click node_controller_entry "https://github.com/thudm/agentrl/blob/main/controller/main.go"
click node_controller_core "https://github.com/thudm/agentrl/blob/main/controller/internal/controller/controller.go"
click node_controller_grpc "https://github.com/thudm/agentrl/blob/main/controller/internal/controller/grpc.go"
click node_cluster "https://github.com/thudm/agentrl/blob/main/controller/internal/cluster/memberlist.go"
click node_dashboard "https://github.com/thudm/agentrl/blob/main/controller/dashboard/embed.go"
click node_snapshots "https://github.com/thudm/agentrl/blob/main/controller/internal/snapshots/manager.go"
click node_protocols "https://github.com/thudm/agentrl/blob/main/proto/controller_v1/controller.proto"
click node_worker_entry "https://github.com/thudm/agentrl/blob/main/worker/src/agentrl/worker/__main__.py"
click node_task_worker "https://github.com/thudm/agentrl/blob/main/worker/src/agentrl/worker/task_worker.py"
click node_worker_grpc "https://github.com/thudm/agentrl/blob/main/worker/src/agentrl/worker/task_worker_grpc.py"
click node_task_environment "https://github.com/thudm/agentrl/blob/main/worker/src/agentrl/worker/task.py"
click node_environment_runtime "https://github.com/thudm/agentrl/blob/main/worker/src/agentrl/worker/environment/docker.py"
click node_state_backends "https://github.com/thudm/agentrl/blob/main/worker/src/agentrl/worker/environment/state/consul.py"
click node_trainer_entry "https://github.com/thudm/agentrl/blob/main/examples/training/async_trainer.py"
click node_task_manager "https://github.com/thudm/agentrl/blob/main/trainer/src/agentrl/trainer/components/task_manager.py"
click node_rollout_workers "https://github.com/thudm/agentrl/blob/main/trainer/src/agentrl/trainer/workers/async_sglang_worker.py"
click node_trajectory_buffer "https://github.com/thudm/agentrl/blob/main/trainer/src/agentrl/trainer/components/buffer.py"
click node_fsdp_workers "https://github.com/thudm/agentrl/blob/main/trainer/src/agentrl/trainer/workers/fsdp_worker.py"
click node_nccl_refresh "https://github.com/thudm/agentrl/blob/main/trainer/src/agentrl/trainer/components/nccl_tensor_comm.py"
click node_eval_cli "https://github.com/thudm/agentrl/blob/main/eval/src/agentrl/eval/__main__.py"
click node_eval_sessions "https://github.com/thudm/agentrl/blob/main/eval/src/agentrl/eval/session/runner.py"
click node_model_clients "https://github.com/thudm/agentrl/blob/main/eval/src/agentrl/eval/client/openai.py"

classDef toneNeutral fill:#f8fafc,stroke:#334155,stroke-width:1.5px,color:#0f172a
classDef toneBlue fill:#dbeafe,stroke:#2563eb,stroke-width:1.5px,color:#172554
classDef toneAmber fill:#fef3c7,stroke:#d97706,stroke-width:1.5px,color:#78350f
classDef toneMint fill:#dcfce7,stroke:#16a34a,stroke-width:1.5px,color:#14532d
classDef toneRose fill:#ffe4e6,stroke:#e11d48,stroke-width:1.5px,color:#881337
classDef toneIndigo fill:#e0e7ff,stroke:#4f46e5,stroke-width:1.5px,color:#312e81
classDef toneTeal fill:#ccfbf1,stroke:#0f766e,stroke-width:1.5px,color:#134e4a
class node_controller_entry,node_controller_core,node_controller_grpc,node_cluster,node_dashboard,node_snapshots,node_protocols toneBlue
class node_worker_entry,node_task_worker,node_worker_grpc,node_task_environment,node_environment_runtime,node_state_backends toneAmber
class node_trainer_entry,node_task_manager,node_rollout_workers,node_trajectory_buffer,node_fsdp_workers,node_nccl_refresh toneMint
class node_eval_cli,node_eval_sessions,node_model_clients toneRose
```

## Components

### Control Plane

Go ingress, coordination, and durable snapshots.

- [Controller entry](https://github.com/thudm/agentrl/blob/main/controller/main.go): Go service · `controller/main.go`
- [Session & worker coordinator](https://github.com/thudm/agentrl/blob/main/controller/internal/controller/controller.go): controller · Routes task sessions and tracks worker assignment. · `controller/internal/controller/controller.go`
- [Controller APIs](https://github.com/thudm/agentrl/blob/main/controller/internal/controller/grpc.go): gRPC/HTTP API · `controller/internal/controller/grpc.go`
- [Cluster membership](https://github.com/thudm/agentrl/blob/main/controller/internal/cluster/memberlist.go): memberlist · `controller/internal/cluster/memberlist.go`
- [Embedded dashboard](https://github.com/thudm/agentrl/blob/main/controller/dashboard/embed.go): Vue frontend · `controller/dashboard/embed.go`
- [Snapshot manager](https://github.com/thudm/agentrl/blob/main/controller/internal/snapshots/manager.go): persistent state · Stores snapshot metadata through HTTP and gRPC APIs. · `controller/internal/snapshots/manager.go`
- [Protocol contracts](https://github.com/thudm/agentrl/blob/main/proto/controller_v1/controller.proto): protobuf · Shared controller and common API contracts. · `proto/controller_v1/controller.proto`

### Task Workers

Python-hosted task environments.

- [Worker entry](https://github.com/thudm/agentrl/blob/main/worker/src/agentrl/worker/__main__.py): Python service · `worker/src/agentrl/worker/__main__.py`
- [TaskWorker](https://github.com/thudm/agentrl/blob/main/worker/src/agentrl/worker/task_worker.py): task service · Owns task lifecycle, prompts, response execution, and rewards. · `worker/src/agentrl/worker/task_worker.py`
- [Worker gRPC transport](https://github.com/thudm/agentrl/blob/main/worker/src/agentrl/worker/task_worker_grpc.py): gRPC client · Workers initiate controller communication. · `worker/src/agentrl/worker/task_worker_grpc.py`
- [Task environments](https://github.com/thudm/agentrl/blob/main/worker/src/agentrl/worker/task.py): deployment-specific tasks · `worker/src/agentrl/worker/task.py`
- [Environment deployment](https://github.com/thudm/agentrl/blob/main/worker/src/agentrl/worker/environment/docker.py): Docker/Kubernetes · Starts task services using container or Kubernetes backends. · `worker/src/agentrl/worker/environment/docker.py`
- [External state providers](https://github.com/thudm/agentrl/blob/main/worker/src/agentrl/worker/environment/state/consul.py): Consul/etcd/Redis · Optional durable task-environment state. · `worker/src/agentrl/worker/environment/state/consul.py`

### Training Pipeline

Ray-based asynchronous RL training.

- [Async trainer](https://github.com/thudm/agentrl/blob/main/examples/training/async_trainer.py): Ray training entry · `examples/training/async_trainer.py`
- [Task scheduler](https://github.com/thudm/agentrl/blob/main/trainer/src/agentrl/trainer/components/task_manager.py): training component · Schedules tasks and coordinates asynchronous work. · `trainer/src/agentrl/trainer/components/task_manager.py`
- [SGLang rollout pool](https://github.com/thudm/agentrl/blob/main/trainer/src/agentrl/trainer/workers/async_sglang_worker.py): Ray actors · Generates asynchronous task trajectories. · `trainer/src/agentrl/trainer/workers/async_sglang_worker.py`
- [Trajectory buffer](https://github.com/thudm/agentrl/blob/main/trainer/src/agentrl/trainer/components/buffer.py): shared buffer · `trainer/src/agentrl/trainer/components/buffer.py`
- [FSDP policy & reference pool](https://github.com/thudm/agentrl/blob/main/trainer/src/agentrl/trainer/workers/fsdp_worker.py): Ray/PyTorch actors · Updates the policy against a frozen KL baseline. · `trainer/src/agentrl/trainer/workers/fsdp_worker.py`
- [NCCL parameter refresh](https://github.com/thudm/agentrl/blob/main/trainer/src/agentrl/trainer/components/nccl_tensor_comm.py): tensor transport · Streams updated policy parameters to rollout replicas. · `trainer/src/agentrl/trainer/components/nccl_tensor_comm.py`

### Evaluation

Multi-model task evaluation CLI.

- [Evaluation CLI](https://github.com/thudm/agentrl/blob/main/eval/src/agentrl/eval/__main__.py): Python CLI · Runs and resumes multi-model, multi-task evaluations. · `eval/src/agentrl/eval/__main__.py`
- [Evaluation sessions](https://github.com/thudm/agentrl/blob/main/eval/src/agentrl/eval/session/runner.py): session orchestration · Coordinates controller tasks and event streaming. · `eval/src/agentrl/eval/session/runner.py`
- [Model-provider clients](https://github.com/thudm/agentrl/blob/main/eval/src/agentrl/eval/client/openai.py): OpenAI-compatible API · `eval/src/agentrl/eval/client/openai.py`

## Connections

- Controller entry → Session & worker coordinator: starts
- Session & worker coordinator → Controller APIs: serves
- Session & worker coordinator → Cluster membership: coordinates through
- Session & worker coordinator → Embedded dashboard: serves
- Session & worker coordinator → Snapshot manager: persists snapshots
- Controller APIs → Protocol contracts: implements
- Worker entry → TaskWorker: starts
- TaskWorker → Task environments: hosts
- TaskWorker → Worker gRPC transport: uses
- Worker gRPC transport → Controller APIs: registers and exchanges sessions
- Worker gRPC transport → Protocol contracts: uses bindings
- Task environments → Environment deployment: deploys via
- Task environments → External state providers: stores state in
- Async trainer → Task scheduler: configures
- Task scheduler → SGLang rollout pool: schedules
- SGLang rollout pool → TaskWorker: runs tasks against
- SGLang rollout pool → Trajectory buffer: writes trajectories
- Trajectory buffer → FSDP policy & reference pool: feeds GRPO/PPO updates
- FSDP policy & reference pool → NCCL parameter refresh: publishes parameters
- NCCL parameter refresh → SGLang rollout pool: refreshes replicas
- Evaluation CLI → Evaluation sessions: runs
- Evaluation sessions → Controller APIs: creates task sessions
- Evaluation sessions → Model-provider clients: requests agent responses
