Sunday, 31 May 2026

Guardrail Placement: A Multi-Layered Approach

 

Most people feel, placing guardrails exclusively at the output stage. This wastes API compute money on bad queries, exposes the system to dangerous reasoning states, and adds noticeable latency

I Propose a Multi-Layered Framework:

Layer 1 (Input/Pre-processing): Implement cheap, fast checks (regex or light classifiers) for prompt injections, toxicity, and PII before spending money on the core agent execution

Layer 2 (Tool Level): Verify parameters before a tool is called, restricting dangerous commands (like blocking "DROP" SQL commands or capping row returns).

Layer 3 (Reasoning/Execution): Monitor the agent while it runs to prevent infinite loops, excessive tool calls, or runaway costs

Layer 4 (Output): Serve as the final defense to ensure no PII or system prompts leaked, accepting higher latency only as a final check.


Gliner API's can be used for Guard Rails, it detects Personally Identifiable Information in text. RegEx can also be used in the Inputlayer

Cost Optimization for AI Applications

 Cost Optimization: Analyzing Before Optimizing

First step, Dig into Logs and Analyze the cost breakdown. Upon Analyzing logs, new insights can be found.

Eg: After cost breakdown Analysis if below is the finding. $8000/month

Model cost: $2,400 - 30%

Embeddings: $2000 - 25%

Vector Databases: $1200 - 15%

Wasted Compute: $2400 - 30%


Cost Breakdown Analysis: You must identify where the money is going by analyzing:

  • Embedding Costs: Are you embedding redundant metadata (headers, footers) unnecessarily? For some applications, Metadata is key and this cannot be applicable.

  • Vector Database Costs: Are you over-indexing or storing unnecessary historical document versions? Eg: If 10 versions of a document are stored in Vector DB and when only the latest version is considered during the retrieval. It's unnecessary to store older versions and it can be replaced with latest document.

  • LLM Inference Costs: Analyze token distribution (input context length vs. output response length). If the response is longer than necessary, this can be taken care by adjusting the prompt as necessary.


Architectural Optimizations: You can cut 40-70% of costs before even touching the model choice by implementing :

  • Semantic Caching: Deduplicating similar queries (e.g., password resets) rather than repeatedly processing them. Semantic Caching is a strategy used to optimize performance and reduce costs in LLM-powered applications by storing and retrieving meanings (vectors) rather than exact text strings. Semantic caching leverages your existing Vector Database (or a specialized cache layer) to perform "similarity lookups" before calling the LLM.

  • Input Filtering: Using simple rules for basic greetings instead of full LLM reasoning.

  • Context Window Reduction: Limiting retrieved documents to only the top 3 most relevant results instead of 10.

  • Batching: Processing document embeddings in large batches rather than one at a time. Most embedding APIs support Batching. Eg: Embed 100 chunks as a Batch per API call.

Tiered Model Routing: Only after architecture is optimized should you route simple tasks to cheaper models (like GPT-3.5) and reserve expensive models (GPT-4) strictly for complex reasoning. 

Saturday, 16 May 2026

Building a ReAct Agent with LangGraph & LangSmith

In this post, I walk through building a ReAct (Reasoning + Acting) agent using LangGraph and Groq's openai/gpt-oss-120b model, where the LLM dynamically decides when to call a Wikipedia tool to answer factual questions. 


The agent is wired as a stateful graph — looping between reasoning and tool use until it has a confident answer. To trace every LLM call, tool invocation, and token usage in real time, LangSmith is integrated by passing a LangChainTracer directly via config={"callbacks": [tracer]} — a more reliable approach than relying on environment variables in Jupyter notebooks. 


Check out the full code and setup on GitHub.


Sourcehttps://www.youtube.com/watch?v=3h4Wc19LhL8

Sunday, 3 May 2026

Mastering LLM Specialization: Fine-Tuning, LoRA, and QLoRA

In the rapidly evolving landscape of Artificial Intelligence, a base model is often just the starting point. To transform a general-purpose LLM into a domain expert, we look toward Fine-Tuning.


What is Fine-Tuning?

Fine-tuning is the process of training a pre-existing LLM to achieve specialized capabilities. While a base model has broad knowledge, fine-tuning allows you to tailor its tone, behavior, and specific knowledge to meet niche domain needs.

Key Benefits:

  • Nuanced Communication: Effectively handles emotions, cultural slangs, and specific dialects.

  • Brand Alignment: A company chatbot can be fine-tuned to represent a specific brand voice (e.g., showing high empathy or professional rigor).

  • Specialized Knowledge: Enhances the model’s deep understanding of proprietary or industry-specific data.

Example: Using Llama-3.2-1B as a base model and fine-tuning it specifically to follow commands results in the Llama-3.2-1B-Instruct model.


RAG vs. Fine-Tuning

Most enterprises choose between—or combine—Retrieval-Augmented Generation (RAG) and Fine-Tuning.

FeatureRAGFine-Tuning
ProsEasy to update knowledge; handles dynamic data; lower cost.Consistent style; faster runtime; tailored for specific tasks.
ConsSlower (requires retrieval step); depends on retrieval quality.Expensive and time-consuming to retrain.

The Industry Standard: Many enterprises now utilize a hybrid approach, leveraging RAG for dynamic data access and Fine-Tuning for behavioral consistency.


PEFT & LoRA: Efficiency Reimagined

PEFT (Parameter-Efficient Fine-Tuning) is a technique that keeps the original model parameters frozen and only adds/trains new, smaller layers.

LoRA (Low-Rank Adaptation) is the most prominent PEFT technique. It represents updates to the model weights mathematically as:

W' = W_{frozen} + \Delta W$

Instead of updating the entire weight matrix, LoRA learns a "low-rank" representation of the changes, drastically reducing the number of trainable parameters.


Understanding Quantization

Quantization is the process of converting high-precision numbers (like Float32) into lower-precision formats (like Int8 or 4-bit) to reduce memory and compute requirements.

The Impact on Memory (e.g., Llama 7B):

  • At 4 Bytes per parameter: $7  Billion \times 4 Bytes = 28 GB

  • At 1 Byte per parameter: $7 Billion \times 1 Byte = 7 GB

This efficiency is vital for edge devices with memory constraints, such as drones or mobile hardware.


QLoRA: The Best of Both Worlds

QLoRA (Quantized Low-Rank Adaptation) combines quantization with LoRA to save memory without sacrificing intelligence. It relies on three major innovations:

  1. 4-bit NormalFloat (NF4): Compresses weights to 4-bit using a mathematical distribution optimized for neural networks.

  2. Double Quantization: Quantizes the quantization constants themselves, saving an additional 0.5 bits per parameter.

  3. Paged Optimizers: Uses CPU RAM as a buffer during GPU memory spikes to prevent training crashes.


FeatureLoRAQLoRA
Model Precision16-bit (Half-precision)4-bit (Compressed)
Memory (7B Model)~28 GB VRAM~12–16 GB VRAM
HardwareA100 or high-end WorkstationConsumer GPUs (RTX 3090/4090)
Accuracy95-100% of full fine-tuning~90-95% (Very close)
Best ForMaximum accuracy on enterprise GPUsBudget-conscious scaling or 70B models

Significance of Fallback Mechanism

  The Core Concept: Reliability in Production While many focus heavily on choosing the most powerful models when building AI agents, the mo...