Architecture
Goal: Understand how NeMo Gym implements environments as composable server components.
Read first: Environments — what an environment is and how it decomposes into dataset, agent harness, verifier, and state.
How NeMo Gym Implements Environments
NeMo Gym implements environments as composable FastAPI servers that communicate over async HTTP. The server composition depends on the integration:
All components are composable. Use different datasets with the same resources server, the same resources server with different models, or the same model with different agent harnesses.
This gives you flexibility to integrate with your existing models and agents:
- Bring your own agent — Integrate your existing agent to use it with any Gym environment components.
- Use a built-in agent — NeMo Gym includes some native agents, e.g. general-purpose multi-step tool calling, as well as built-in integrations with external harnesses like OpenHands.
- Train with any model endpoint — The Model Server standardizes different LLM endpoints behind the Responses API and provides token IDs and log probabilities needed for RL training.
Why Separate Servers
Many agent frameworks run the agent, its tools, and the model client in one process. NeMo Gym runs each component as its own server instead, for these reasons:
- Isolated dependencies. Each server runs in its own virtual environment, built from that server’s own dependency file. An agent harness and a resources server can depend on conflicting library versions without breaking each other. See Per-server runtime files.
- Independent scaling. Each server runs its own worker processes, so a resources server that runs expensive verification can get more of them without changing the agent or model server. Gym servers run on CPU by default and can be deployed on different nodes from the GPUs that train the model. See Deployment Topology.
- Reuse across evaluation and training. The same agent, resources server, and model server serve
gym eval runand every supported training framework. Changing the trainer or the model doesn’t require changing the environment. - HTTP boundaries that match training frameworks. Training frameworks already serve their policy model through an OpenAI-compatible HTTP endpoint, and the model server calls it the same way it calls any other provider. Episodes reach Gym over HTTP too, so evaluation and training submit work the same way. See Integrate RL Frameworks.
You still start everything with one command: gym env start launches each server and waits until all of them are ready. The servers then talk to each other over async HTTP through a shared, pooled client built for high concurrency. See Communication.
How an Agent Runs a Task in NeMo Gym Environments
Each task attempt is an episode. Rollout collection sends every episode to an Environment Server. An Environment Server that drives agent sessions, such as single_agent_turn, runs it in this order, and the resulting trajectory is called a rollout:
- Seed — The Environment Server receives a task row from the dataset and seeds a session on the Resources Server, which sets up isolated state for this task. It then seeds a session on the Agent Server, passing along any tool and sandbox access that the Resources Server returned.
- Agent Loop — The Environment Server activates the agent session. The agent calls a model for inference, then routes tool calls to either its own tools or the Resources Server. This repeats until the agent decides the task is complete.
- Close and Verify — The Environment Server closes the agent session, then asks the Resources Server to score the attempt. The verifier inspects the final state and returns a reward signal.
- Clean up — The Environment Server closes the resources session, which releases per-task state such as sandboxes.
When an Environment Server drives agent sessions, the Agent Server does not decide when to verify. The Environment Server orders the calls, so a different benchmark, or any agent that implements agent sessions, can be swapped in through configuration, with no pair-specific launcher. The built-in single_agent_turn Environment Server runs one agent turn against one Resources Server. Other interaction patterns, such as a simulated user, define their own Environment Server.
Most shipped simple_agent configurations use single_agent_turn_legacy, which drives these steps for the flat rows that rollout collection sends to /run. A crash then repeats only the current step, not the whole episode. The rest stay on the legacy_agent relay below, because their Resources Server’s seed_session takes only the legacy body or its /verify reads the row’s rollout coordinates. Agents that still run the whole episode in their own /run use the legacy_agent Environment Server, which Gym also generates by default for any agent that no Environment Server names. It relays /run to the agent, and the agent runs the whole episode itself, including any seed_session and verify calls. Cleanup then depends on the agent, so Resources Servers on this path usually release per-task state in verify() (see Session Cleanup). For that request flow, see System Design.
The harbor Environment Server runs tasks defined as task folders in the Harbor format, and no Resources Server takes part. It keeps the same order: seed, activate, close, verify, clean up. A multi-step task repeats the agent turn and its verification per step, in a fresh agent session each unless the server sets turn_sessions: continue. At seed it starts the task’s sandbox itself and hands the agent access to it. After it closes the agent session, it runs the task’s own verifier from tests/ in that sandbox, or in a fresh one, and reads the reward the verifier wrote. It then stops every sandbox it started. To choose between Environment Servers, see Design Agent-Environment Interactions.
Server Types
Environment Server
Receives each episode: rollout collection submits tasks to its /run endpoint. An Environment Server that drives agent sessions, such as single_agent_turn, binds an agent and a Resources Server by reference and decides the order of calls: it seeds the Resources Server and Agent Server sessions, activates the agent, closes the agent session, verifies, and cleans up, including on failure. legacy_agent binds only an agent and relays /run to it. harbor also binds only an agent: it owns the task’s sandbox and runs the task folder’s own verifier. An Environment Server implements no tools, agent logic, or scoring of its own. See Design Agent-Environment Interactions.
Agent Server
Hosts agent harnesses, which call the model and route tool calls. Use your own harness, or use built-in harnesses such as OpenHands or NeMo Gym’s native harnesses such as Simple Agent.
Model Server
A stateless LLM inference endpoint that standardizes different model providers behind the Responses API. Supports local inference and inference providers.
Resources Server
Manages environment-specific tools, per-task state isolation, and verification for tasks defined as JSONL rows. Task folders carry their own verifier and need no Resources Server. For JSONL rows, the Resources Server provides:
- Environment-Specific Tools — capabilities the environment provides to any agent (e.g., code execution, database queries, API calls)
- State Isolation — each rollout gets its own session, so attempts never interfere with each other. Environments range from lightweight (verify a math answer, no setup needed) to heavyweight (provision a Docker container with a specific repo checkout for SWE-Bench-style tasks).
- Verification — scoring logic that evaluates the agent’s output and returns a reward
- Sandbox ownership — when a task needs an isolated runtime, the Resources Server usually creates the sandbox and passes the agent a handle to it. When an Environment Server drives agent sessions, the agent borrows the sandbox and never destroys it; some agents behind
legacy_agentstill stop it themselves. The Resources Server decides whether to verify in the same sandbox or in a fresh one, and destroys each sandbox it owns. This keeps the benchmark’s verification policy the same no matter which agent runs the task.
Where Tools Live
Tools exist on a spectrum — some belong to the agent and can be used with any environment, some belong to the environment and can be used with any agent:
- Agent-specific tools are part of the agent harness. They’re capabilities the agent brings regardless of which environment it runs in (e.g., OpenHands brings file editing and terminal tools).
- Environment-specific tools can be provided by the Resources Server. They’re capabilities the environment provides to any agent that connects (e.g., a
run_testsendpoint, a database query tool, a sandbox execution API).
An agent can use both simultaneously — its own tools and the environment’s tools in the same task. NeMo Gym’s server split reflects this: agent-specific logic in the harness, environment-specific logic in the Resources Server.
Communication
Gym servers communicate over async HTTP (aiohttp) with:
- Session cookies propagated through the call stack for stateful environments
- Retry logic with exponential backoff (3 attempts)
- Connection pooling via a singleton aiohttp client for high-concurrency workloads
Sandbox execution and file transfer are not Gym HTTP calls. They use the configured sandbox provider’s transport, such as HTTP, gRPC, a Docker CLI, or a local subprocess.
Distributing Heavy Work
Some servers do work too heavy for their own processes, such as running generated code, compilers, or test suites, or scoring rollouts with a judge or reward model. A server can move that work elsewhere. None of these options changes how servers talk to each other: that is always HTTP.
- Sandboxes. The Sandbox API runs work in isolated environments with the CPU, memory, and GPU the server requests. Remote providers, such as a Kubernetes cluster or a cloud service, run those sandboxes on their own machines rather than on the server’s host. Use sandboxes when the work needs isolation or its own runtime.
- Inference endpoints. A server that needs a model, such as an LLM judge, calls a model server by name instead of loading the model itself. The model server can front an inference provider or a dedicated vLLM deployment on its own GPUs, so the calling server stays light.
- Ray. For CPU-heavy work that needs neither isolation nor a model, a server can opt into Ray and spread the work across nodes as Ray tasks. Gym starts or joins a Ray cluster only for servers that opt in, and installs and runs without Ray otherwise. See Scale CPU-Heavy Work with Ray.
Where the Code Lives
Each server type has its own top-level directory: resources_servers/, responses_api_agents/, responses_api_models/, and environment_servers/. What they share lives in the nemo_gym library, which is split into layered packages. For the full map, see Repository Layout.