|
| 1 | +--- |
| 2 | +title: "Architecture" |
| 3 | +description: "Understand how NeMo Gym implements environments as composable server components." |
| 4 | +position: 2.5 |
| 5 | +--- |
| 6 | + |
| 7 | +<Info> |
| 8 | + |
| 9 | +**Goal**: Understand how NeMo Gym implements environments as composable server components. |
| 10 | + |
| 11 | +**Read first**: [Environments](/about/concepts/environments) — what an environment is and how it decomposes into dataset, agent harness, verifier, and state. |
| 12 | + |
| 13 | +</Info> |
| 14 | + |
| 15 | +## How NeMo Gym Implements Environments |
| 16 | + |
| 17 | +NeMo Gym implements environments as composable FastAPI servers that communicate over async HTTP: |
| 18 | + |
| 19 | +| Concept | NeMo Gym Component Implementation | |
| 20 | +|---------|---------------| |
| 21 | +| Dataset | JSONL: Responses API input — each row is a task (a single problem or challenge) for the agent to solve | |
| 22 | +| Agent Harness | FastAPI Agent Server | |
| 23 | +| Verifier + State | FastAPI Resources Server | |
| 24 | +| Model | FastAPI Model Server or managed by your own agent harness | |
| 25 | + |
| 26 | +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. |
| 27 | + |
| 28 | +This gives you flexibility to integrate with your existing models and agents: |
| 29 | + |
| 30 | +- **Bring your own agent** — Integrate your existing agent to use it with any Gym environment components. |
| 31 | +- **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. |
| 32 | +- **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. |
| 33 | + |
| 34 | +## How an Agent Runs a Task in NeMo Gym Environments |
| 35 | + |
| 36 | +Each task attempt flows through three steps. The resulting trajectory is called a *rollout*: |
| 37 | + |
| 38 | +1. **Initialize** — The agent receives a task row from the dataset and initializes a session on the Resources Server, which sets up isolated state for this task. |
| 39 | +2. **Agent Loop** — The agent calls a model for inference, then routes any tool calls to either its own tools or the Resources Server. This repeats until the agent decides the task is complete. |
| 40 | +3. **Verify** — The agent asks the Resources Server to score the attempt. The verifier inspects the final state and returns a reward signal. |
| 41 | + |
| 42 | + |
| 43 | +``` |
| 44 | + Dataset (JSONL - one row per task) |
| 45 | + │ |
| 46 | + ▼ |
| 47 | +┌──────────────────────────────────────────┐ |
| 48 | +│ Agent Server │ |
| 49 | +│ │ |
| 50 | +│ run(): │ |
| 51 | +│ 1. resources.seed_session() ─────────────► Resources Server |
| 52 | +│ 2. agent loop: │ |
| 53 | +│ model.responses() ─────────────► Model Server |
| 54 | +│ resources.my_tool() ─────────────► Resources Server |
| 55 | +│ 3. resources.verify() ─────────────► Resources Server |
| 56 | +└──────────────────────────────────────────┘ |
| 57 | +
|
| 58 | +┌───────────────────────────┐ ┌────────────────────────────────────┐ |
| 59 | +│ Model Server │ │ Resources Server │ |
| 60 | +│ │ │ │ |
| 61 | +│ responses(): │ │ seed_session(): init env state │ |
| 62 | +│ → text, tool calls, │ │ my_tool(): execute action │ |
| 63 | +│ or code │ │ verify(): evaluate → reward │ |
| 64 | +└───────────────────────────┘ └────────────────────────────────────┘ |
| 65 | +``` |
| 66 | + |
| 67 | + |
| 68 | +## Server Types |
| 69 | + |
| 70 | +### Agent Server |
| 71 | + |
| 72 | +Hosts agent harnesses that orchestrate rollouts. Use your own harness, or use built-in harnesses such as OpenHands or NeMo Gym's native harnesses such as Simple Agent. |
| 73 | + |
| 74 | +### Model Server |
| 75 | + |
| 76 | +A stateless LLM inference endpoint that standardizes different model providers behind the Responses API. Supports local inference and inference providers. |
| 77 | + |
| 78 | +### Resources Server |
| 79 | + |
| 80 | +Manages environment-specific tools, per-task state isolation, and verification: |
| 81 | + |
| 82 | +- **Environment-Specific Tools** — capabilities the environment provides to any agent (e.g., code execution, database queries, API calls) |
| 83 | +- **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). |
| 84 | +- **Verification** — scoring logic that evaluates the agent's output and returns a reward |
| 85 | + |
| 86 | +## Where Tools Live |
| 87 | + |
| 88 | +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: |
| 89 | + |
| 90 | +- **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). |
| 91 | +- **Environment-specific tools** are part of the Resources Server. They're capabilities the environment provides to any agent that connects (e.g., a `run_tests` endpoint, a database query tool, a sandbox execution API). |
| 92 | + |
| 93 | +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. |
| 94 | + |
| 95 | +## Communication |
| 96 | + |
| 97 | +Servers communicate over async HTTP (aiohttp) with: |
| 98 | +- **Session cookies** propagated through the call stack for stateful environments |
| 99 | +- **Retry logic** with exponential backoff (3 attempts) |
| 100 | +- **Connection pooling** via a singleton aiohttp client for high-concurrency workloads |
| 101 | + |
| 102 | +## Next Steps |
| 103 | + |
| 104 | +<Cards> |
| 105 | + |
| 106 | +<Card title="Concepts" href="/about/concepts"> |
| 107 | +Understand environments, evaluation, and training before diving into implementation. |
| 108 | +</Card> |
| 109 | + |
| 110 | +<Card title="Browse Environments" href="https://github.com/NVIDIA-NeMo/Gym#-available-environments"> |
| 111 | +Browse available environments for evaluation and training. |
| 112 | +</Card> |
| 113 | + |
| 114 | +<Card title="Agents" href="/agent-server"> |
| 115 | +Explore available agent harnesses and learn how to integrate your own agent. |
| 116 | +</Card> |
| 117 | + |
| 118 | +<Card title="Training" href="/training-tutorials"> |
| 119 | +Improve your agent or model with RL or fine-tuning. |
| 120 | +</Card> |
| 121 | + |
| 122 | +<Card title="Build Custom Environments" href="/environment-tutorials"> |
| 123 | +Create your own evaluation or training environments. |
| 124 | +</Card> |
| 125 | + |
| 126 | +</Cards> |
0 commit comments