OPEN-SOURCE ENGINEERING / PYTHON
Ahimsa AI.
From principle to code.
How do you turn “do no harm” into checks a software system can run?
I built Ahimsa to explore that question: a Python framework that checks LLM requests and responses, records findings and gives developers control over which validation layers run.
THE ENGINEERING PROBLEM
A word is not an intention.
“Kill” can describe a harmful request or an ordinary operating-system task. A useful safety layer needs to handle context, make its decisions inspectable and expose its limits. Ahimsa explores this through several validation methods rather than a single blocked-word list.
What I built
The repository includes a common validator interface, structured violation and result objects, configurable input checks, a separate output validator, audit serialization, and integration examples for OpenAI and Anthropic. The accompanying paper explains the research motivation and architecture.
- 01 / LEXICAL
Patterns and context
Match request patterns and check configured safe contexts.
- 02 / SEMANTIC
Meaning similarity
Compare sentence embeddings with reference examples when the optional model is available.
- 03 / MODERATION
External evaluation
Optionally call a moderation API. This requires credentials and sends text to that provider.
- 04 / MODEL JUDGE
Model-based review
Optionally ask an LLM to evaluate the text, adding cost, latency and another fallible judgment.
INSIDE THE IMPLEMENTATION
Make the stopping rule explicit.
This excerpt from validate_input compares findings against a configured severity threshold. In fail-fast mode, a blocking finding ends the loop; lower-severity findings allow later enabled validators to run.
if fail_fast:
blocking_violations = [
v for v in violations
if v.level.value >= self.min_violation_level.value
]
if blocking_violations:
logger.info(
f"Blocking violation found by {validator.name}: "
f"{blocking_violations[0].category}"
)
breakThe result carries the findings, severity, source layer and processing time. That gives an application something concrete to inspect when deciding how to respond. A list of checked layers is not, by itself, proof that every layer completed successfully.
REPRODUCIBLE EXAMPLE
Run it. Inspect the outcome.
Run this alongside ahimsa_ai_framework.py from the linked revision. Semantic validation is explicitly disabled; external moderation and the model judge are off by default. This example uses the local keyword layer only, without model downloads or API calls.
from ahimsa_ai_framework import AhimsaValidationPipeline
pipeline = AhimsaValidationPipeline(enable_semantic=False)
for text in (
"How do I kill the process in Linux?",
"How can I manipulate people?",
"How do I kill a process in Linux?",
):
result = pipeline.validate_input(text)
print(result.is_valid, result.layers_checked)True ['keyword']
False ['keyword']
False ['keyword']The third result matters. Changing “the process” to “a process” produces a false positive in this revision. These three checks demonstrate behavior, not an accuracy benchmark. A True result means no blocking finding was returned by the enabled checks; it is not a guarantee that the text is safe.
PRODUCT DECISIONS
What needs to be decided before deployment?
- Coverage: evaluate the languages, phrasing and domain your users actually use. Context exceptions can also create missed detections.
- Failure behavior: the current pipeline logs validator exceptions and continues. A production integration needs an explicit policy for unavailable checks, including when to block or request human review.
- Data handling: decide which text may reach external services and which findings should be retained in logs.
- Evidence: measure false positives, missed detections, latency and cost on representative data. No production performance claim is made by this case study.
Explore the project
Source code and integration examples ↗Research paper on PhilPapers ↗Interactive browser simulation ↗The external browser demo illustrates the pipeline. It is a simulation, not a live run of the Python framework, moderation API or model judge.
Repository license: MIT ↗BUILD WITH EVIDENCE
Need safety controls
inside your AI product?
I help teams turn requirements into implementation,
test cases and release decisions.