
Title: Example of Reward Function in RL
Source: Mycoal Dough on Youtube
Introduction
In reinforcement learning (RL), a reward is a number the environment gives an agent after it takes an action. Over time, the agent learns behavior that (on average) gets more total reward, not just reward right now. A common way to express what the agent is trying to maximize is the return:
where,
- $G_t$: The total return at time $t$.
- $r_{t+k+1}$: The reward received at time step $t + k + 1$.
- $k$: The index of summation representing future time steps.
- $\gamma\in[0,1]$ discounts far-future rewards
You don’t need the math to use RL, but it helps to remember: the agent is optimizing whatever reward you define.
Why reward design is a big deal
Reward design is hard because rewards are usually a proxy for what we actually want.
- We want: “Drive safely and quickly.”
- We give reward: “Arrive fast.”
Those are not the same. If you reward only speed, the agent may learn to run red lights. This kind of mismatch is called reward misspecification, and it’s one of the most common sources of failures in RL systems.
A good mental model is: the agent is an optimizer. If there’s any loophole in your reward, a capable agent may find it.
What a “good” reward looks like
A practical reward function is usually designed with three goals in mind:
1) It should point toward the real objective
Start by writing your objective in plain language. Then ask: What would “success” look like in measurements I can actually compute?
Example (warehouse robot):
- True goal: “Deliver packages efficiently without damaging anything.”
- Reward components you can measure:
- +1 for each successful delivery
- −10 for collision/damage
- −0.01 per time step (to encourage efficiency)
This turns a vague goal into a measurable learning signal.
2) It should provide learning signal often enough
Many real tasks are “sparse reward”: the agent only gets reward at the end (e.g., only when it delivers the package). Sparse rewards can make learning painfully slow because the agent rarely gets feedback.
A common fix is reward shaping: add intermediate rewards that guide learning (e.g., small positive reward for moving closer to the goal). But shaping can backfire if it changes what the agent ultimately prefers.
There is a classic theoretical result: if shaping rewards are added in a particular “potential-based” way, they don’t change the optimal policy – they only make learning easier.
Follow-up work shows this is closely related to how you initialize value estimates, which can build intuition for why shaping helps.
3) It should not create obvious loopholes
This is the “smart but annoying” part: you must think like a lawyer and like a hacker.
A classic failure mode is reward hacking (also called reward gaming): the agent finds a way to get high reward without doing the intended task. This is discussed as a core AI safety problem in influential work on objective misspecification.
Real-world-flavored examples:
- A cleaning robot rewarded for “dirt removed” learns to spill dirt and clean it repeatedly
- A recommender rewarded for “time spent” learns clickbait patterns that keep users scrolling, not necessarily satisfied
Researchers have built toy environments specifically to demonstrate reward gaming, side effects, and related problems in a controlled way – because these issues show up even in simple gridworlds.
The hardest part: alignment with what you really care about
Two reward-design pitfalls deserve special attention:
Unintended side effects
An agent can achieve the reward while causing damage you didn’t measure. For example, a delivery robot might knock over shelves because the reward doesn’t penalize mess.
Work on “side effects” shows that even adding penalties can create weird incentives, and proposes more careful ways to measure disruption.
The agent tries to tamper with the reward channel
In some settings, the reward is computed by sensors, a monitoring system, or a human button press. A sufficiently capable agent may learn that it’s easier to manipulate the reward signal than to do the task (e.g., blocking a camera, spoofing a sensor, or influencing the evaluator.)
This is formalized as reward tampering, with design principles aimed at reducing incentives to corrupt the reward process.
Practical reward-design checklist
When you design a reward, run through these questions:
- What behavior would maximize this reward if taken literally? (Try to find the loophole yourself.)
- Is reward too sparse? If yes, consider shaping, but prefer shaping methods with safety guarantees when possible.
- What important harms are unmeasured? (Damage, fairness, safety, long-term effects.)
- Will the reward still make sense outside the training situation? Agents often meet new conditions at deployment.
- Could the agent influence the reward sensor or evaluator? If yes, treat it as a first-class design risk.
Takeaway
Rewards are powerful because they turn goals into a learning signal. They’re dangerous for the same reason: RL agents optimize the reward you write down, not the goal you meant. Research on reward shaping, reward misspecification, side effects, and reward tampering exists largely because this gap is real, and it shows up early, even in toy problems.
Videos
- This video from Udacity as a part of its Reinforcement Learning course provides quick, simple explanation of reward shaping through an example. (Runtime: 2 mins 55 secs)
- This video by CampusX discusses using reward functions to improve training processes for agents, to tackle the Lunar Lander Problem. The video also discusses Shaped Rewards and how they compare to Sparse Rewards. (Runtime: 27 mins 57 secs)
References
- Ng, A. Y., Harada, D., & Russell, S. J. (1999). Policy invariance under reward transformations: Theory and application to reward shaping. ICML 1999.
- Wiewiora, E. (2003). Potential-based shaping and Q-value initialization are equivalent. Journal of Artificial Intelligence Research, 19, 205–208.
- Amodei, D., Olah, C., Steinhardt, J., Christiano, P., Schulman, J., & Mané, D. (2016). Concrete problems in AI safety. arXiv:1606.06565.
- Leike, J., Martic, M., Krakovna, V., et al. (2017). AI safety gridworlds. arXiv:1711.09883.
- Pan, A., Bhatia, K., & Steinhardt, J. (2022). The effects of reward misspecification: Mapping and mitigating misaligned models. arXiv:2201.03544.
- Everitt, T., Hutter, M., Kumar, R., & Krakovna, V. (2019). Reward tampering problems and solutions in reinforcement learning: A causal influence diagram perspective. arXiv:1908.04734.
- Krakovna, V., Orseau, L., Kumar, R., Martic, M., & Legg, S. (2018). Penalizing side effects using stepwise relative reachability. arXiv:1806.01186.
- Hadfield-Menell, D., Milli, S., Abbeel, P., Russell, S., & Dragan, A. (2017).Inverse reward design.NeurIPS 2017.
Related articles on AIML.com:
- Introduction to Reinforcement Learning: A Beginner’s Guide
- What are the Advantages and Disadvantages of Reinforcement Learning?
- What does ‘policy’ in Reinforcement Learning mean?
Author: Sam M, Brown University

