A small, typed, declarative rules engine for Python. Replace brittle if/elif chains with typed, composable predicates - and route only genuine edge cases to the model.
pip install ai-rules-engine
from airules import Fact, KnowledgeEngine, Rule, Default, StringFieldfrom pydantic_ai import Agentclass Ticket(Fact):subject: StringFieldbody: StringFieldclass TicketRouter(KnowledgeEngine[Ticket, Team]):@Rule(Ticket.subject.contains("billing", case_insensitive=True)| Ticket.body.contains("invoice", case_insensitive=True))def billing(self, ticket: Ticket) -> Team:return Team.BILLING@Rule(Ticket.subject.contains("password", case_insensitive=True))def auth(self, ticket: Ticket) -> Team:return Team.AUTH@Defaultdef llm_fallback(self, ticket: Ticket) -> Team:# Only reached for inputs no rule could predictreturn agent.run_sync(ticket.subject).output
KnowledgeEngine[FactType, ReturnType] - your editor catches misspelled fields and mismatched return types before you run anything. Pyright strict-friendly throughout.
Deterministic rules evaluate in microseconds at zero API cost. Only inputs that fall outside every rule reach the model - the ones that genuinely need reasoning.
Predicates compose with & | ~ and round-trip through to_dict(). Call describe() to dump the entire rule set - diff deployments, build a rules editor, or feed it back into the LLM so it knows exactly what's already covered.
Most decisions in production are already known. A rules engine handles them for free. The LLM handles the rest.
Add a rules layer in front of your model in minutes.