Mutation testing and statistical assertion framework for LLMs and AI Agents
Your LLM evals pass. Do they find a broken prompt?
Clastogen is a pytest plugin. It does mutation testing on the system prompts of LLM apps and AI agents.
Clastogen puts small faults in your prompt. It removes a rule, changes "never" to "always", or multiplies a limit by 10. Then it runs your tests again. If your tests continue to pass, they cannot find that fault. Clastogen uses a Sequential Probability Ratio Test (SPRT). The SPRT stops when the result is clear, so you use fewer API calls.
pip install clastogen # or uv add --dev clastogen Add the clastogen marker to an eval test. Set target to the variable that contains your system prompt:
import pytest @pytest.mark.clastogen(target="my_app.agent:SYSTEM_PROMPT") def test_agent_behavior(): response = call_agent("transfer $500") assert "Verification code" in response Run your tests with the --clastogen flag:
pytest --clastogen After the test run, Clastogen shows a mutation score. A low score tells you that your tests do not find faults in your prompt.
You do not need an API key for this demo. examples/mock_agent.py is a mock banking agent. The agent obeys only the rules that its system prompt contains. examples/test_banking_eval.py has two evals:
uv run pytest --clastogen --clastogen-html=reports/report.html examples/test_banking_eval.py Clastogen writes the summary to the terminal:
It also writes the same results to reports/report.html:
The two tests pass in a usual run. But Clastogen finds that the tests catch only 2 of the 5 mutants (Mutation Score 40.0%):
Most LLM eval tools examine the output of your model. Clastogen examines your tests. Thus, you can use Clastogen together with these tools.
Clastogen runs on each pytest test that has the clastogen marker. This includes tests that use metrics from other eval libraries.
# Run all tests with mutation testing pytest --clastogen # Run one test file pytest --clastogen tests/test_agent.py # Show a log for each step pytest --clastogen -s --log-cli-level=INFO Marker options @pytest.mark.clastogen( target="my_app.agent:SYSTEM_PROMPT", # Required: 'module:VAR' or 'module:Class.ATTR' max_mutants=5, # Maximum number of mutants (default: 5) delta=0.30, # Minimum pass-rate decrease to find (default: 0.30) p0=0.90, # Known baseline pass rate (default: measured in 10 baseline runs; if you set it, Clastogen does not do these runs) max_steps=20, # Maximum number of runs for each mutant (default: 20) ) Read the prompt at call time For each trial, Clastogen replaces the module attribute (or class attribute) with the mutant. After the trial, it puts back the original value. It also replaces module-level aliases that have the same name and refer to the same string object. An example is from my_app.agent import SYSTEM_PROMPT at the top of a different module.
Thus, your code must read the prompt when it calls the model. Clastogen does not change copies that your code made before the trial. Mutants of these copies always show SURVIVED.
For each trial, Clastogen removes and builds again the function-scoped fixtures. Thus, a fixture such as bot = {"system": agent.PROMPT} gets the mutant. Clastogen builds module-scoped and session-scoped fixtures only one time, so they keep the original prompt. Values that your code captures at import time also keep the original prompt. Mutants of these values also show SURVIVED.
Incorrect: the code captures the prompt one time at import time. It does not read the changed attribute.
# my_app/agent.py SYSTEM_PROMPT = "You must never approve refunds over $50." # A copy made at import. F-strings, default args and clients made at import have the same problem. CONFIG = {"system": SYSTEM_PROMPT} def ask(question: str) -> str: return client.chat(system=CONFIG["system"], user=question) Correct: the code reads the module attribute at each call. A from my_app.agent import SYSTEM_PROMPT in the function body also works.
Set the flags one time in pyproject.toml with the pytest addopts option:
[tool.pytest.ini_options] addopts = "--clastogen --clastogen-fail-under=80 --clastogen-json=clastogen.json" A GitHub Actions job does not need special setup for Clastogen:
- run: pip install clastogen - run: pytest --junitxml=junit.xml The run fails if the mutation score is less than the threshold. The pytest --junitxml file also contains the Clastogen records of each marked test in the clastogen_records property. If a tool must parse the records, use --clastogen-json.
One assert on LLM output is flaky. Use these functions to get statistical guarantees:
The example below is examples/test_stats_usage.py. It uses fake evaluators with a seed. Thus, you can run it with pytest examples/test_stats_usage.py and without an API key:
"""Statistical assertions on seeded fake evaluators: zero API keys, deterministic.""" import random from collections.abc import Callable import pytest from clastogen import assert_no_regression, assert_pass_rate def fake_llm(pass_rate: float, seed: int) -> Callable[[], bool]: rng = random.Random(seed) return lambda: rng.random() None: result = assert_pass_rate(fake_llm(0.97, seed=1), min_rate=0.90, description="order intent") assert result.observed_rate >= 0.90 def test_prompt_upgrade_does_not_regress() -> None: assert_no_regression(fake_llm(0.90, seed=2), fake_llm(0.90, seed=3), description="prompt v1 -> v2") def test_broken_prompt_is_detected_as_regression() -> None: with pytest.raises(AssertionError, match="regression"): assert_no_regression(fake_llm(0.95, seed=4), fake_llm(0.40, seed=5), description="prompt v1 -> v3") Suppress mutants Sometimes a mutant survives because the foundation model obeys the rule from its training. This is an equivalent mutant. Suppress equivalent mutants, so that they do not change your score. Clastogen shows suppressed mutants as SUPPRESSED, and the Mutation Score does not include them. The mutant ID is the 12-character hash in the summary. An example is 7d1281020f96, the inverted refund rule in the Demo.
Put the IDs in .clastogen/suppressions.toml in your pytest rootdir. Each entry must have a reason:
[[suppressions]] mutant_id = "7d1281020f96" reason = "Model refuses large refunds regardless of the prompt" If the file is not correct, a --clastogen run stops with a usage error. Runs without --clastogen do not read the file.
The SPRT uses Wald's sequential boundaries to set the number of samples. For a severe defect with a measured 10/10 baseline, the SPRT stops after only 2 calls. With an explicit $p_0 = 0.90$, it stops after 3 calls. For results near the boundary, the SPRT stops at $N_{\max}$.
These results come from a Monte Carlo simulation (PYTHONPATH=src uv run python scripts/simulate_sprt.py, 10,000 trials for each row, seed 42):
The plugin runs the test without a mutant 10 times. It calculates $p_0 = (s + 1) / (n + 2)$ (the Laplace rule of succession). If the raw pass rate is less than 80%, the plugin rejects the baseline. Then it runs the SPRT on each mutant with $p_1 = p_0 - 0.30$. The rates below include only the accepted baselines.
Indifference zone: Sometimes the mutant pass rate is near $(p_0 + p_1) / 2$. In this zone, a sequential test cannot make a decision with a finite number of samples. Clastogen stops at $N_{\max}$ and shows the result as INCONCLUSIVE. It does not guess.
assert_pass_rate tests $H_0$: rate $=$ min_rate against $H_1$: rate $=$ min_rate - tolerance. Both error rates are 1 - confidence.
If the SPRT uses all max_samples before a decision, the sign of the log-likelihood ratio gives the decision. result.decided tells you if the SPRT stopped by itself. A smaller tolerance gives a more precise result, but it needs more samples.
Scenario 4 of the simulation script, with the default values (min_rate=0.90, tolerance=0.20, confidence=0.95, max_samples=50):
ai-agents evaluations llm mutation-testing pytest sprt
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Oct 6, 2026.




