惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

Security Latest
Security Latest
Scott Helme
Scott Helme
C
Cybersecurity and Infrastructure Security Agency CISA
Engineering at Meta
Engineering at Meta
小众软件
小众软件
Security Archives - TechRepublic
Security Archives - TechRepublic
The Last Watchdog
The Last Watchdog
Cloudbric
Cloudbric
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
G
GRAHAM CLULEY
N
News | PayPal Newsroom
宝玉的分享
宝玉的分享
Y
Y Combinator Blog
人人都是产品经理
人人都是产品经理
云风的 BLOG
云风的 BLOG
U
Unit 42
Attack and Defense Labs
Attack and Defense Labs
Microsoft Security Blog
Microsoft Security Blog
博客园_首页
Hacker News: Ask HN
Hacker News: Ask HN
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
M
MIT News - Artificial intelligence
T
Threat Research - Cisco Blogs
P
Privacy & Cybersecurity Law Blog
Martin Fowler
Martin Fowler
Forbes - Security
Forbes - Security
GbyAI
GbyAI
Cisco Talos Blog
Cisco Talos Blog
AWS News Blog
AWS News Blog
酷 壳 – CoolShell
酷 壳 – CoolShell
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
S
SegmentFault 最新的问题
J
Java Code Geeks
T
Tenable Blog
L
Lohrmann on Cybersecurity
Webroot Blog
Webroot Blog
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
H
Help Net Security
The Hacker News
The Hacker News
Schneier on Security
Schneier on Security
The Cloudflare Blog
月光博客
月光博客
F
Fortinet All Blogs
The GitHub Blog
The GitHub Blog
aimingoo的专栏
aimingoo的专栏
博客园 - 【当耐特】
雷峰网
雷峰网
T
Troy Hunt's Blog
罗磊的独立博客
L
LangChain Blog

DEV Community

