TLDR: GRACE (Generating Rewards As Code) is a new framework that uses Large Language Models (LLMs) and an evolutionary search process to create understandable, code-based reward functions for AI agents. Unlike traditional “black-box” methods, GRACE learns these reward functions directly from expert demonstrations, making them easy to interpret and debug. It has been shown to be highly accurate, sample-efficient, and capable of generating strong policies in complex environments like BabyAI and AndroidWorld, even outperforming other methods with significantly less data. A key benefit is the creation of reusable code modules for multi-task learning.
In the world of Artificial Intelligence, particularly in Reinforcement Learning (RL), agents learn by receiving rewards for their actions. Traditionally, these reward functions are designed manually, which can be a complex and error-prone process. An alternative, Inverse Reinforcement Learning (IRL), aims to automatically figure out these reward functions by observing expert behavior. However, many existing IRL methods produce “black-box” models that are difficult for humans to understand or verify.
A new research paper introduces GRACE (Generating Rewards As Code), a novel framework that tackles this challenge by using Large Language Models (LLMs) to create interpretable, code-based reward functions directly from expert demonstrations. This means the reward function isn’t a mysterious algorithm, but actual executable code that can be easily inspected and understood by humans.
How GRACE Works: A Three-Phase Approach
GRACE operates through an iterative, three-phase process:
Phase 1: Goal States Identification. The process begins with an LLM analyzing both expert demonstrations and random trajectories. Its primary task here is to pinpoint the “goal states” within the expert demonstrations – essentially, what success looks like. It also generates an initial set of Python-based reward functions designed to assign high values to these identified goal states and low values to non-goal states.
Phase 2: Reward Refinement through Evolutionary Search. This phase is where the reward functions get smarter. GRACE employs an evolutionary search, where the LLM iteratively refines the reward code. It does this by “mutating” the existing code based on examples where the current reward function failed (e.g., misclassifying a state). A “fitness function” measures how well a reward function distinguishes between goal and non-goal states, guiding the LLM to improve its code over many generations.
Phase 3: Active Data Collection via Reinforcement Learning. Once a good reward function is found, it’s put to the test. An RL agent is trained using this reward function to explore the environment and generate new trajectories. These new trajectories are then fed back into Phase 1 and 2, allowing GRACE to discover new edge cases or unexpected behaviors, further refining the reward function. This loop continues until the agent achieves a desired level of performance.
Also Read:
- Crafting Smarter Control Systems: A New Approach with AI and Optimization
- Automating Reward Design for Stable Imitation Learning
Key Advantages and Findings
The researchers validated GRACE on two distinct environments: BabyAI, a procedurally generated maze environment, and AndroidWorld, a real-world device control simulator. The results highlight several significant advantages:
- High Accuracy and Sample Efficiency: GRACE successfully recovers accurate reward models, even with very few expert demonstrations. It showed non-trivial performance with just a single demonstration and achieved near-perfect scores with only eight expert trajectories, a stark contrast to traditional deep IRL methods that often require vast amounts of data.
- Superior Policy Learning: GRACE consistently matched or outperformed established IRL approaches like GAIL and, in some cases, even online RL trained with ground-truth rewards. This demonstrates that the interpretable, code-based rewards are highly effective for training capable AI agents.
- Well-Shaped Rewards: The framework can produce “well-shaped” rewards that provide informative intermediate signals, not just a final success criterion. This dramatically accelerates learning, especially for complex, long-horizon tasks.
- Code Reusability: A unique benefit of representing rewards as code is the natural emergence of reusable functions. As GRACE evolves, it shifts from creating new code modules to heavily reusing effective, high-level modules it has already discovered, fostering efficient multi-task generalization.
While GRACE offers significant advancements, the paper also discusses limitations, such as the potential for LLMs to introduce false positives during goal state identification and the challenges of programmatic rewards in highly perceptual, high-dimensional input domains. However, the framework’s ability to generate transparent, verifiable, and effective reward functions marks a substantial step forward in making AI systems more understandable and trustworthy.
For more in-depth technical details, you can read the full research paper: GRACE: A Language Model Framework for Explainable Inverse Reinforcement Learning.


