A contextual bandit is a reinforcement learning algorithm that chooses actions based on a given context (observed features about the user, environment, or situation) to maximize cumulative reward over time. Unlike a standard multi-armed bandit that ignores side information, the contextual variant learns a policy—a mapping from context vectors to action probabilities—enabling personalized decision-making where the optimal action depends on who the user is and what situation they are in.
Glossary
Contextual Bandit

What is a Contextual Bandit?
A contextual bandit is a reinforcement learning algorithm that selects actions based on observed context to maximize cumulative reward, learning a policy that maps situational features to optimal personalized decisions in real-time.
The algorithm operates by observing a context vector x at each step, selecting an action a, and receiving a reward r, then updating its policy to improve future selections. This creates an exploration-exploitation trade-off: the system must balance exploiting known high-reward actions for a given context against exploring uncertain actions to gather data that may reveal even better mappings. Common implementations include LinUCB (which models rewards as a linear function of context) and neural bandits (which use deep networks to capture non-linear context-reward relationships), making them foundational for real-time recommendation and dynamic pricing systems.
Key Contextual Bandit Algorithms
Contextual bandits bridge supervised learning and reinforcement learning by learning a policy that maps observed context features to the optimal action. Here are the foundational algorithms that power real-time personalization.
LinUCB (Linear Upper Confidence Bound)
A foundational algorithm that models the expected reward of each action as a linear function of the context features. It selects actions by computing an upper confidence bound on the predicted reward, analytically adding an exploration bonus proportional to the uncertainty of the parameter estimates.
- Mechanism: Maintains a ridge regression model per arm; the confidence interval width shrinks as more data is collected.
- Exploration bonus: Computed as the standard deviation of the estimate multiplied by a hyperparameter alpha.
- Use case: News article recommendation where user and article features are available.
- Advantage: Computationally efficient with a closed-form update and strong theoretical regret guarantees under linear realizability.
Thompson Sampling (Posterior Sampling)
A Bayesian algorithm that maintains a probability distribution over the reward model parameters for each action. At each step, it samples a set of parameters from the posterior, acts greedily with respect to that sample, and then updates the posterior with the observed reward.
- Mechanism: For linear models, uses a Gaussian posterior over weight vectors; for categorical rewards, uses Beta distributions.
- Key property: Probability of selecting an action matches the probability that it is optimal given current knowledge.
- Advantage: Naturally balances exploration and exploitation without an explicit hyperparameter; empirically outperforms UCB in many settings.
- Variants: Can be extended to deep neural networks via Monte Carlo dropout or ensemble sampling.
Epsilon-Greedy with Context
The simplest contextual bandit strategy: with probability 1 - ε, exploit by selecting the action with the highest predicted reward from a supervised model; with probability ε, explore by selecting a random action uniformly.
- Implementation: Train a regression or classification model on all observed (context, action, reward) tuples to predict expected reward for each candidate action.
- Decay strategies: Often anneal ε over time (e.g., ε = 1/t) to shift from exploration to exploitation.
- Limitation: Explores uniformly without considering uncertainty, wasting trials on clearly suboptimal actions.
- Practical use: A strong baseline in production systems due to simplicity; often paired with warm-starting from logged bandit feedback data.
Neural Contextual Bandits (NeuralUCB, NeuralTS)
Extensions of linear bandit algorithms that use a deep neural network as the reward function approximator, enabling the modeling of complex, non-linear relationships between context and reward.
- NeuralUCB: Uses the network's gradient features as a learned representation and applies a UCB exploration bonus in the penultimate layer's space.
- NeuralTS: Applies Thompson Sampling by treating the neural network's output as a Gaussian process with the neural tangent kernel, sampling from the posterior predictive distribution.
- Challenge: Requires careful tuning of network width and learning rates to maintain calibrated uncertainty estimates.
- Advantage: Can capture feature interactions and non-linear patterns that linear models miss, crucial for high-dimensional user behavior contexts.
Off-Policy Evaluation (IPS, DR, SNIPS)
A suite of statistical techniques for evaluating a new contextual bandit policy using only logged historical data collected under a different (often stochastic) logging policy, without deploying the new policy online.
- Inverse Propensity Scoring (IPS): Reweights observed rewards by the ratio of target policy probability to logging policy probability for the chosen action.
- Doubly Robust (DR): Combines IPS with a direct reward model to reduce variance while maintaining unbiasedness if either the propensity model or reward model is correct.
- Self-Normalized IPS (SNIPS): Normalizes IPS weights to control variance from extreme propensity ratios.
- Critical for safety: Enables offline validation before risking real user experience degradation.
Contextual Combinatorial Bandit
An extension where the agent selects a set or slate of actions simultaneously (e.g., a ranked list of products) rather than a single action, with the reward depending on the entire chosen combination.
- Challenge: The action space grows combinatorially (e.g., choosing 5 items from 10,000), making naive enumeration impossible.
- Solution approaches: Assume reward is a linear combination of individual item rewards, or use submodular maximization with bandit feedback.
- Application: Personalized homepage layout, where the system must select and order a set of widgets or articles.
- Key metric: Often optimized for Cascade Model user behavior, where users scan top-to-bottom and click probability decays with position.
Frequently Asked Questions
Clear, technically precise answers to the most common questions about contextual bandit algorithms, their mechanisms, and their role in real-time personalization systems.
A contextual bandit is a reinforcement learning algorithm that chooses actions based on observed context (a feature vector describing the current user, situation, or environment) to maximize cumulative reward over time. Unlike a standard multi-armed bandit, which learns a single best action for all situations, a contextual bandit learns a policy—a mapping from context to action—allowing it to personalize decisions. For example, a standard bandit might learn that "Product A is always best," while a contextual bandit learns that "Product A is best for users in segment X at time Y, but Product B is best for segment Z." This is achieved by modeling the expected reward as a function of both the action and the context, typically using a parametric model like logistic regression, a neural network, or a gradient-boosted tree. The algorithm observes the context x_t, selects an action a_t, and receives a reward r_t, then updates its model to improve future predictions. This framework directly addresses the exploration-exploitation trade-off in a personalized manner, making it foundational for real-time recommender systems, dynamic pricing, and clinical trial design.
Real-World Applications of Contextual Bandits
Contextual bandits bridge the gap between offline model training and online decision-making, powering systems that learn and adapt from every user interaction in real time.
Dynamic News Recommendation
Modern news aggregators use contextual bandits to personalize article rankings for each visitor. The context includes user demographics, reading history, time of day, and device type. The action is selecting which article to display in a headline slot, and the reward is a click or long dwell time. Unlike static collaborative filtering, the bandit continuously adapts to breaking news and shifting reader interests without waiting for batch retraining. Yahoo! pioneered this approach with their Today module, achieving a 12.5% click-through rate lift over non-contextual methods by balancing the exploration of fresh content against the exploitation of proven high-performers.
Adaptive Clinical Trial Design
In pharmaceutical research, contextual bandits optimize patient treatment assignment in adaptive trials. The context is a patient's genomic markers, disease progression metrics, and demographic profile. The action is assigning a treatment arm, and the reward is a positive therapeutic outcome. By using a Thompson Sampling policy, the system allocates more patients to promising treatments while continuing to explore alternatives for under-represented subgroups. This approach reduces the number of patients exposed to ineffective treatments and accelerates the identification of responder populations, directly addressing the ethical and economic costs of traditional fixed-randomization trials.
E-Commerce Homepage Optimization
Large retailers deploy contextual bandits to orchestrate every component of a landing page. The context vector encodes the user's referral source, loyalty tier, real-time session behavior, and current inventory levels. The action space is combinatorial: selecting a hero banner, a promotional offer, and a product carousel layout simultaneously. The reward is a composite of add-to-cart events and revenue. This replaces rigid A/B testing with a system that learns the optimal page configuration for each micro-segment. A key architectural challenge is managing the delayed reward problem, where a purchase may occur days after the initial impression, requiring inverse propensity scoring for unbiased offline evaluation.
Mobile Notification Timing
Contextual bandits determine the optimal moment and message content for push notifications to prevent user churn. The context captures the user's timezone, app engagement history, battery level, and current activity inferred from accelerometer data. The action is choosing whether to send a notification and which copy variant to use. The reward is the user opening the app within a defined attribution window. A critical nuance is modeling the negative reward of sending a notification that causes the user to disable notifications entirely, framing the problem as a constrained optimization where long-term channel health is balanced against short-term engagement.
Cloud Resource Auto-Scaling
Infrastructure platforms use contextual bandits to dynamically allocate compute resources. The context is the current server load, request queue depth, and time-of-day traffic patterns. The action is provisioning or de-provisioning virtual machine instances, and the reward is a function of response latency and energy cost. A contextual bandit outperforms static threshold-based autoscaling by learning non-linear relationships between traffic composition and resource needs. For example, a video transcoding workload may require GPU allocation only when specific file formats appear in the queue, a pattern a bandit can learn without explicit rules.
Personalized Education Pathways
Adaptive learning platforms employ contextual bandits to sequence curriculum modules. The context is the student's knowledge state estimated from assessment performance, learning style indicators, and engagement metrics. The action is selecting the next learning activity from a pool of candidates, and the reward is a correct answer or skill mastery demonstration. The system must handle the cold start problem for new students by leveraging population priors while rapidly individualizing. Crucially, the bandit's exploration policy must be constrained by pedagogical guardrails to prevent exposing a struggling student to material far beyond their current zone of proximal development.
Contextual Bandit vs. Related Approaches
A comparison of the contextual bandit framework against classical supervised learning, non-contextual multi-armed bandits, and full reinforcement learning across key operational dimensions.
| Feature | Contextual Bandit | Multi-Armed Bandit | Supervised Learning | Full RL (MDP) |
|---|---|---|---|---|
Uses Context (State) | ||||
Learns from Feedback | Partial (reward for chosen action only) | Partial (reward for chosen arm only) | Full (correct label provided) | Partial (reward + next state) |
Handles Delayed Rewards | ||||
Exploration Mechanism | Inherent (e.g., epsilon-greedy, Thompson sampling) | Inherent (e.g., UCB, epsilon-greedy) | None (requires explicit collection) | Inherent (policy gradient, Q-learning) |
Primary Objective | Maximize cumulative reward | Maximize cumulative reward | Minimize prediction error | Maximize long-term discounted return |
Counterfactual Learning | Core capability (off-policy evaluation) | Core capability | Not applicable | Supported (off-policy RL) |
Cold Start Strategy | Optimistic initialization or prior-based sampling | Uniform exploration or prior-based sampling | Content-based features or fallback | Random policy or demonstration data |
Typical Latency Budget | < 10 ms per decision | < 5 ms per decision | < 50 ms per prediction | Varies (often > 100 ms for planning) |
Enabling Efficiency, Speed & Accuracy
Intelligent Analysis, Decision & Execution
We build AI systems for teams that need search across company data, workflow automation across tools, or AI features inside products and internal software.
Talk to Us
Search across company data
Give teams answers from docs, tickets, runbooks, and product data with sources and permissions.
Useful when people spend too long searching or get different answers from different systems.

