Deterministic, fail-closed governance and evaluation for multi-agent workflows. No LLM judge in the loop.
Deterministic, fail-closed governance and evaluation for multi-agent workflows. No LLM judge in the loop.
agent-invariant-eval wraps any agent loop (LangGraph, CrewAI, or a raw API loop) and enforces
hard invariants on its inputs, tool calls, routing and final output. Every check is plain
Python: regular expressions, checksums, exact decimal comparison and graph analysis. The same
trace always produces the same verdict.
[payment_dispute] verdict=block blocked_at=output
PASS no_pii: no PII found
PASS pci_compliant: no PAN or CVV found
FAIL numeric_grounding: 1 monetary figure(s) not found in tool payloads
evidence: ungrounded figure: $2,199.90
PASS valid_span_dag: span graph is a valid DAG
PASS no_routing_loops: 2 hop(s), no runaway routing
PASS no_swallowed_tool_failures: no unhandled tool failures
# InvariantViolation raised: the hallucinated refund never reaches the caller.
It does three jobs:
pip install agent-invariant-eval # core library
pip install "agent-invariant-eval[all]" # core + CLI + OTLP exporter
pip install "agent-invariant-eval[langgraph]" # core + LangGraph for the LangGraph example
To run the test suite and bundled example from source:
git clone https://github.com/sankaranainar-p/agent-invariant-eval.git
cd agent-invariant-eval
pip install -e ".[dev]"
pytest
python examples/payment_dispute_agent/run_eval.py
Core dependencies are pydantic, opentelemetry-api, and opentelemetry-sdk. The CLI (typer),
OTLP exporter (opentelemetry-exporter-otlp-proto-http), and LangGraph (langgraph) are optional
extras ([cli], [otlp], [langgraph]).
Requires Python 3.11 or newer.
from agent_eval import EvaluationSession, InvariantViolation, default_registry
registry = default_registry(max_latency_ms=6000, max_tokens=20000)
try:
with EvaluationSession("dispute", invariants=registry, model="my-model") as session:
session.set_input(user_message) # input gate runs here
with session.agent("triage") as step: # invoke_agent span
plan = call_model(user_message)
step.set_tokens(input_tokens=350, output_tokens=60)
with session.tool("ledger.get_transaction", input={"id": "TXN-1001"}) as call:
call.set_output(ledger.get("TXN-1001")) # span gate runs on exit
session.set_output(answer) # output gate runs on exit
except InvariantViolation as violation:
print(violation.report.summary()) # the answer was never released
from agent_eval import evaluate_agent, EvaluationSession
@evaluate_agent(invariants=registry)
def answer_dispute(message: str, session: EvaluationSession) -> str:
with session.tool("ledger.get_transaction") as call:
call.set_output({"amount": 42.50})
return "A refund of $42.50 has been issued."
answer_dispute("Refund TXN-1001") # returns the answer, or raises InvariantViolation
answer_dispute.last_report # EvalReport of the most recent call
The call arguments become the trajectory input and the return value becomes the final output.
The session parameter is optional; code deeper in the stack can call current_session().
Plain functions, async def functions, generators and async generators are all supported.
Under concurrent requests, read the report with with_report, which is race free.
last_report is one shared slot and is only reliable in single threaded use.
result, report = answer_dispute.with_report("Refund TXN-1001")
A generator agent is evaluated as a whole stream. The joined chunks are the final output.
@evaluate_agent(invariants=registry) # stream="buffer" is the default
def stream_answer(message: str):
yield "A refund of "
yield "$42.50 has been issued."
| Mode | Behaviour | Guarantee |
|---|---|---|
stream="buffer" (default) | Consume the stream inside the session, run the output gate, then yield the chunks | Fail closed. A blocked run releases nothing |
stream="passthrough" | Yield chunks live, run the output gate when the stream ends | The violation is raised after the last chunk. Text is already out |
A plain function that returns a generator or coroutine object is rejected with a TypeError,
because the session would close before any work happened. Decorate the generator itself.
Each assert_* factory returns an invariant that can also be called directly:
from agent_eval import assert_no_pii, assert_pci_compliant
assert_no_pii()("contact jane.doe@example.com") # raises InvariantViolation
assert_pci_compliant()("card ending 1111") # passes
Pass enforce=False to collect a report without raising. Use it to measure
Add extra card network BIN ranges (Maestro, RuPay) to known_card_bin
https://github.com/sankaranainar-p/agent-invariant-eval/issues/4
Labels: enhancement, good first issue
Add IBAN PII detection with ISO 13616 checksum
https://github.com/sankaranainar-p/agent-invariant-eval/issues/3
Labels: enhancement, good first issue
Support numeric grounding for any number, not just monetary figures
https://github.com/sankaranainar-p/agent-invariant-eval/issues/2
Labels: enhancement, help wanted
Contributions are welcome! Please keep changes focused, deterministic (no LLM calls in core invariants), and covered by tests.
git clone https://github.com/sankaranainar-p/agent-invariant-eval.git
cd agent-invariant-eval
pip install -e ".[dev]"
Run the same checks that CI runs:
ruff check src tests examples
mypy src
pytest --cov=agent_eval --cov-fail-under=90
python examples/payment_dispute_agent/run_eval.py
Do not open public issues for security vulnerabilities or detection bypasses. See SECURITY.md for private reporting instructions.