Authentication Security Deep Dive: From Brute Force to Salted Hashing (With Java Examples) Why AI Systems Don’t Fail — They Drift Spilling beans for how i learn for exam😁"Reinforcement Learning Cheat Sheet" I Replaced Chrome with Safari for AI Browser Automation. Here's What Broke (and What Finally Worked) How Python Borrows Other People's Work The $40 Architecture: Processing 1 Billion API Requests with 99.99% Uptime Vibe Coding: A Workflow Guide (From Zero to SaaS) Most webhook security guides protect the wrong side. The scary part is delivery. Headless CMS for TanStack Start: Build a Blog with Cosmic EU Age Verification App "Hacked in 2 Minutes" — What Actually Happened Comfy Cloud’s delete function does not actually remove files Running AI Models on GPU Cloud Servers: A Beginner Guide Event-driven media intelligence with AWS Step Functions and Bedrock I scored 500 AI prompts across 8 quality dimensions — here's what broke How to Call Google Gemini API from Next.js (Free Tier, No Backend Needed) The Portal Protocol: Reclaiming Human Connection in the Age of AI How to Fix Your Team's Scattered Knowledge Problem With a Self-Hosted Forum Intro to tc Cloud Functors: A Graph-First Mental Model for the Modern Cloud Designing Multi-Tenant Backends With Both Ownership and Team Access I Built a Neumorphic CSS Library with 77+ Components — Here's What I Learned PostgreSQL Performance Optimization: Why Connection Pooling Is Critical at Scale Cómo construí un SaaS multi-rubro para gestionar expensas en Argentina con FastAPI + Vue 3 🚀 I Built an Ethical Hacking Scanner Tool – Open Source Project I Replaced /usage and /context in Claude Code With a Single Statusline A Pythonic Way to Handle Emails (IMAP/SMTP) with Auto-Discovery and AI-Ready Design I Collected 8.9 Million Polymarket Price Points — Here's What I Found About How Markets Really Move EcoTrack AI — Carbon Footprint Tracker & Dashboard Everyone's Using AI. No One Agrees How. 5 self-hosted ebook managers worth trying in 2026 Building Your First AI Agent with LangChain: From Chatbot to Autonomous Assistant Common SOC 2 Failures (Real World) Stop Vibe-Checking Your AI App: A Practical Guide to Evals How to Use SonarQube and SonarScanner Locally to Level Up Your Code Quality Your Next To-Do App Is Dead — I Replaced Mine with an OpenClaw AI Sign a Nostr event in 60 lines of Python using coincurve — no nostr-sdk, no nbxplorer, no rust toolchain ITGC Audit Explained Like You’re in Big 4 Patch Tuesday abril 2026: Microsoft parcha 163 vulnerabilidades y un zero-day en SharePoint Stop scraping everything: a better way to track competitor price changes Listing on MCPize + the Official MCP Registry while routing payments OUTSIDE the marketplace — how I kept 100% of my x402 revenue Building an AI-Powered Risk Intelligence System Using Serverless Architecture Why We Ripped Function Overloading Out of Our AI Toolchain Testing AI-Generated Code: How to Actually Know If It Works SaaS Churn Is Killing Your Business. Here Is What to Do About It (Without a Support Team) The Speed of AI Is No Longer Linear - And Self-Improving Models Are Why How to Implement RBAC for MCP Tools: A Practical Guide for Engineering Teams From Standard Quote to Persuasive Proposal: AI Automation for Arborists I built a CLI that scaffolds complete multi-tenant SaaS apps Axios CVE-2025–62718: The Silent SSRF Bug That Could Be Hiding in Your Node.js App Right Now The dashboard that ended our friendship Data Pipelines Explained Simply (and How to Build Them with Python) The Hidden Cost of AI Systems Nobody Talks About. undefined vs undeclared, and how typeof behaves Switching from file-based jobs to NATS/Kafka in Rust without changing code io_uring Adventures: Rust Servers That Love Syscalls Why Agentic AI is Killing the Traditional Database The POUR principles of web accessibility for developers and designers Quantum Neural Network 3D — A Deep Dive into Interactive WebGL Visualization How To Install Caveman In Codex On macOS And Windows Automation Pipeline Reliability: Why Your Workflow Breaks When Nobody Is Watching I Built an 'Open World' AI Coding Agent — It Works From ANY Folder From Freelancing to Product: A Tech Service Company's SaaS Transformation China's AI Giants: Adding Tencent Hunyuan & ByteDance Doubao to AI University (74 Providers) On the Vibe Coders and Their Lies clerk: Auto-Summarize Your Claude Code Sessions AI Weekly — 2026/04/10–04/17 | The Model Lockdown Is Here, but the Toolchain Is the Real Battleground AI 週報 — 2026/04/10–2026/04/17 模型封鎖潮來了,但工具鏈才是真戰場 Maybe this is how Open-Source apps are born... 🚀 Fine-Tune LLMs with LoRA and QLoRA: 2026 Guide tRPC v11 + Next.js App Router: End-to-End Type Safety Without the Boilerplate ShadCN UI in 2026: Why I Stopped Installing Component Libraries and Started Owning My Components SaaS Billing in React Server Components: Stripe + Supabase Without a Single `useEffect` Join our DEV Weekend Challenge — $1,000 in Prizes Across TEN winners! Submissions Due April 20 at 6:59 AM UTC. Implementing FSRS Spaced Repetition in Flutter + Supabase — Adding Memory Science to an AI Learning App "I Texted My Localhost From the Train — Claude Code Fixed the Bug Before I Got Home" I Built a Sales Prep AI and It Went Deeper Than Expected Design to Code #2: One JSON, Eleven Outputs Solving the 100M-Row Problem: A Summary Table Pattern for High-Volume Push Notification Logs Flutter Web With Wasm: What Actually Changes For Developers I Built 50 Royalty-Free Soundtracks for My Side Project in a Weekend Using AI Music Generation The Vibe Coding Security Checklist: 7 Things to Check Before You Ship Stop Letting Googlebot Guess Fix Your React App's SEO Right Desconstruindo o Streaming do LinkedIn: Como Criar um Engine de Extração de Vídeo de Alta Performance com HLS e FFmpeg (EDA Part-1) EDA (Exploratory Data Analysis) Explained With Real Life — Why Looking at Your Data Is the Most Important Step in Machine Learning Brand Relationship Management at Scale: Our 4-Touch Outreach System for 200+ Brands Why String.fromEnvironment() Might Return an Empty String in Dart JGuardrails 1.0.0 — Hardening Java LLM Apps Against Jailbreaks, Toxicity, and Prompt Injection Plan and Schedule a Full Week of Threads Content From One Claude Conversation Coding Cat Oran Ep3, Five Tables Changed Everything Updated: BFF Pattern I'm done watching freelancers get buried by 200 proposals. So I'm building the alternative. This is my first post BFS Algorithm in Java Step by Step Tutorial with Examples Tracking LLM Pricing Monthly: An Open Dataset for 22 AI Models How We Measure Content ROI on a Comparison Site: Revenue Attribution Without Perfect Data Introducing Nova AI Ops: The AI-Native Operating System for SRE Teams I built a free desktop video downloader for Windows — Grabbit How Talkie OCR Helps Vision-Impaired & Dyslexic Users Read the World Around Them VRCFaceTracking安装和iPhone面捕配置教程,有bug Even CrowdStrike Can't See Your Agents The Automation Gold Rush: What n8n Workflows and Claude Are Opening Up for Developers Right Now
Your LLM shouldn't be handling if/else logic. Here's a smarter way.
Tomasz Bilaszewski · 2026-06-18 · via DEV Community

