Harm Prevention Algorithm: A Real-Time Safety Net for Compassionate AI

Abstract

The Harm Prevention Algorithm delivers safety in real time, keeping an AI system within a formally defined safe set while it operates. The Ahimsa Decision Algorithm screens actions before they run, and the Risk Anticipation Algorithm weighs risk before deployment; Harm Prevention is the ever-present safety net during operation itself.

Harm Prevention Algorithm for Compassionate AI - Sri Amit Ray Compassionate AI Lab

It relies on Control Barrier Functions. The safe set is described by a barrier function that must never become negative, and only control inputs that keep it non-negative are permitted. As a result, failures lean toward safety rather than toward disaster. The algorithm is part of the Responsibility and Oversight spine of Sri Amit Ray’s Compassionate AI Architecture™ (CAIA™).

Key Takeaways

  • Provides continuous, real-time protection while the AI is acting.
  • Defines safety mathematically with a barrier function h(x) ≥ 0.
  • Allows only control inputs that keep the system inside the safe set.
  • Completes the safety trio with the Ahimsa and Risk Anticipation algorithms.

Algorithm 18 of 25 · CAIA™ · Responsibility & Oversight Spine

Harm Prevention Algorithm
for Compassionate AI

Algorithm 18 of 25 in Sri Amit Ray’s Compassionate AI Architecture™ (CAIA™), a seven-layer, human-centred framework that tests every AI decision for compassion, safety and fairness before it reaches people. Within it, the Harm Prevention Algorithm operates as a cross-cutting mechanism of the Responsibility pillar, auditing every layer and reinforcing the Human Oversight stage of the architecture.

Author
Dr. Sri Amit Ray
Organization
Sri Amit Ray Compassionate AI Lab
Series
Human-Centered Compassionate AI — Algorithm 18 of 25
Architecture
Component of the Compassionate AI Architecture™ (CAIA™)
Article Type
Research Article

In Simple Words

Plainly: the algorithm keeps the system inside a mathematically defined safe zone and, if trouble comes, makes it slow down gracefully instead of crashing into harm. Think of it as the seatbelt and crumple zone of an AI system.

Readiness level: Field-ready in control settings (control-barrier functions are established); applying them to learned policies is active work.

Why This Algorithm Matters

Developed at the Sri Amit Ray Compassionate AI Lab, the Harm Prevention Algorithm applies Control Barrier Functions to give mathematically grounded, real-time safety, restricting an AI’s choice of actions to a formally verified safe operating region throughout deployment. The Ahimsa Decision Algorithm filters harm before execution and the Risk Anticipation Algorithm assesses risk before deployment; the Harm Prevention Algorithm is the active safety net that works without pause while the system is running. [1]

“A safe system is one that fails gently, and never toward harm.”

— A guiding principle of the Sri Amit Ray Compassionate AI Lab

The Mathematics

The safe state set C = {x | h(x) ≥ 0} is defined by the Control Barrier Function h(x). Safety is maintained when:

\[ \mathcal{C}=\{x\mid h(x)\ge 0\},\qquad \sup_{u\in U}\big[\nabla h(x)^{\top}f(x,u)+\alpha\!\left(h(x)\right)\big]\ge 0 \]

The safe action set K(x) enforces this condition:

\[ K(x)=\{\,u\in U\mid \nabla h(x)^{\top}f(x,u)+\alpha\!\left(h(x)\right)\ge 0\,\} \]

Symbols Used

Symbol Meaning
\(h(x)\) control-barrier function; the safe set is \(h(x)\ge 0\)
\(f(x,u)\) system dynamics under control u
\(\alpha(\cdot)\) class-K function shaping the safety margin
\(K(x)\) set of controls that keep the system safe

A Step-by-Step Example

For an autonomous wheelchair, \(h(x)\) encodes distance-to-obstacle ≥ 0. As the chair nears a wall, the barrier condition removes any control command that would breach the margin, so only velocity and steering options that preserve safe distance remain — the system slows and steers away automatically.

How It Is Implemented

