Your numbers decide. The AI only writes.
Turn your data into AI-written reports with checked figures, sentence-level decisions, and receipts you can replay.
Quick start · Your data · Examples · MCP setup · Service · Checks · Contribute · Guía en español
Real output of antihallucination verify examples/saas/written_by_an_ai.txt --data examples/saas/data.json. No model, no API key.
Reports built from your data
A sales summary should use the sales you measured. A support report should keep the customer counts in your tickets. Your code owns the figures and verdicts; Anti-Hallucination lets a writer put them into words, then checks each sentence before it goes out.
| What you need | What you get |
|---|---|
| Keep figures under your control | Python fills {placeholders} from your facts instead of asking a model to type the values |
| Check a draft you already have | verify() flags unsupported figures, conflicting directions, and other rule violations |
| Decide what happens when a check fails | Drop the sentence or replace it with a fallback you supplied |
| Explain the output | A decision for each sentence, plus a receipt you can verify and replay |
| Fit your workflow | Python library, command line, and an MCP server; English and Spanish checks |
Use it for sales reports, SaaS metrics, finance summaries, and support digests.
The checks run locally and use the Python standard library. The package also
installs httpx for optional calls to model providers.
Quick start
Three ways in, from nothing to install to Python. None needs an API key.
In your browser, with nothing to install
- Open the notebook in Colab. A Google account is enough.
- Choose Runtime → Run all, or the ▷ Run all button. If Colab warns that the notebook was not written by Google, choose Run anyway.
- Read the output under 2. Check it: what happened to each sentence and
why. To try your own sentence, edit
my_textunder 4 and run that cell with its ▷ button.
From Claude, with no code
Claude Desktop (the app, not the website) or Claude Code calls the checks for you. Set it up once:
-
Install uv, which downloads and runs the server. On macOS or Linux, in a terminal:
curl -LsSf https://astral.sh/uv/install.sh | sh
On Windows, in PowerShell:
powershell -ExecutionPolicy ByPass -c "irm https://astral.sh/uv/install.ps1 | iex"
-
Connect the server.
-
Claude Desktop: open Settings from the app's menu, not from a chat: on macOS, Claude → Settings… in the top bar; on Windows, the ☰ menu at the top left → File → Settings. Go to Developer → Edit Config, paste this into the file and save it. If the file already has
"mcpServers", add only the"anti-hallucination"entry inside it.{ "mcpServers": { "anti-hallucination": { "command": "uvx", "args": ["--with", "antihallucination[mcp]", "antihallucination", "mcp"] } } }
-
Claude Code: run
claude mcp add --transport stdio --scope user anti-hallucination -- uvx --with "antihallucination[mcp]" antihallucination mcp
-
-
Quit Claude Desktop completely, from its menu or the system tray and not just the window, and open it again. The server shows up under the chat box's + button (Add files, connectors, and more) → Connectors. In Claude Code,
claude mcp listshows it as connected. -
Ask in plain words, with your figures. To have a text written:
Here are my numbers: September sales 1,240 units, October sales 1,440 units, and 37 new customers in October. Use anti-hallucination to write a two-sentence summary for my team.
To check a text you already have:
Check this text with anti-hallucination against my numbers. Text: "In October sales rose 20% to 1,500 units, thanks to the new website." My numbers: September sales 1,240 units, October sales 1,440 units.
You get the text, what happened to each sentence and why, and a receipt. In a test with Claude Code, Claude had the server compute the 16.1% rise both times instead of working it out itself, and the check caught “20%”, “1,500” and “thanks to”.
If the server does not show up, run the command from the configuration in a
terminal: uvx --with "antihallucination[mcp]" antihallucination mcp. If it
starts and waits in silence, it works: stop it with Ctrl+C and restart Claude.
If the terminal cannot find uvx, open a new terminal after installing uv. If
Claude Desktop still cannot find it, put the full path that which uvx
(macOS, Linux) or where uvx (Windows) prints in "command". Claude
Desktop's logs are in ~/Library/Logs/Claude on macOS and
%APPDATA%\Claude\logs on Windows.
From Python
Python 3.11+ · The example below runs locally.
pip install antihallucination
from antihallucination import Fact, compose, verify
facts = [
Fact("sales", 1250, unit="units", subject="sales"),
Fact("sales_change", -3.2, unit="%", change=True, subject="sales"),
]
# The writer uses placeholders. Python fills the figures.
draft = "Sales fell {sales_change:abs} to {sales}. Next quarter will be better."
result = compose(draft, facts, locale="en")
print(result.text)
# Sales fell 3.2% to 1,250 units.
# Check an existing draft against the same facts.
report = verify("Sales grew 5% to 1,300 units.", facts, locale="en")
print([(finding.check, finding.span) for finding in report.findings])
# [('unbacked_number', '5%'), ('unbacked_number', '1,300'), ('direction_contradiction', 'grew')]
The forecast is dropped. The measured decline stays. In the second draft,
both invented figures and the conflicting direction are flagged.
result.decisions explains what happened to each sentence;
result.receipt records the inputs and decisions with a SHA-256 digest.
Let a model write the draft
Install the Claude extra and set ANTHROPIC_API_KEY:
pip install "antihallucination[claude]"
Using the facts from above:
from antihallucination import write
from antihallucination.writers import ClaudeWriter
written = write(
"A two-sentence sales summary for the board.",
facts,
writer=ClaudeWriter(),
locale="en",
)
print(written.text)
write() gives the writer placeholder names, formatting rules, and change
directions while withholding the numeric values in Fact.value. It checks
the draft and can ask for another attempt with the reasons a sentence failed.
Source text, labels, and your brief can still contain numbers.
Prefer another provider? OpenAICompatibleWriter.openai(model),
.groq(model), and .ollama(model) use the same writing pipeline. You can
also pass any function with the signature (system, prompt) -> str.
Your data
The checks know only what you give them: your figures (facts), texts of yours the report may use as they are (labels), and the sentence to write when a sentence about a subject fails (fallbacks). With Claude, you write your numbers in the chat and Claude fills this in. In Python and on the command line, you write it:
{
"locale": "en",
"facts": [
{"name": "sales_sep", "value": 1240, "unit": "units", "subject": "sales"},
{"name": "sales_oct", "value": 1440, "unit": "units", "subject": "sales"},
{"name": "sales_change", "compute": "percent_change", "from": "sales_sep", "to": "sales_oct", "subject": "sales"},
{"name": "new_customers", "value": 37, "subject": "customers"}
],
"labels": [{"name": "month", "text": "October"}],
"fallbacks": {"sales": "Sales were {sales_oct} ({sales_change:signed})."}
}
| Key of a fact | What it is |
|---|---|
name |
Your name for it, with no spaces. Placeholders use it: {sales_oct} |
value |
The bare number: 1440, not "1,440 units". Python writes it in the text's format |
unit |
units, USD, %, pp… |
subject |
What it measures, in the words the text uses ("sales"), so that “sales rose” is checked against it. One word or a list |
change |
true when the value is itself a change, a rise or a fall |
decimals |
How many decimals Python writes |
compute, from, to |
A change computed here from two facts: "percent_change" or "difference". Nobody types it |
Saved as data.json, it checks a text with
antihallucination verify text.txt --data data.json. For “In October sales
rose 20% to 1,500 units. We added 37 new customers.”, the first sentence is
replaced by “Sales were 1,440 units (+16.1%).” and the second is kept. The
service takes the same object as data.
A label can also be chosen from a fact by thresholds (“a slight decline”), and
sources, known_names and checks hold cited documents, the names a text
may use, and checks to switch off.
antihallucination/data.py
documents every key. The checks trust these values: they make the text say
what your data says, not make your data right.
From a database or a spreadsheet
The checks connect to nothing. Your code reads the figures and hands them over, so the model never touches them:
import sqlite3
from antihallucination import Fact, percent_change, verify
db = sqlite3.connect("sales.db") # or your Postgres, MySQL... connection
units = dict(db.execute("SELECT month, units FROM sales WHERE month IN ('2026-09', '2026-10')"))
sep = Fact("sales_sep", units["2026-09"], unit="units", subject="sales")
oct_ = Fact("sales_oct", units["2026-10"], unit="units", subject="sales")
change = percent_change("sales_change", sep, oct_, subject="sales")
report = verify("In October sales rose 20% to 1,500 units.", [sep, oct_, change], locale="en")
print([(finding.check, finding.span) for finding in report.findings])
# [('unbacked_number', '20%'), ('unbacked_number', '1,500')]
With write() instead of verify(), a model drafts the text with
placeholders, and the figures go from your database into the text without
passing through the model. Without code, export the query or the spreadsheet
to CSV and paste it into the chat with Claude.
How it works
The LLM only chooses the words: it writes placeholders instead of your figures,
and Python checks each sentence of the draft and writes the figures into the
sentences that pass. verify() runs the same checks on a text that already has
its figures.
- Facts hold values your code computed. Labels hold verdicts your rules
chose, such as “a slight decline”;
classify()can choose them by threshold. - Computed changes come from two facts:
percent_change()anddifference()compute them exactly, so neither you nor the writer does the arithmetic. The receipt records them as computed, and from which facts. - Sources hold evidence a draft can cite with
[id]. A citation provides context for the checks; it does not prove that the sentence follows from the source. - Fallbacks are sentences you wrote. A failed sentence can be replaced by a fallback for the same subject; otherwise it is dropped.
- Receipts keep the inputs, decisions, and a digest so you can check their consistency and replay the run.
What it checks
| Check | compose() — a draft with placeholders |
verify() — existing text |
|---|---|---|
| Figures | Python fills placeholders; figures copied from a cited source are also allowed | Each figure must match a fact at the written precision and in its unit (1.9 pp is never a fact in %), or occur in a label or permitted source |
| Signs and directions | A word such as “fell” or “cayeron” must agree with the change it talks about: the subject it names (“the rise in churn”), the figure beside it, or for “this rise” the sentence before; unsupported movement is flagged | Same |
| Names | Checks proper names against facts, labels, sources, and known_names |
Same |
| Forecasts, certainty, causes | Flags phrases from English and Spanish word lists by default, readings of the figures included (“indicating”, “sugiere”); a cause or a forecast that a source states passes when the sentence repeats it in the source’s words | Same |
| Data in every sentence | Requires a placeholder, a checked direction, or a source citation | Requires a figure, a verdict from your data, a checked direction, or a citation |
Number parsing follows the locale: 1.250 means a thousand two hundred and
fifty in Spanish, while 1,250 means that in English. It also handles scales
such as 1,2 millones and 4bn, and a vocabulary of numbers written in words.
Large inputs
Checking time grows in line with the size of the text and the data together,
for every kind of input found to be slow but one, at the end of this section:
twice the input takes about twice the time. On the test machine, a 256 KB
report of about 5,000 sentences takes 1 to 2 seconds, depending on its load,
and the slowest kind of text found, digits next to numbers in words, about 4
seconds. tools/timing.py measures each kind on your machine.
When the text matches the data over and over, such as one figure that stands
for thousands of facts, the work and the receipt would keep growing. The
checks stop at 200,000 matches and raise TooManyMatches. The examples, made
256 KB long, use at most 6,720.
Two test files keep this from coming back: tests/test_long_texts.py and
tests/test_large_data.py time 23 inputs, the 17 that took from 6 to more
than 25 seconds before and 6 for the checks added in v0.4.0, and each must
finish within 3 seconds. The faster code was
also compared with the code before it on tens of thousands of random texts and
data sets, and gave the same receipt, digest included, for every one.
tools/compare_versions.py repeats that comparison against any git ref, and
tools/timing.py shows how each kind of input grows. The
test record
has the numbers.
One case is still slow in the library: data that gives thousands of facts the same subject, named in thousands of sentences (4,000 of each take 17 s). The service stops any request at its time limit.
Why not ask another model to check?
A model used as a judge reads the text and the data and says whether they agree. It can judge meaning, and this layer does not replace it. For figures, though, it brings back what you are trying to remove:
| Another model as judge | Anti-Hallucination | |
|---|---|---|
| Who writes the figures | The writer types them; the judge reads them afterwards | Python fills them from your facts (compose()) |
| Same input, same decision | Not guaranteed: sampling and model updates change answers | Yes: the rules are Python, and replay() reruns a receipt to show it |
| Cost and data | A model call per check; the text and the data go to a provider | Runs locally on the standard library: no call, no key |
| Why a sentence failed | A judgment in prose | A named check and the words that failed: «8%» is not in your facts |
| What it can judge | Meaning, paraphrase, tone | Figures, directions, names, forecasts, certainty, causes, data in each sentence |
If you need both, use both: a judge for meaning, these checks for the figures. Scope and limitations lists what the rules miss.
Examples
Each example includes a data file, a draft with placeholders, and a text written with figures. The test suite checks their expected outputs.
| Example | What it demonstrates |
|---|---|
| SaaS metrics · English | Invented revenue figures, a cause, and a forecast are caught; supplied fallbacks keep the report useful |
| Support digest · English | A ticket summary changes 23 customers to 32 and invents a cause; checks catch both |
| Sales report · Spanish | A draft claims growth while the supplied change is −3.2% |
| Python walkthrough | Run both modes, inspect the decisions, and replay the receipt |
Use with MCP
Use the same checks from Claude Code, Claude Desktop, or another MCP client.
The server exposes four tools and makes no model or network calls itself. It
is listed in the official MCP Registry
as io.github.Jegoba90/anti-hallucination. The setup with uv for Claude
Desktop and Claude Code is in Quick start, step by
step. With pip instead of uv:
pip install "antihallucination[mcp]"
claude mcp add --transport stdio --scope user anti-hallucination -- python -m antihallucination.mcp_server
In Claude Desktop's configuration, the same server is "command": "python"
with "args": ["-m", "antihallucination.mcp_server"], run by the Python where
you installed the extra: python3 or an absolute path, depending on the
system. The file is claude_desktop_config.json, in %APPDATA%\Claude\ on
Windows and in ~/Library/Application Support/Claude/ on macOS.
antihallucination mcp and antihallucination-mcp launch the same server.
| Tool | Purpose |
|---|---|
prepare_report |
Build the writing instructions with placeholders and rules |
compose_report |
Fill the draft, run checks, and return text, decisions, and a receipt |
verify_text |
Check an existing text against the supplied data |
check_receipt |
Verify receipt consistency and replay its decisions |
How an assistant uses them. The server sends these steps to the client as its instructions, so the model follows them without a prompt:
prepare_reportwith the user's figures asfacts: it returns a placeholder for each figure and the rules.- The model writes its draft with those placeholders and types no figure.
compose_reportwith the draft and the same data: Python writes every figure, and the result holds the finaltext, a decision for each sentence, and a receipt.
verify_text checks a text that already has its figures, and check_receipt
replays a receipt.
The assistant supplies the data. It may already have seen, calculated, or
changed the values. The server checks the text against what it receives; it
does not establish where those values came from. What it can take off the
assistant is the arithmetic: a change between two figures goes in as
{"compute": "percent_change", "from": "...", "to": "..."}, and the server
computes it. Each receipt shows which figures were computed and which were
supplied. Client and model privacy policies still apply.
Privacy and data flow
The four MCP tools run checks on the server's machine, make no outbound model or network calls, and return results to your MCP client. They do not save reports or receipts to disk. Your client may send tool inputs and outputs to its model provider or keep them in conversation history.
Optional writers used through write() or the CLI write command send prompts
to the provider you select. Prompts include source text, the brief, and labels;
placeholders for fact values do not anonymize that content. Receipts contain
fact values and report text, so treat them as sensitive as the original data.
Read the privacy policy for destinations, stored outputs, retention, and self-hosted service details.
Run it as a service
For teams that would rather call an endpoint than add Python to their agent. One container serves the same checks two ways: a REST API for agents in any language, and MCP over HTTP for Claude Code and other MCP clients. It calls no model, keeps nothing between requests, and runs inside your own network.
export ANTIHALLUCINATION_TOKEN=$(openssl rand -hex 32)
docker run -d -p 8000:8000 -e ANTIHALLUCINATION_TOKEN ghcr.io/jegoba90/anti-hallucination
The service ships from v0.3.0 on, and each release publishes the image. docker build -t anti-hallucination . builds it from this repository.
Without Docker, run pip install "antihallucination[server]" and then
antihallucination serve.
| Route | Body | Does |
|---|---|---|
POST /v1/compose |
draft, data, locale, writer |
Fill a draft with placeholders and keep what passes |
POST /v1/verify |
text, data, locale, writer |
Check a text that already has its figures |
POST /v1/prepare |
brief, data, locale |
The placeholders and rules to hand a writer |
POST /v1/receipt |
receipt, data |
Verify a receipt and replay its decision |
/mcp |
The same four tools over MCP | |
GET /health |
Status and version |
data is the JSON of the command line's data file, and its locale is the
default; writer names who wrote the text, for the receipt. Responses match
the MCP tools' output.
curl -s localhost:8000/v1/verify \
-H "Authorization: Bearer $ANTIHALLUCINATION_TOKEN" -H "Content-Type: application/json" \
-d '{"text": "Sales grew 5% to 1,300 units.", "locale": "en", "data": {"facts": [
{"name": "sales", "value": 1250, "unit": "units", "subject": "sales"},
{"name": "sales_change", "value": -3.2, "unit": "%", "change": true, "subject": "sales"}]}}'
To connect Claude Code:
claude mcp add --transport http anti-hallucination http://your-host:8000/mcp \
--header "Authorization: Bearer <token>"
Every route except /health needs the token. Without
ANTIHALLUCINATION_TOKEN the service only starts on 127.0.0.1, so the
container will not start without one. Make the token long and random, as
openssl rand -hex 32 does. The service refuses a token shorter than 16
characters.
The service speaks plain HTTP. Without TLS, the token and every report travel in clear text, so anyone on the network path can read and reuse them. Put it behind a proxy that terminates TLS (nginx, Caddy, a cloud load balancer), or reach it only inside a private network you trust. Do not expose its port to the internet directly.
A request must send its body with Content-Type: application/json, or it gets
415. A service on 127.0.0.1 also refuses a Host or Origin header that is
not local, with 421 or 403, on both the REST routes and /mcp. That keeps
a web page from reaching it through the visitor's browser. A proxy on the same
machine must pass a local Host, or the service must listen on another
address, with a token.
Each request is limited, and one past a limit gets 413 while the service
goes on:
| Limit | Default | Set it with |
|---|---|---|
| Request size | 256 KB | ANTIHALLUCINATION_MAX_BODY or --max-body (bytes) |
| Time to check it | 10 s | ANTIHALLUCINATION_TIMEOUT or --timeout (seconds) |
| Memory to check it (Linux and macOS) | 1024 MB | ANTIHALLUCINATION_MAX_MEMORY or --max-memory (MB; 0 for none) |
Each request's checks run in a worker process of their own, so one that runs
long can be stopped without stopping the others. A text that matches the data
more than 200,000 times (say, one figure that stands for thousands of facts) is
refused by the checks themselves, in the library too: TooManyMatches.
A fact's value takes at most 40 digits written out in full: 1e39 and 1e-39
fit, 1e40 does not. The receipt writes every digit, so without this limit a
64-byte request with 1e-10000000 would get a 10 MB answer. A longer value is
refused with 400, and with ValueError in the library.
Command line
antihallucination verify examples/saas/written_by_an_ai.txt --data examples/saas/data.json
antihallucination compose examples/saas/draft.txt --data examples/saas/data.json --receipt receipt.json
antihallucination receipt receipt.json --data examples/saas/data.json
antihallucination write --data examples/saas/data.json --brief "A short monthly update for the team."
python -m antihallucination works too. The data file holds facts, labels,
sources, fallbacks, and known names, as in Your data; every key
is documented in
antihallucination/data.py.
Exit codes: 0 all sentences passed, 1 checks failed, 2 invalid input,
3 writer failure.
Scope and limitations
Experimental · v0.4. This is a layer of explicit rules for text generated from supplied data. It is not a general factuality or semantic entailment checker.
- It does not establish that your facts are true or that your sources support every possible paraphrase. A source citation alone is not proof.
- Names, directions, forecasts, certainty, causes, and numbers written in words use heuristics and word lists. Phrasings outside those rules can pass; a name at the start of a sentence is not checked.
- A sentence can carry a valid figure and still add an unsupported opinion. Put verdicts in labels and keep human review where it matters.
- The checks do not know which way is better for each metric: “churn improved by 0.4 pp” is cut when churn fell, because “improved” does not say that it went down.
- A receipt's SHA-256 digest checks internal consistency. It is unkeyed and does not authenticate the author or the origin of the data.
The test record includes real Groq drafts and a Claude Code MCP session, along with what has and has not been tested live. CI runs the tests on Python 3.11, 3.13, and 3.14, plus minimal-install and installed-wheel checks. The security review lists what was found before v0.3.0, how each point was fixed, and what remains.
Contributing
If it caught a number for you, a ⭐ on the repository helps others find it.
Found a phrasing the checks miss? Have a report that needs a clearer decision? Open an issue with a small example, or read the contribution guide to run the checks and submit a change.
pip install -e ".[dev]"
pytest tests/ -v
mypy .
ruff check .
Origin and license
Extracted from the reporting engine behind CryptoCapi, where it checks the market reports a crypto agent writes.
Released under the MIT license.
Metadata
Release files for antihallucination 0.4.2
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| antihallucination-0.4.2.tar.gz | 142.3 kB | With provenance | Details for antihallucination-0.4.2.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| antihallucination-0.4.2-py3-none-any.whl | Python 3 | none | any | Details for antihallucination-0.4.2-py3-none-any.whl |
Total release size: 229.8 kB
Release files / antihallucination-0.4.2.tar.gz
| Download URL | antihallucination-0.4.2.tar.gz |
|---|---|
| Size | 142.3 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
47ce6c18d8f1c1c2c7d51f04b6f8d55ec60609d2853a3d3181459018ade2b77f
|
|
BLAKE2b-256 checksum How to use checksums |
4b6aaf7c30595b71390d1e34602567721486d6f0c2496ec72e63694d8cbeff35
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/7.0.0 CPython/3.13.16
|
Provenance
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 Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Nov 29, 2026.
Transparency logRelease files / antihallucination-0.4.2-py3-none-any.whl
| Download URL | antihallucination-0.4.2-py3-none-any.whl |
|---|---|
| Size | 87.5 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
e2f1c3c6898f0d1c42c71b7ead36af79eb25a0a74e8a45d64ab9f40f8ef3d891
|
|
BLAKE2b-256 checksum How to use checksums |
b605873542ed3333545a0cb5547d981bff5accf563d7947e05aff99fc31dcdcc
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/7.0.0 CPython/3.13.16
|
Provenance
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 Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Nov 29, 2026.
Transparency logSecurity for antihallucination 0.4.2
Provenance
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.
Full provenance
Both files in this release have been uploaded with provenance metadata.
Attestation for antihallucination-0.4.2.tar.gz
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Nov 29, 2026.
Transparency logA note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.