I. The Expensive if/else Statement

LLMs are remarkably good at handling the unknown. Give them an edge case you've never anticipated, and they'll reason through it correctly. That's genuine value.

The problem is that most of your traffic isn't unknown. A payment that failed on the third retry, a VIP customer with a disputed charge, an order flagged at 0.9 risk score — you already know what to do with these. But if your architecture routes everything through the LLM, it pays full inference cost regardless:

from pydantic_ai import Agent
from enum import Enum

class Action(Enum):
    FLAG_FRAUD = "flag_fraud"
    OPEN_DISPUTE = "open_dispute"
    SCHEDULE_RETRY = "schedule_retry"
    ALERT_ACCOUNT_MANAGER = "alert_account_manager"
    STANDARD_PROCESSING = "standard_processing"

agent = Agent(
    "openai:gpt-4o-mini",
    output_type=Action,
    system_prompt="Decide what action to take on this order event.",
)

async def handle(event: dict) -> Action:
    result = await agent.run(str(event))
    return result.output

This works. It also pays LLM prices for decisions that — in a large portion of cases — are already fully determined by the input data.

The obvious fix — an if/elif pre-filter — creates a different problem. It works until you have 15 conditions, three engineers with conflicting opinions, a silent ordering bug, and tests that only verify outputs, not the logic structure itself. The spaghetti grows fast.

What you actually need is a layer that:

  • codifies what you already know as explicit, deterministic rules — zero API cost, microsecond latency
  • routes only the genuinely unknown cases to the LLM
  • stays auditable, testable, and readable as it grows

That's exactly what airules is built for. By the end of this article you'll have a working hybrid system that reserves the LLM for what it's actually good at — and handles everything else for free.


II. The core architecture: Facts, Rules, and Engines

Why type safety matters here

The problem with raw if/elif chains isn't just readability. event["risk_score"] is untyped — a missing key silently raises KeyError at runtime, a misspelled field name goes unnoticed until production, and nothing stops you from accidentally comparing a float to a string.

airules brings static typing to decision logic. Your IDE catches bad field references. Pyright flags type mismatches. The rule set becomes something a tool can inspect, not just a human can read.

Step 1: Define a Fact (your typed input schema)

from typing import Literal
from airules import Fact, Field, NumberField