The reference implementation expresses the model as compassionate, auditable logic, written for clarity rather than raw performance so that ethical reviewers as well as engineers can follow every decision.

def enforce_safety_barrier(state, nominal_action, barrier_function):
    grad_h = compute_gradient(barrier_function, state)
    if check_barrier_condition(grad_h, nominal_action):
        return nominal_action
    else:
        safe_action = project_to_safe_manifold(nominal_action, grad_h)
        return safe_action

Fail-Safe and Graceful Degradation

For AI systems where complete failure could itself cause harm — healthcare monitoring systems, autonomous safety systems, critical infrastructure AI — the Harm Prevention Algorithm includes fail-safe and graceful degradation protocols. Rather than failing completely, these systems fall back to safer, more conservative modes of operation when confidence levels drop or harm signals are detected. Graceful degradation means that as system performance degrades, control progressively transfers to human operators rather than continuing autonomous operation at reduced performance. This transfer must be explicit and transparent — human operators must know they are taking over from a degraded system and understand the nature of the degradation.

Measuring It Reliably

Verify the safe set formally where possible, test graceful-degradation behaviour under sensor faults and edge cases, and measure time-to-safe-state. Validate against the fail-safe research baseline (Amodei et al.).

Where It Can Help

  • Medical Robotics: Continuously constraining surgical robot movements within pre-verified safe operating regions, preventing actions that exceed safe force or proximity parameters.
  • Autonomous Vehicles: Real-time trajectory constraint to prevent maneuvers that exceed safe speed, acceleration, or proximity to vulnerable road users.
  • Industrial Automation: Hard constraints on robot trajectories in human-occupied workspaces, ensuring physical safety regardless of task optimization pressures.

Use Cases in Practice

Beyond the general applications above, the algorithm supports concrete, human-centred deployments across compassionate-AI domains:

🤖 Compassionate Baby Care Robotics AI

A baby-care robot stays inside a provable safe set around the infant and degrades gently on sensor faults.

🤖 Compassionate Eldercare Robotics AI

An assistive robot maintains safe distance and slows automatically as it nears a person or obstacle.

🕊️ Peacekeeping Force Compassionate Robotics AI

On any fault or uncertainty, the robot defaults to a safe, non-harmful state.

🏙️ Smart Cities — Public Welfare Optimization

Autonomous infrastructure degrades gracefully toward safety rather than failing abruptly.

Ethical Foundations

Ethics and empathy are the starting point of the Sri Amit Ray tradition, not an extra layer added at the end. This algorithm rests on three commitments: non-harm (ahimsa), openness about how its decisions are reached, and respect for privacy and human dignity. Like every one of the twenty-five Human-Centered Compassionate AI algorithms, it is meant to work under human oversight — supporting human moral judgement, never standing in for it. [1]

Known Limits and Cautions

  • It needs a known model of what “safe” means, which is hard to specify for open-ended cognitive tasks.
  • Cautious barriers can restrict the system more than necessary.
  • Faulty sensors can corrupt the barrier value \(h(x)\) itself, so redundancy is essential.

Guarding Against Misuse

Any tool strong enough to protect people can also be misused to harm them, so these safeguards must accompany every deployment. The main risks of misuse for this algorithm, and how to guard against them, are:

  • A nominal safety wrapper can create false assurance if it is untested — require evidence of fail-safe behaviour, not just its presence.
  • Do not disable or weaken barriers to improve throughput.

Alignment with AI Standards

To support real-world adoption, the algorithm maps onto recognised AI governance frameworks and standards:

Framework How this algorithm contributes
NIST AI RMF MANAGE — safety and resilience.
EU AI Act Robustness and safety duties for high-risk AI.
ISO/IEC 42001 Operational safety and incident controls.
ISO 12100 / IEC 61508 Functional-safety alignment for physical systems.

Supporting the 17 UN Sustainable Development Goals

The Harm Prevention Algorithm uses control-barrier functions to keep a system inside a provably safe set and to fail gently, never toward harm. Fail-safe design protects health, infrastructure, and institutions, building on the 10 Ethical AI Indexes, the work on building value-aligned AI agents, and the study of constructive versus destructive AI dynamics.

