RL in Production
Off-policy evaluation, reward delay, sim-to-real, safe RL, when NOT to use RL
You have trained a stellar RL agent in simulation — 95th percentile performance on your evaluation benchmark. You deploy it to production. Within 2 days, performance drops 40%. The agent has found a way to collect rewards that technically satisfies your reward function but does not serve users. This is not a simulation failure — it is the fundamental difficulty of RL in production: specification, distribution shift, and safety.
Four production failure modes, and their fixes. First: reward hacking. The agent optimizes the letter, not the spirit, of the reward. "Maximize clicks" becomes clickbait that never delivers value. Fix: before training, spend 2 hours listing every way an agent could maximize the reward without satisfying the underlying objective. For each exploit, add a penalty term or constraint. Second: distribution shift. The simulator does not match production — agents learn to exploit simulator artifacts. Fix: domain randomization, sim-to-real transfer, real-data offline RL. Third: catastrophic forgetting. Online updates overwrite good behavior. Fix: experience replay, behavioral cloning on a buffer of historically good trajectories. Fourth: exploding Q-values. RL can be unstable without careful configuration. Fix: gradient clipping, target networks, reward normalization.
Safe exploration matters in production because exploratory actions can cause real harm. A bad recommendation alienates a user. An incorrect drug dose harms a patient. Constrained RL formulations add a safety metric as a constraint: optimize reward subject to the constraint being satisfied. Conservative policies require human approval for actions below a confidence threshold.
Offline RL addresses sample efficiency and safety together by training entirely on logged historical data without new environment interaction. Conservative Q-Learning (CQL), Implicit Q-Learning (IQL), and Decision Transformer learn from fixed datasets — critical when real environment interaction is expensive or risky.
NOT this: if the agent performs well in simulation, it will perform well in production. Simulation fidelity is never perfect. RL agents are brittle to distribution shift in ways that supervised learning models are not — the policy was trained to optimize in the simulated world, and optimization finds every crack in the simulation. Always A/B test with limited traffic before full deployment, and monitor for the specific reward hacking patterns your reward function makes possible.
Key points
- Red-team the reward function before training — list every way an agent could maximize the reward without satisfying the underlying objective. This adversarial analysis takes 2 hours and prevents the most common production failures. For each exploit, add a penalty term or constraint before training begins. Reward hacking patterns are almost always predictable in advance if you think adversarially.
- Always maintain a hard fallback policy that activates if the RL agent's action confidence drops below a threshold. RL agents degrade unpredictably. A simple rule-based or supervised learning fallback prevents a partial RL failure from becoming a total outage. Never deploy an RL agent without it. This is non-optional.
- In production, compare the RL agent's average reward per episode against a simple heuristic policy — if the RL agent underperforms the heuristic after deployment, you have distribution shift. Training distribution and deployment distribution diverged. The agent learned to optimize for the training world, which differs from the real one. First diagnostic step: compare on the heuristic baseline, then investigate what changed in the deployment distribution.
- Off-policy evaluation (OPE) estimates a new policy π_e's performance from data already logged under the old policy π_b, without deploying it live. The importance-sampling (IS) estimator reweights each logged outcome by how much more or less likely π_e was to take that action: V̂^IS = (1/N) Σ_n w_n·r_n where w_n = π_e(a_n|s_n)/π_b(a_n|s_n). For a single-step bandit this is manageable, but for a T-step sequential MDP the trajectory-level weight is a product Π_t w_t — variance grows exponentially in T, and delayed rewards make this worse by stretching T further before a reward lands. The doubly robust (DR) estimator fixes this by combining a fitted reward model R̂(s,a) with an IS correction on the residual; it stays consistent if EITHER the reward model OR the importance weights are accurate — hence "doubly robust" — which is why it dominates plain IS in practice.
The most important production RL skill is red-teaming your reward function before training — listing every way an agent could maximize the proxy without satisfying the actual objective, then adding penalties for each exploit before a single training step runs.
Recap
- Sim 95th percentile -> 40% drop in 2 days: specification, distribution shift, and safety are the real difficulty.
- Four failure modes: reward hacking, distribution shift, catastrophic forgetting, exploding $Q$-values — each with a fix.
- Red-team the reward before training: 2 hours listing every exploit, add a penalty per exploit. #1 skill.
- Always keep a hard fallback policy that activates below a confidence threshold — non-optional.
- Safe/constrained RL: optimize reward subject to a safety constraint; conservative policies need human approval.
- Offline RL (CQL, IQL, Decision Transformer) trains on logged data — critical when real interaction is risky.
- RL agents are brittle to distribution shift in ways supervised models aren't; A/B test on limited traffic first.
Check your understanding
Q1. You have logged data from a recommendation policy π_b (ε-greedy with ε=0.3) and want to estimate the click-through rate of a new model π_e. You have 10M impressions and 10 candidate items per request. Describe the IS estimator, its variance problem for long horizon, and why DR is better.
- A) Use the IS estimator V̂^IS = (1/N) Σ_n (π_e(a_n|s_n)/π_b(a_n|s_n)) r_n; with ε=0.3 and 10 items, off-policy actions have weights up to ~33 (manageable at 10M samples for single-step bandits); for sequential MDPs with T steps the trajectory-level weight is Π_t w_t which can be 33^T — exponential variance; the DR estimator V̂^DR combines a fitted reward model R̂ with IS correction and is consistent if either the model or IS weights are accurate, making it more robust than either alone
- B) The IS estimator is unbiased and with 10M samples has negligible variance; there is no meaningful difference between IS and DR for this problem size; use IS for simplicity
- C) IS estimation is invalid when π_b is ε-greedy because ε-greedy is not a proper probability distribution; use only the Direct Method (reward model) for evaluation and ignore the IS estimator entirely
- D) The IS estimator always has zero variance when the behaviour policy π_b is known exactly; variance only occurs when π_b must be estimated from data; with a known ε-greedy policy the IS estimator is both unbiased and zero-variance
Q2. Your RL agent for robot manipulation works perfectly in simulation (95% success rate) but achieves only 20% in the real lab. Which two of the following are independent sources of the sim-to-real gap, each needing a distinct fix?
- A) Physics mismatch — friction, mass, and contact dynamics don't match reality; fix via system identification and domain randomisation of physical parameters during training
- B) Observation discrepancy — rendered images differ from real camera output in lighting and texture; fix via domain randomisation of visual appearance or a sim-to-real image transfer model
- C) The choice of optimizer (Adam vs SGD) used during simulated training, which is what actually determines whether the learned policy generalizes to real actuators
- D) The reward function's numerical scale, which must always be normalized to [-1,1] regardless of the task or it will fail to transfer to any real robot
Q3. A team proposes using RL for a clinical trial treatment assignment (which treatment to give each patient each day). What are the specific risks, and what alternative framework would you recommend?
- A) RL is appropriate for clinical trials as long as the reward function is carefully designed to include both short-term and long-term health outcomes; the main risk is reward hacking, which can be mitigated with a KL penalty to the standard of care
- B) RL works for clinical trials but requires offline RL (not online RL) to avoid safety risks; use CQL with conservative Q-value estimation to ensure the policy stays within the safe action distribution observed in historical patient data
- C) RL is inappropriate here due to: delayed outcomes (months-long credit assignment), non-stationary patient populations, catastrophic (not expected-value) safety requirements, low sample counts (hundreds not millions), regulatory interpretability requirements, and distribution shift from the changing policy; recommend contextual bandits with Thompson sampling + posterior-based safety arm exclusion, or a Bayesian adaptive clinical trial design (REMAP-style response-adaptive randomisation) which is FDA-recognised and provides both statistical validity and adaptivity
- D) The main risk is that RL requires too many patient interactions to learn a good policy; the fix is to use transfer learning from existing clinical trial data to pre-train the policy before deploying it in a new trial, which reduces the number of patients needed to fewer than 100
Try it interactively
ML Systems Lab is a free interview-prep platform for ML engineers — work through the full interactive module, quizzes, and drills.
Open ML Systems Lab →