OrderStatus = Literal["placed", "payment_failed", "fulfilled", "disputed", "refunded"]
CustomerTier = Literal["standard", "vip", "wholesale"]

class OrderEvent(Fact):
    status: Field[OrderStatus]
    amount_usd: NumberField[float]
    customer_tier: Field[CustomerTier]
    retry_count: NumberField[int]
    risk_score: NumberField[float]  # 0.0–1.0 from fraud detection service

Fact is not a Pydantic model or a dataclass — it's a purpose-built descriptor system where each field type (NumberField, Field, ListField) doubles as a predicate builder. That's what lets you write OrderEvent.risk_score.ge(0.85) as a first-class object rather than an inline comparison that lives and dies in one function.

Step 2: Build the engine

from airules import KnowledgeEngine, Rule, Default

class OrderRouter(KnowledgeEngine[OrderEvent, Action]):

    # High risk score is an unconditional block — checked first
    @Rule(OrderEvent.risk_score.ge(0.85))
    def high_risk(self, event: OrderEvent) -> Action:
        return Action.FLAG_FRAUD

    # A disputed charge always opens a case, regardless of other signals
    @Rule(OrderEvent.status.eq("disputed"))
    def dispute(self, event: OrderEvent) -> Action:
        return Action.OPEN_DISPUTE

    # Failed payment with retries remaining → schedule another attempt
    @Rule(
        OrderEvent.status.eq("payment_failed")
        & OrderEvent.retry_count.lt(3)
    )
    def retry(self, event: OrderEvent) -> Action:
        return Action.SCHEDULE_RETRY

    # VIP customer exhausted retries → human touch, not automation
    @Rule(
        OrderEvent.status.eq("payment_failed")
        & OrderEvent.customer_tier.eq("vip")
        & OrderEvent.retry_count.ge(3)
    )
    def vip_payment_exhausted(self, event: OrderEvent) -> Action:
        return Action.ALERT_ACCOUNT_MANAGER

    # Fires only when nothing above matched
    @Default
    def fallback(self, event: OrderEvent) -> Action:
        return Action.STANDARD_PROCESSING

How execution flows

Rules evaluate top-to-bottom. The first match wins — no fall-through, no ambiguity. @Default is always last, regardless of where you declare it.

router = OrderRouter()

router.run(OrderEvent(
    status="payment_failed",
    amount_usd=149.00,
    customer_tier="vip",
    retry_count=3,
    risk_score=0.2,
))
# → Action.ALERT_ACCOUNT_MANAGER  (matched `vip_payment_exhausted`, stopped there)

The engine is Generic[OrderEvent, Action]run() returns Action | None, fully typed. No casting, no Any, no surprises.

Trade-off to know: airules uses declaration order as the default priority. If two rules can match the same input, the one declared first wins. This is explicit and predictable, but it means rule ordering is load-bearing — treat it with the same care you'd treat database index order. In the example above, high_risk must come before dispute because a high-risk disputed order should be flagged for fraud, not just opened as a dispute case.


III. Serializable Rules: Your LLM can read them too

The problem with hardcoded logic

If your rules live only in Python source, the rest of your system is blind to them. Your database can't query them. A code review diff shows syntax changes, not logic changes. And critically — your LLM can't reason about them.

Rules as data

airules predicates are first-class objects that serialize to plain dicts:

p = (
    OrderEvent.status.eq("payment_failed")
    & OrderEvent.retry_count.lt(3)
)

p.to_dict()
# {
#   "op": "and",
#   "operands": [
#     {"op": "eq",  "field": "status",      "value": "payment_failed"},
#     {"op": "lt",  "field": "retry_count", "value": 3}
#   ]
# }

# Round-trips cleanly
restored = Predicate.from_dict(p.to_dict())

One level up, describe() dumps the entire rule set — every rule, its predicate, its priority:

import json
print(json.dumps(OrderRouter.describe(), indent=2))
# {
#   "facts": [{"name": "OrderEvent", "fields": {
#     "status": "Field", "amount_usd": "NumberField",
#     "customer_tier": "Field", "retry_count": "NumberField", "risk_score": "NumberField"
#   }}],
#   "rules": [
#     {"name": "high_risk",            "predicate": {...}, "priority": 5, "is_default": false},
#     {"name": "dispute",              "predicate": {...}, "priority": 4, "is_default": false},
#     {"name": "retry",                "predicate": {...}, "priority": 3, "is_default": false},
#     {"name": "vip_payment_exhausted","predicate": {...}, "priority": 2, "is_default": false},
#     {"name": "fallback",             "predicate": null,  "priority": 0, "is_default": true}
#   ]
# }

Store it. Diff it in PRs. Feed it into a rules-editor UI. Or — and this is the key move — pass it directly into your LLM's system prompt.


IV. Building the hybrid router

The architecture

Incoming order event
        │
        ▼
┌──────────────────┐    match     ┌──────────────────┐
│   OrderRouter    │ ──────────▶  │  Return Action   │  ← ~0ms, $0.00
│   (airules)      │              └──────────────────┘
│                  │    no match
│                  │ ──────────▶  ┌──────────────────┐
└──────────────────┘              │  LLM fallback    │  ← ~800ms, costs tokens
                                  └──────────────────┘

Rules handle the known cases for free. The LLM handles only what genuinely falls through — unusual combinations of signals that no single rule anticipated.

The @Default fallback

The LLM call lives inside @Default — the method that fires only when the engine found no match. Everything from sections II and III stays the same; you're replacing just the fallback method:

import json
from pydantic_ai import Agent
from airules import Fact, Field, NumberField, KnowledgeEngine, Rule, Default

class OrderRouter(KnowledgeEngine[OrderEvent, Action]):

    @Rule(OrderEvent.risk_score.ge(0.85))
    def high_risk(self, event: OrderEvent) -> Action:
        return Action.FLAG_FRAUD

    @Rule(OrderEvent.status.eq("disputed"))
    def dispute(self, event: OrderEvent) -> Action:
        return Action.OPEN_DISPUTE

    @Rule(
        OrderEvent.status.eq("payment_failed")
        & OrderEvent.retry_count.lt(3)
    )
    def retry(self, event: OrderEvent) -> Action:
        return Action.SCHEDULE_RETRY

    @Rule(
        OrderEvent.status.eq("payment_failed")
        & OrderEvent.customer_tier.eq("vip")
        & OrderEvent.retry_count.ge(3)
    )
    def vip_payment_exhausted(self, event: OrderEvent) -> Action:
        return Action.ALERT_ACCOUNT_MANAGER

    @Default
    async def llm_triage(self, event: OrderEvent) -> Action:
        # Only reached for combinations no rule anticipated —
        # e.g. a wholesale customer with moderate risk (0.6) and a fulfilled
        # order that was just disputed for the first time.
        rules_schema = json.dumps(type(self).describe(), indent=2)

        agent = Agent(
            "openai-chat:gpt-5.4-mini",,
            output_type=Action,
            system_prompt=(
                "You are an order event classifier. "
                "The rules below already handle known cases deterministically — "
                "you only receive events that matched none of them. "
                "Decide the best action for this edge case.\n\n"
                f"Existing rules:\n{rules_schema}"
            ),
        )
        result = await agent.run(str(event))
        return result.output


router = OrderRouter()
action = await router.run_async(event)

Why type(self).describe() is the key line

Without it, your LLM and your rules engine are two separate systems with no shared understanding. The model might return SCHEDULE_RETRY for an event that should have been caught by the high_risk rule — creating inconsistencies that are maddening to debug.

With it, the LLM receives a precise, machine-readable description of every rule that already exists. It knows exactly which cases are handled upstream, so it can't contradict them — and it focuses only on the genuine gap.

The model isn't duplicating your rules. It's extending them.

run_async and evaluate_async handle async @Default methods automatically. Your synchronous @Rule methods don't need to change.


V. Observability: turning defaults into a roadmap