The most directly relevant Sustainable Development Goals, and how this algorithm helps fulfil them, are:

  • SDG 3 Good Health and Well-being. Graceful degradation in medical and safety-critical systems prevents harm to human life.
  • SDG 9 Industry, Innovation, and Infrastructure. Provable safety margins make autonomous infrastructure dependable and resilient.
  • SDG 16 Peace, Justice, and Strong Institutions. Fail-safe behaviour upholds the duty of care institutions owe to the public.

In this way the algorithm is not a stand-alone technical artefact but a concrete instrument for the global development agenda, translating compassion into measurable progress toward the 2030 Goals and beyond.

Where It Fits in CAIA™

AI System · Raw DataInput
Layer 1 · Compassion FoundationCQ Score
Layer 2 · Suffering ReductionSuffering Score
Layer 3 · Empathy IntelligenceEmpathy Score
Layer 4 · Human FlourishingFlourishing Score
Layer 5 · Equity & ProtectionProtection Score
Layer 6 · Planetary CompassionPlanetary Score
Layer 7 · Future GenerationFuture-Gen Score
Human Oversight Gate · DeploymentFinal Gate

Figure: This algorithm works on the cross-cutting Responsibility & Oversight spine that audits every layer of the Compassionate AI Architecture™ (CAIA™).

CAIA-SVG-START
CAIA-SVG-END

This algorithm is one of twenty-five building blocks of Sri Amit Ray’s Compassionate AI Architecture™ (CAIA™). CAIA works like a seven-layer, human-centred operating system: before any AI decision can be deployed, it has to pass through each layer of compassion in turn.

It does not occupy a single compassion layer. Instead it functions as a cross-cutting mechanism of the architecture’s Responsibility pillar, operating across all seven layers and at the Human Oversight gate of the pipeline. It keeps the whole system inside a provably safe set and ensures graceful degradation — the safety spine beneath every layer.

Because CAIA requires that every layer’s score remain auditable, contestable, and safe, governance algorithms like this one are what allow the architecture to be trusted rather than merely asserted. A decision that fails here can be halted no matter how well it scores on any individual compassion layer. The architecture rests on four pillars — Compassion, Responsibility, Sustainability, and Future Generations — and this algorithm is central to the second.

Read the full architecture: The Sri Amit Ray Compassionate AI Architecture™ (CAIA™).

This algorithm is one of the 25 Human-Centered Compassionate AI algorithms — see the complete canonical reference list.

How to Cite

Ray, Sri Amit. “Harm Prevention Algorithm for Compassionate AI.” Compassionate AI, Sri Amit Ray Compassionate AI Lab, 2026, https://sriamitray.com/research/harm-prevention-algorithm-compassionate-ai/.

References

  1. Ray, Amit. “The Compassionate AI Lab Declaration on AI Safety, Suffering Reduction, and the Protection of Humanity.” Compassionate AI, vol. 4, no. 12, 2025, pp. 72–74, amitray.com.
  2. Ray, Amit. “AI Agents and Robots in Peacekeeping Force and Social Care: Compassionate AI Technologies.” Compassionate AI, vol. 3, no. 9, 2025, pp. 75–77, amitray.com.
  3. Amodei, Dario, et al. “Concrete Problems in AI Safety.” arXiv, 2016, arxiv.org/abs/1606.06565.
  4. National Institute of Standards and Technology. Artificial Intelligence Risk Management Framework (AI RMF 1.0). NIST AI 100-1, 2023, nist.gov.
  5. Russell, Stuart. Human Compatible: Artificial Intelligence and the Problem of Control. Viking, 2019.
  6. Ray, Amit. Human-Centered Compassionate AI: Sri Amit Ray’s 25 Algorithms for Protecting Humanity and Future Generations. Compassionate AI Lab, 2026. ISBN 978-8169641050.
  7. Ray, Amit. Compassionate Artificial Intelligence: Frameworks and Algorithms. Compassionate AI Lab, 2018. ISBN 978-9382123460.
  8. Ray, Amit. Compassionate Superintelligence AI 5.0: AI with Blockchain, BMI, Drone, IoT, and Biometric Technologies. Compassionate AI Lab, 2018. ISBN 978-9382123446.

