Why Practical AI Apps Need Deterministic Guardrails Around LLM Outputs
Lessons from building FosholLens: why prompts alone are not safety boundaries, and how plain code validators keep probabilistic models honest.
Prompts Suggest Behavior; Code Enforces Invariants
When building FosholLens—a Bangla crop advisory tool for rice and potato leaf diseases—relying on a single LLM prompt quickly revealed a critical domain risk. For bacterial leaf blight or tungro virus, chemical fungicides do not work, yet generic language models routinely hallucinate plausible-sounding fungicide instructions anyway.
Adding stronger wording like 'NEVER recommend fungicides for viral diseases' inside a system prompt reduces mistakes, but under noisy inputs or model fallbacks, prompts alone never give a 100% guarantee.
Isolating Perception From Rules and Validation
A much safer pattern is treating vision and language models strictly as perception and phrasing layers, while keeping domain rules in deterministic Python or TypeScript.
In FosholLens, once the classifier identifies a bacterial/viral class—or when confidence margin is too low—plain Python sets cultural_only = True before any text generation runs, restricts RAG retrieval exclusively to safe agronomic chunks, and runs a post-generation validator on the output.
def synthesize_safe_advisory(ctx: CropContext, retrieved_chunks: list[Chunk]) -> str:
allowed_chemicals = set() if ctx.cultural_only else extract_approved_names(retrieved_chunks)
draft = call_llm_writer(ctx, retrieved_chunks)
if validate_narrative(draft, allowed_chemicals=allowed_chemicals):
return draft
# Never ship unverified chemical names — fall back to a locked Bangla template
return render_verified_template(ctx, retrieved_chunks)Designing for Offline and Degraded Conditions
Real-world deployments also face rate limits, API timeouts, and poor mobile connectivity. Pairing cloud APIs with deterministic fallbacks—such as precomputed vector + local TF-IDF retrieval and CPU ONNX inference—keeps core user flows working reliably even when external services degrade.
Key Takeaway
Use AI where ambiguity lives (vision, voice, natural phrasing), and use deterministic code and SQL where correctness is non-negotiable (math, permissions, and safety constraints).