PhysAgentGym: Physical-Law Verifiers for Training Small Code-Reasoning Agents

Lama MoukheiberNeurIPS 2026

TL;DR

Training AI coding agents usually requires a mountain of human-written unit tests, answer keys, or brittle reward models. PhysAgentGym bypasses this bottleneck by letting the laws of physics act as the grader: an LLM writes code to simulate a physical scenario, and fundamental rules—like conservation of energy or constant gravitational acceleration—automatically score the resulting trajectory. By filtering training rollouts with these annotation-free physical checks, the author lifts a small, open-source 7B model to match frontier giants like GPT-5 and Claude Sonnet 4.5, even on physics phenomena it never saw during training.

What Problem Does This Paper Address?

If you want to train a language model to reason and write code through trial and error, you need a verifier—something that looks at the model's output and says, "Yes, that worked," or "No, try again." In math and standard software engineering, we have plenty of verifiers in the form of exact numeric answers and human-written unit tests.

In the empirical sciences like physics and chemistry, however, large-scale verifiable datasets are scarce. Hiring experts to annotate outputs or write custom unit tests for every new scientific simulation doesn't scale, and learned "reward models" (AIs trained to grade other AIs) tend to make mistakes of their own. Yet nature already provides the ultimate, objective rulebook. The central question of this paper is simple: where a domain is governed by closed-form physical laws, can those laws themselves serve as automatic, zero-annotation verifiers to train smaller AI agents?

Methods

Overview of the PhysAgentGym pipeline, from scenario to physical-law verification and small model training.

Figure 1: PhysAgentGym verifies with physical laws instead of expert labels, unit tests, or reward models. An LLM agent writes code that simulates a scenario, the laws score its trajectory with no answer key, and fine-tuning on the verified rollouts brings a 7B model to the frontier.

The paper introduces PhysAgentGym, an interactive coding environment built around three pieces:

  1. Scenario & Standards: The agent is given a text description of a classical mechanics problem (e.g., an oblique projectile throw) alongside initial conditions and natural-language standards the motion must obey (e.g., "total mechanical energy is conserved").
  2. The ReAct Agent Loop: Using a standard Reason-and-Act loop, the LLM writes a Python script (restricted to NumPy and SciPy) to simulate the physics and output a 2D time-series trajectory. It gets up to six tries to run its code, read diagnostic feedback, and refine its simulation before submitting.
  3. The Physical-Law Verifier (physjudge): This is the novel core. Neither the agent nor the verifier ever sees a reference "answer key" trajectory. Instead, a rule-based compiler pattern-matches each natural-language standard to a library of deterministic physics checks (e.g., fitting a parabola, checking that energy doesn't drift upward, or directly testing Newton's second law). Each check outputs a continuous score in $[0, 1]$ and a binary pass/fail verdict.

To train smaller models, the author collects 556 multi-turn rollouts from Claude Sonnet 4.5 on a subset of tasks, filters them using the verifier (keeping only episodes that pass every check and achieve a mean score $\ge 0.85$), and fine-tunes open Qwen2.5-Coder-Instruct models (1.5B, 3B, and 7B) using QLoRA on the surviving 491 full conversations.

Datasets

The paper introduces two procedural benchmark suites within PhysAgentGym, differing from existing benchmarks (like SWE-bench or JEEBench) by combining interactive code execution, multi-turn feedback, and answer-key-free evaluation:

  • Main Single-Body Suite (1,200 train / 120 test tasks): Covers four subcategories across ten motion regimes. Only two subcategories—Potential–Kinetic Energy Exchange (free fall, throws) and Linear Motion—are used to generate training data. Circular Motion (near out-of-distribution) and Damping (oscillators; far out-of-distribution) are held out entirely to test generalization.
  • Harder Composite Suite (1,200 train / 120 test tasks): Built to push frontier models, featuring quadratic air drag, eccentric two-body Kepler orbits, driven damped oscillators, and three-spring coupled masses. Unlike the main suite, the agent is only given physical constants and initial states, forcing it to set up and numerically solve differential equations.

Note: Every task also generates an exact analytical or high-precision withheld solution, used strictly behind the scenes by the author to audit whether the verifier is doing its job.