Automate internal workflows
Use AI to route work, draft outputs, trigger actions, and keep approvals and logs in place.
Useful when repetitive work moves across multiple tools and teams.

Add AI to products and internal tools
Build assistants, guided actions, or decision support into the software your team or customers already use.
Useful when AI needs to be part of the product, not a separate tool.
Related Terms
Mastering contextual bandits requires understanding the foundational concepts of sequential decision-making, evaluation, and deployment. These cards break down the critical adjacent topics.
Exploration-Exploitation Trade-off
The fundamental dilemma at the heart of every contextual bandit. The algorithm must balance exploiting known high-reward actions against exploring uncertain actions to gather new data.
- Epsilon-greedy: Explores randomly with probability ε, exploits otherwise.
- Upper Confidence Bound (UCB): Selects actions based on an optimistic estimate of potential reward plus an uncertainty bonus.
- Thompson Sampling: A Bayesian approach that samples from the posterior distribution of each action's reward probability.
Poor management of this trade-off leads to regret—the cumulative loss compared to always choosing the optimal action.
Contextual vs. Non-Contextual Bandits
A standard Multi-Armed Bandit (MAB) selects actions without observing any side information, treating all users identically. A Contextual Bandit conditions each decision on a feature vector x representing the current user, session, or environment.
- MAB Example: A/B testing a single headline for all visitors.
- Contextual Example: Personalizing the headline based on visitor location, device, and referral source.
The addition of context transforms the problem from finding a single global optimum to learning a policy π(a|x) that maps diverse situations to personalized actions.
Counterfactual Evaluation
Evaluating a new bandit policy offline using historical data collected by a different logging policy. This is critical because deploying an untested policy in production is risky.
- Inverse Propensity Scoring (IPS): Reweights observed rewards by the inverse probability of the action being taken by the logging policy, correcting for selection bias.
- Doubly Robust (DR): Combines IPS with a direct reward model to reduce variance while maintaining unbiasedness if either the propensity or reward model is correct.
- Clipping: Caps extreme importance weights to control variance at the cost of introducing some bias.
Online Learning and Regret Minimization
Contextual bandits operate in an online learning paradigm where the model updates incrementally as each new data point arrives, without requiring batch retraining.
- Regret is the formal metric: the difference between the cumulative reward of the algorithm and that of the optimal policy in hindsight.
- No-Regret Algorithms: Guarantee that average regret per round approaches zero as T → ∞.
- Concept Drift: Online updates allow the policy to adapt to shifting user preferences, seasonal trends, or catalog changes without manual intervention.
Policy Gradient Methods
A class of algorithms that directly parameterize and optimize the policy π(a|x) using gradient ascent on expected reward, rather than estimating action values.
- REINFORCE: The simplest policy gradient, using Monte Carlo returns to update the policy in the direction of higher rewards.
- Actor-Critic: Combines a policy network (actor) with a value function estimator (critic) to reduce gradient variance.
- Advantage: Policy gradients naturally handle continuous action spaces and stochastic policies, making them suitable for complex personalization tasks where discrete bandit assumptions break down.
Reward Signal Design
The reward function defines what the bandit optimizes for, and poorly designed rewards lead to unintended behaviors.
- Immediate vs. Delayed: A click is immediate; a purchase or subscription renewal is delayed and must be attributed correctly.
- Primary vs. Proxy: Revenue is a primary metric; time-on-site is a proxy that may not correlate with business value.
- Multi-Objective: Combining conflicting goals (e.g., engagement vs. revenue) requires scalarization or constrained optimization.
- Inverse Reinforcement Learning: Inferring the reward function from observed expert behavior when explicit rewards are unavailable.

About the author
Prasad Kumkar
CEO & MD, Inference Systems
Prasad Kumkar is the CEO & MD of Inference Systems and writes about AI systems architecture, LLM infrastructure, model serving, evaluation, and production deployment. Over 5+ years, he has worked across computer vision models, L5 autonomous vehicle systems, and LLM research, with a focus on taking complex AI ideas into real-world engineering systems.
His work and writing cover AI systems, large language models, AI agents, multimodal systems, autonomous systems, inference optimization, RAG, evaluation, and production AI engineering.
Partnered with leading AI, data, and software stack.
How We Work
Custom AI workflows for your Business
One-fit-all AI don't work for modern businesses. At Inferensys, we aim to understand your business & custom requirements; which we use to define most efficient agentic workflows, the data, and the tools for your business.
01
Review the use case
We understand the task, the users, and where AI can actually help.
Read more02
Pick the right approach
We define what needs search, automation, or product integration.
Read more03
Build the first useful version
We implement the part that proves the value first.
Read more04
Improve from there
We add the checks and visibility needed to keep it useful.
Read moreThe first call is a practical review of your use case and the right next step.
Talk to Us