The metric that matters

Once this is running, the number to watch isn't "how many events did the engine handle." It's "what percentage hit @Default?"

  • 10% default rate → rules handle 90% of traffic; the LLM sees only genuine edge cases
  • 40% default rate → rules are missing major patterns; significant known traffic still hitting the API
  • 80% default rate → back to nearly the original problem; your rules aren't covering enough

The gap between those numbers is the payoff from getting your rules right.

Injecting an observer

The engine accepts an observer that receives every evaluation result — without blocking the engine:

from airules import LoggingObserver

# Built-in: logs every evaluation, warns loudly on @Default hits
router = OrderRouter(observer=LoggingObserver())
await router.run_async(event)
# INFO     OrderRouter: rule='retry' result=<Action.SCHEDULE_RETRY: 'schedule_retry'>
# WARNING  OrderRouter: default fired — status='fulfilled', risk_score=0.62, customer_tier='wholesale'

For custom telemetry — Prometheus counters, Datadog traces, a Slack alert when the default rate spikes — implement OutcomeObserver:

import logging
from airules import OutcomeObserver, Outcome

class DefaultRateObserver(OutcomeObserver[OrderEvent, Action]):
    def __init__(self) -> None:
        self.total = 0
        self.defaults = 0

    def observe(
        self,
        outcome: Outcome[OrderEvent, Action],
        engine: KnowledgeEngine[OrderEvent, Action],
    ) -> None:
        self.total += 1
        if outcome.is_default:
            self.defaults += 1
            logging.warning("no rule matched: %s", outcome.fact)

    @property
    def default_rate(self) -> float:
        return self.defaults / self.total if self.total else 0.0

The iterative loop

Your @Default hits aren't failures — they're a data-driven roadmap:

  1. Observe — log every @Default hit with the full outcome.fact
  2. Analyze — what field combinations keep appearing? Wholesale customers with moderate risk scores? Fulfilled orders that get disputed?
  3. Add a rule — write one new @Rule for each pattern you can name
  4. Repeat — default rate drops, LLM calls drop, and the rules become a living record of your domain knowledge

The LLM is effectively labeling the gaps in your rule set. Every correct @Default classification is a signal that says "this combination needs a rule." Over time, the engine handles more and more traffic — the LLM handles less and less, reserved for inputs that genuinely benefit from its reasoning.


VI. When to use it — and when not to

Good fit:

  • Structured event routing — order events, webhooks, payment signals, API callbacks where inputs have typed, discrete fields
  • Business rule enforcement — eligibility checks, rate limiting, tier-based access where the logic is known and articulable
  • LLM cost guardrails — deterministically handle the known cases before they ever reach the API
  • Auditable decision logic — anywhere you need to explain why a specific input was handled a specific way

Poor fit:

  • Unstructured text classification — if your inputs are raw free text with no structured signals, keyword matching creates false positives. Extract structured fields first (even with a lightweight model), then apply rules to those.
  • 2–3 branch logic that won't grow — if you have two cases and a clear stopping condition, a plain if statement is the right tool. No ceremony needed.
  • Purely fuzzy matching — if you genuinely can't articulate a rule as a typed condition, the engine can't express it. ML classifiers or embedding-based retrieval handle that better.

Production note: airules is experimental at v0.1.1. The core API is stable in spirit, but details may still change. Pin your version and read the changelog before upgrading in production.


VII. Conclusion

Optimizing LLM usage isn't just about picking a cheaper model or adding a cache. It's about being honest about which parts of your pipeline actually need intelligence — and handling everything else deterministically.

A typed rules engine that encodes what you already know is the correct complement to an LLM. airules makes that layer explicit, auditable, and — via describe() — self-documenting enough to feed back into the LLM itself as context.


Get started:

pip install ai-rules-engine

Full working examples | Documentation

Building a similar hybrid system? Share your default rate in the comments — curious how different domains compare. And if airules is missing something you need, open an issue. The roadmap is driven by real use cases.