# LoongSage

> Baidu's agentic RL post-training framework on Ray, Megatron-Core and SGLang, with tested recipes

- Category: Training & Fine-Tuning / RLHF & Alignment
- Language: Python
- Licence: Apache-2.0
- Repository: https://github.com/baidu-baige/LoongSage
- Website: https://docs.loongsage.org
- Canonical page: https://ai-tldr.dev/tools/loongsage/
- Updated: 2026-11-24

## In short

LoongSage is a reinforcement-learning post-training framework for large language models, published by Baidu under its baidu-baige GitHub organisation. It sits in the RLHF & Alignment category of the Training & Fine-Tuning tools and is open-source, written in Python and licensed Apache-2.0. To get started: pull and start the container.

## Overview

LoongSage is a reinforcement-learning post-training framework for large language models, published by Baidu under its baidu-baige GitHub organisation. It uses Ray for scheduling, Megatron-Core as the training backend and SGLang as the inference (rollout) backend, and it is aimed at agentic RL on very large models — the validated recipes include DeepSeek-V4-Flash, DeepSeek-V4.1-Flash and ERNIE-5.5-Flash-Base as well as Qwen3 models down to a single-GPU Qwen3-0.6B example. The code acknowledges slime as its main architectural reference, with ideas from verl and Agent Lightning.

Its central design choice is zero-intrusion agent access. An agent only has to call the framework's inference service through a standard chat-completion interface; the built-in AgentFlow module handles multi-turn context, tool-output recognition, loss masking, trajectory collection and sandbox lifecycles. That is how the project trains black-box coding agents — OpenCode, Codex and Claude Code — with RL without changing their code. A single controller drives training, sampling, teacher inference and weight synchronisation across Ray actors.

The framework puts weight on keeping training and inference consistent, which matters most for mixture-of-experts models and low precision. It ships router replay for experts, an FP32 output layer, importance-sampling correction and other alignment techniques, and monitors the train–inference probability gap live; the README reports that gap held around 1e-4. Training can run fully asynchronously with partial rollout and oversampling, which the team reports raised throughput 39% in its experiments, and weights move between engines over CUDA IPC when colocated or NCCL across nodes.

Beyond RL algorithms such as DAPO, GRPO and PPO it supports SFT and on-policy distillation (PG-style, GKD-style, full-vocabulary and multi-teacher), mixed training across several data sources, image-modality RL and quantisation-aware training for DeepSeek-V4.1. Twelve extension points — agents, rewards, sandboxes, advantage estimators, losses, KL terms and more — load from a custom directory without touching the core. It is Apache-2.0 licensed and was open-sourced in August 2026, with a technical report on arXiv.

## What it does

- Any agent trains through a plain chat-completion endpoint; AgentFlow handles multi-turn context, loss masks, trajectories and sandboxes
- Ray + Megatron-Core + SGLang stack with a single controller and colocated or disaggregated GPU placement
- Train–inference consistency tooling for MoE: router replay, FP32 output layer, importance-sampling correction and live bias monitoring
- Fully asynchronous training with partial rollout, oversampling and sliding-window staleness control
- On-policy distillation (PG, GKD, full-vocabulary, multi-teacher), SFT and multi-data-source mixed training
- Tested recipes under conf/ for DeepSeek-V4/V4.1-Flash, ERNIE-5.5-Flash-Base and Qwen3 models

## Getting started

The project recommends its container image, which pins CUDA, PyTorch, Megatron-Core, Megatron-Bridge, SGLang and Ray and applies its SGLang patches. The first-run example below trains Qwen3-4B with DAPO on one 8-GPU node, as in the README.

1. Pull and start the container

```bash
docker pull loongsage/loongsage:latest

docker run -it --gpus all --ipc=host --network=host \
  -v /path/to/workspace:/root loongsage/loongsage:latest bash
```

2. Download a model and dataset

```bash
hf download Qwen/Qwen3-4B --local-dir /root/Qwen3-4B
hf download Haitao999/DAPO-Math-17k-unique --repo-type=dataset \
  --local-dir /root/DAPO-Math-17k-unique
```

3. Launch a recipe

   Every run goes through examples/start.sh with a Hydra config name from conf/ plus any overrides; logs land in log/trainer_<timestamp>.log.

```bash
bash examples/start.sh qwen3_4b/dapo_h800_1node \
  hf_model_path=/root/Qwen3-4B \
  data_source.dataset.prompt_data_path=/root/DAPO-Math-17k-unique
```

## When to use it

- RL post-training of large MoE models where train–inference mismatch can collapse a run
- Training an existing coding agent (OpenCode, Codex, Claude Code) with RL without rewriting the agent
- On-policy or multi-teacher distillation into a smaller model at cluster scale
- Starting from a recipe that has already converged on known hardware instead of tuning hyperparameters from scratch

## Version history

- 2026-11-24 v0.2.1: Adds vision RL for DeepSeek-V4.1, ERNIE-5.5-Flash-Base recipes, group-level rewards and a single-GPU Countdown example.