Further Reading from the Compassionate AI Lab

  1. Ray, Amit. “Build Your Own Agentic AI Healthcare MCP Server: A Complete Guide to AI-Powered Healthcare Agents.” Compassionate AI, 3.8 (2026): 60-62. https://amitray.com/agentic-ai-healthcare-mcp-server/
  2. Ray, Amit. “Medical AI Drones for Disaster Management: Intelligent Unmanned Health-Care Systems.” Compassionate AI, 3.8 (2026): 57-59. https://amitray.com/medical-ai-drones-for-disaster-management/
  3. Ray, Amit. “Compassionate Medical AI Drones as Intelligent Unmanned Health-Care Systems: A Human-Centered AI Architecture for Suffering Reduction and Human Flourishing.” Compassionate AI, 3.8 (2026): 57-59. https://amitray.com/medical-ai-drones-human-centered-ai-healthcare/
  4. Ray, Amit. “The Sri Amit Ray Compassion Quotient (CQ) Algorithm for Compassionate AI.” Compassionate AI, 2.6 (2026): 81-83. https://amitray.com/compassion-quotient-cq-algorithm-compassionate-ai/
  5. Ray, Amit. “Collective Human Flourishing Optimization AI Algorithm (CHFOA): A Compassion-Centered Multi-Stakeholder AI Optimization Framework.” Compassionate AI, 2.6 (2026): 90-92. https://amitray.com/collective-human-flourishing-optimization-algorithm-chfoa/
  6. Ray, Amit. “Human Flourishing Optimization Algorithm (HFOA) for Compassionate AI Architecture.” Compassionate AI, 2.6 (2026): 81-83. https://amitray.com/human-flourishing-optimization-algorithm-hfoa-compassionate-ai/
  7. Ray, Amit. “The Sri Amit Ray Compassionate AI Architecture (CAIA): A Seven-Layer Framework for Human-Centered Artificial Intelligence.” Compassionate AI, 2.6 (2026): 81-83. https://amitray.com/compassionate-ai-architecture/
  8. Ray, Amit. “Human-Centered Compassionate AI: The 25 Algorithms for Protecting Humanity and Future Generations.” Compassionate AI, 2.6 (2026): 30-32. https://amitray.com/human-centered-compassionate-ai-the-25-algorithms/
  9. Ray, Amit. “AI Consciousness and Self-Aware Robots: Sri Amit Ray’s Framework for Compassionate Intelligence.” Compassionate AI, 2.5 (2026): 57-59. https://amitray.com/ai-consciousness-self-aware-robots/
  10. Ray, Amit. “The 12 Sri Amit Ray Compassionate AI Principles | Ethical AI Framework.” Compassionate AI, 2.4 (2026): 12-14. https://amitray.com/the-12-compassionate-ai-principles-for-humanity-and-future-generations/
  11. Ray, Amit. “The Compassionate AI Lab Declaration on AI Safety, Suffering Reduction, and the Protection of Humanity.” Compassionate AI, 4.12 (2025): 72-74. https://amitray.com/compassionate-ai-declaration-protection-of-humanity/
  12. Ray, Amit. “Compassionate AI for Forward Deployed Engineers (FDEs) in SaaS Companies.” Compassionate AI, 4.11 (2025): 15-17. https://amitray.com/compassionate-ai-for-forward-deployed-engineers-fdes-in-saas-companies/
  13. Ray, Amit. “Microbial AI, Bioleaching and Digital Twins for Manufacturing the 17 Rare Earth Elements.” Compassionate AI, 4.10 (2025): 42-44. https://amitray.com/microbial-ai-bioleaching-and-digital-twins-for-manufacturing-the-17-rare-earth-elements/
  14. Ray, Amit. “AI-Driven Rare Earth Element Magnet Design: Detailed Methodologies.” Compassionate AI, 4.10 (2025): 27-29. https://amitray.com/ai-driven-rare-earth-elements-magnet-design-methodologies/
  15. Ray, Amit. “AI Agents and Robots in Peacekeeping Force and Social Care: Compassionate AI Technologies.” Compassionate AI, 3.9 (2025): 75-77. https://amitray.com/ai-agents-robots-peacekeeping-force-social-care-compassionate-ai/
  16. Ray, Amit. “Ray Mother–Infant Inter-brain Synchrony Algorithm for Deep Compassionate AI.” Compassionate AI, 3.9 (2025): 60-62. https://amitray.com/ray-mother-infant-inter-brain-synchrony-algorithm-deep-compassionate-ai/
  17. Ray, Amit. “Pain Recognition and Prediction AI Algorithm (PRPA) for Compassionate AI.” Compassionate AI, 3.9 (2025): 60-62. https://amitray.com/pain-recognition-and-prediction-algorithm-prpa-for-compassionate-ai/
  18. Ray, Amit. “Modeling Consciousness in Compassionate AI: Transformer Models and EEG Data Verification.” Compassionate AI, 3.9 (2025): 27-29. https://amitray.com/modeling-consciousness-in-compassionate-ai-transformer-models/
  19. Ray, Amit. “The 7 Pillars of Compassionate AI Democracy.” Compassionate AI, 3.9 (2024): 84-86. https://amitray.com/the-7-pillars-of-compassionate-ai-democracy/
  20. Ray, Amit. “Compassionate AI-Driven Democracy: Power and Challenges.” Compassionate AI, 3.9 (2024): 48-50. https://amitray.com/compassionate-ai-driven-democracy-power-and-challenges/
  21. Ray, Amit. “The 10 Ethical AI Indexes for LLM Data Training and Responsible AI.” Compassionate AI, 3.8 (2023): 35-39. https://amitray.com/the-10-ethical-ai-indexes-for-responsible-ai/
  22. Ray, Amit. “Ethical Responsibilities in Large Language AI Models: GPT-3, GPT-4, PaLM 2, LLaMA, Chinchilla, Gopher, and BLOOM.” Compassionate AI, 3.7 (2023): 21-23. https://amitray.com/ethical-responsibility-in-large-language-ai-models/
  23. Ray, Amit. “Calling for a Compassionate AI Movement: Towards Compassionate Artificial Intelligence.” Compassionate AI, 2.6 (2023): 75-77. https://amitray.com/calling-for-a-compassionate-ai-movement/
  24. Ray, Amit. “From Data-Driven AI to Compassionate AI: Safeguarding Humanity and Empowering Future Generations.” Compassionate AI, 2.6 (2023): 51-53. https://amitray.com/from-data-driven-ai-to-compassionate-ai-safeguarding-humanity-and-empowering-future-generations/
  25. Ray, Amit. “Artificial intelligence for Climate Change, Biodiversity and Earth System Models.” Compassionate AI, 1.1 (2022): 54-56. https://amitray.com/artificial-intelligence-for-climate-change-and-earth-system-models/
  26. Ray, Amit. “Artificial Intelligence for Balance Control and Fall Detection of Elderly People.” Compassionate AI, 4.10 (2018): 39-41. https://amitray.com/artificial-intelligence-for-balance-control-and-fall-detection-system-of-elderly-people/
  27. Ray, Amit. “Artificial Intelligence to Combat Antibiotic Resistant Bacteria.” Compassionate AI, 2.6 (2018): 3-5. https://amitray.com/artificial-intelligence-for-antibiotic-resistant-bacteria/
  28. Ray, Amit. “Navigation System for Blind People Using Artificial Intelligence.” Compassionate AI, 2.5 (2018): 42-44. https://amitray.com/artificial-intelligence-for-assisting-blind-people/
  29. Ray, Amit. “Brain-Computer Interface and Compassionate Artificial Intelligence.” Compassionate AI, 2.5 (2018): 3-5. https://amitray.com/brain-computer-interface-compassionate-ai/
Scroll to Top