Key Insights and Ideas

  • High physical-law scores guarantee real physical accuracy: When the author audited 359 test episodes against the withheld ground-truth trajectories, zero physically wrong trajectories ever scored $\ge 0.9$. Every high-scoring simulation lay within $0.12\%$ of the exact textbook solution.
  • Agents learn the loop and generalize to unseen physics: Because models are fine-tuned on entire multi-turn episodes—including broken code, per-law diagnostic feedback, and fixes—they learn how to debug simulations, not just memorize formulas. That is why the 7B model jumps nearly 49 points on Damping, a physical phenomenon completely absent from its training data.
  • Continuous scores beat binary pass/fail for tiny models: Filtering training data by both passing all checks and clearing a $0.85$ continuous score threshold (removing just 17 borderline rollouts) boosted the 1.5B model's score by $+12.7$ points compared to pass-only filtering.
  • Frontier models proactively tune numerical solvers: On eccentric Kepler orbits, calling SciPy's default ODE solver out-of-the-box yields an $8.7\%$ error (scoring $0.868$). Both Claude Sonnet 4.5 and Opus 4.8 scored $0.998$ because, in all 60 episodes, they autonomously tightened the numerical solver tolerances in their code.

Key Results

Comparison of zero-shot and verifier-trained Qwen2.5-Coder models against the 0.94 frontier baseline.

Comparison of zero-shot and verifier-trained Qwen2.5-Coder models against the 0.94 frontier baseline.
  • Reaching the Frontier: Zero-shot Qwen2.5-Coder models score 0.000 (1.5B), 0.011 (3B), and 0.668 (7B), largely failing to format valid actions or trajectories. After fine-tuning on verifier-selected rollouts, they jump to 0.702 (1.5B), 0.888 (3B), and 0.942 (7B)—putting the 7B model in a statistical tie with Claude Sonnet 4.5 (0.944), Claude Opus 4.8 (0.944), and GPT-5 (0.937–0.944).
  • Out-of-Distribution Generalization: On held-out Circular Motion, the trained 7B model matches Sonnet 4.5 (0.911). On far-OOD Damping, it climbs from 0.440 zero-shot to 0.927 (nearly matching Sonnet's 0.935).
  • Separating Frontier Models: On the main suite, strict finite-difference smoothness checks cap even the exact textbook solution at 0.944, creating a ceiling effect. On the harder Composite Suite, exact solutions score ~0.99, wrong physics scores $\le 0.76$, and Claude Opus 4.8 (0.995) measurably pulls ahead of Sonnet 4.5 (0.990).

Limitations and Future Work

  • Domain boundaries: Physical-law verification only works where exact, computable invariants exist for a single trajectory. Chaotic systems (like double pendulums), contact-rich collisions, random processes (diffusion), and turbulence are out of scope.
  • Verifier blind spots and hand-crafting: Each new class of phenomenon still requires a human to write ~12-line check functions, and rewording a rule can alter which pattern triggers. In the main suite, no check verified the initial starting position—allowing six 7B Ferris-wheel trajectories with shifted centers to slip through with moderate scores (0.76–0.89).
  • Scale and training setup: Training at 1.5B parameters is noisy across seeds, and all fine-tuning here relied on supervised distillation from a single teacher (Sonnet 4.5).

For future work, the most natural next step is plugging physjudge directly into reinforcement learning (RL) post-training, letting small models learn from their own scored rollouts rather than a frontier teacher's, and extending law-based verifiers to fields like chemistry (atom conservation) or signal processing (Parseval's identity).

Summary updated

Abstract

Code-reasoning agents trained from rollouts rely on verifiers, such as expert an- notation, hand-written unit tests, or learned reward models, each of which needs human effort for every new domain. However, in domains governed by closed- form physical laws, whether the laws themselves can serve as automatic verifiers remains unclear. We propose PhysAgentGym, an agentic code-execution gym for classical mechanics of point masses, in which a language model writes a Python program that simulates a physics scenario and a rule-based verifier com- piles natural-language standards into checks on the emitted trajectory. The scores are annotation-free, since physical laws supply the ground truth and neither the agent nor the verifier reads the exact solution, and an audit against these withheld solutions shows that every highly scored test trajectory from Claude Sonnet 4.5, Claude Opus 4.8, GPT-5, and our trained 7B model matches its exact solution. Fine-tuning Qwen2.5-Coder on Claude Sonnet 4.5 rollouts selected by the verifier brings the 7B model statistically on par with Sonnet 4.5 and GPT-5. It matches Sonnet 4.5 on held-out Circular Motion and comes close on held-out Damping, a phenomenon absent from training. Frontier models already reach the highest score the verifier gives in practice, but on harder composite tasks with drag, orbits, and driven and coupled oscillators, where the exact solution earns nearly the full score, the verifier separates correct from wrong physics and Claude Opus 4.8 scores measurably above Sonnet 4.5.
BibTeX
@inproceedings{moukheiber2026physagentgym,
  author = {Lama Moukheiber},
  title = {{PhysAgentGym: Physical-Law Verifiers for Training Small Code-Reasoning Agents}},
  booktitle = {Advances in Neural Information Processing Systems},
  year = {2026},
  url = {https://openreview.net/forum?id=NV5qjfDXZB},
}
Figure