<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Ahmet Ozan GAZİ</title>
    <description>The latest articles on DEV Community by Ahmet Ozan GAZİ (@ozy31).</description>
    <link>https://dev.to/ozy31</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F1315268%2F67e138c9-df75-4d0f-917d-8a963e6c4bc7.jpeg</url>
      <title>DEV Community: Ahmet Ozan GAZİ</title>
      <link>https://dev.to/ozy31</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/ozy31"/>
    <language>en</language>
    <item>
      <title>I Autoscaled My LLM on Queue Depth. It Scaled Down Under Full Load.</title>
      <dc:creator>Ahmet Ozan GAZİ</dc:creator>
      <pubDate>Wed, 18 Nov 2026 14:21:57 +0000</pubDate>
      <link>https://dev.to/ozy31/i-autoscaled-my-llm-on-queue-depth-it-scaled-down-under-full-load-5do5</link>
      <guid>https://dev.to/ozy31/i-autoscaled-my-llm-on-queue-depth-it-scaled-down-under-full-load-5do5</guid>
      <description>&lt;p&gt;&lt;em&gt;From DevOps to AI Platform Engineering — Part 4&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;In &lt;a href="https://dev.to/ozy31/i-built-a-self-hosted-chatgpt-on-my-laptop-heres-what-broke-2gci"&gt;part 1&lt;/a&gt; I built &lt;strong&gt;OzAI&lt;/strong&gt;, a self-hosted LLM platform on a MacBook Pro M1 Pro. In &lt;a href="https://dev.to/ozy31/your-llm-returns-200-ok-and-still-fails-how-i-monitor-a-self-hosted-model-16il"&gt;part 2&lt;/a&gt; I added monitoring and a load test. In &lt;a href="https://dev.to/ozy31/rag-took-my-local-llm-from-110-to-910-the-last-bugs-werent-the-model-1df6"&gt;part 3&lt;/a&gt; RAG took it from 1/10 to 9/10 correct answers.&lt;/p&gt;

&lt;p&gt;Part 3 ended with a promise: move OzAI to Amazon EKS with GPU nodes, serve it with vLLM, and autoscale on queue depth with KEDA.&lt;/p&gt;

&lt;p&gt;I'm going to keep half of that promise, and I want to be upfront about which half.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;What I didn't do:&lt;/strong&gt; I didn't run GPUs on AWS for this article. So there are &lt;strong&gt;no measured vLLM or GPU numbers here.&lt;/strong&gt; Any GPU number below is labeled as an estimate or a formula.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;What I did do:&lt;/strong&gt; the autoscaling logic is the part that breaks in production, and it doesn't need a GPU to test. So I built the same scaling chain in a local Kubernetes cluster and load tested it three times. The second run found a bug in my own design. The third run fixed it.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fbkqbzums7y3d2gr1di10.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fbkqbzums7y3d2gr1di10.png" alt="Replica count over time during a 1 → 8 → 1 user step load: scaling on waiting requests drops from 3 to 2 replicas under full load, scaling on in-flight requests stays at 3; TTFT p50 35.3 s with 1 replica, 12.4 s and 4.7 s with autoscaling" width="800" height="336"&gt;&lt;/a&gt;&lt;/p&gt;




&lt;h2&gt;
  
  
  The design I wanted to test
&lt;/h2&gt;

&lt;p&gt;I already have a Terraform repo for the EKS version: &lt;a href="https://github.com/ozy31/terraform-aws-eks-ai-cluster" rel="noopener noreferrer"&gt;terraform-aws-eks-ai-cluster&lt;/a&gt;. Karpenter launches GPU nodes, model weights are mounted from S3, vLLM serves the model, and KEDA scales the vLLM Deployment on this Prometheus query:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;sum(vllm:num_requests_waiting{model_name="qwen3-8b"})
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The idea is common and, I think, right: &lt;strong&gt;for LLM serving, CPU and GPU utilization are bad scaling signals.&lt;/strong&gt; A GPU running vLLM shows high utilization whether it serves 2 users or 20. What users feel is &lt;em&gt;waiting&lt;/em&gt;. So you scale on the number of requests waiting in the server's queue.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;terraform validate&lt;/code&gt; passes. That proves the HCL is valid, not that the scaling works.&lt;/p&gt;

&lt;h2&gt;
  
  
  The local lab
&lt;/h2&gt;

&lt;p&gt;Everything runs in &lt;a href="https://kind.sigs.k8s.io/" rel="noopener noreferrer"&gt;kind&lt;/a&gt; (Kubernetes in Docker) on the laptop. Docker gets 8 vCPUs and 8 GB.&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;EKS design&lt;/th&gt;
&lt;th&gt;Local lab&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;vLLM on an NVIDIA GPU&lt;/td&gt;
&lt;td&gt;llama.cpp server on CPU&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;vllm:num_requests_waiting&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;&lt;code&gt;llamacpp:requests_deferred&lt;/code&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Qwen3 8B&lt;/td&gt;
&lt;td&gt;Qwen2.5 0.5B (Q4), so 3 replicas fit in 8 GB&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Karpenter adds GPU nodes&lt;/td&gt;
&lt;td&gt;one node, pods only&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Weights from S3 via Mountpoint&lt;/td&gt;
&lt;td&gt;weights from a host folder&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;kube-prometheus-stack&lt;/td&gt;
&lt;td&gt;Prometheus server only, 5s scrape&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;KEDA ScaledObject&lt;/td&gt;
&lt;td&gt;the same ScaledObject, shorter windows&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;Why llama.cpp instead of vLLM? vLLM is built for NVIDIA GPUs. For this test I need two behaviors from the server, and llama.cpp has both:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;Parallel slots with continuous batching&lt;/strong&gt; (&lt;code&gt;--parallel 2&lt;/code&gt;): each pod serves 2 requests at once, and more requests queue inside the pod.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;A queue metric in Prometheus format&lt;/strong&gt; (&lt;code&gt;--metrics&lt;/code&gt;): &lt;code&gt;requests_processing&lt;/code&gt; and &lt;code&gt;requests_deferred&lt;/code&gt; (waiting).&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Each pod gets 2 CPUs and 2 slots, with min 1 and max 3 replicas. Here is the ScaledObject. Production waits 2 minutes between scale-ups and 10 minutes before scaling down, because each replica is a GPU node. I shortened those windows so a run fits in 8 minutes:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight yaml"&gt;&lt;code&gt;&lt;span class="na"&gt;spec&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
  &lt;span class="na"&gt;scaleTargetRef&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="s"&gt;name:&lt;/span&gt; &lt;span class="s"&gt;ozai-lab&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;
  &lt;span class="na"&gt;minReplicaCount&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;
  &lt;span class="na"&gt;maxReplicaCount&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt;
  &lt;span class="na"&gt;advanced&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="na"&gt;horizontalPodAutoscalerConfig&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
      &lt;span class="na"&gt;behavior&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
        &lt;span class="na"&gt;scaleUp&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
          &lt;span class="na"&gt;policies&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="s"&gt;type:&lt;/span&gt; &lt;span class="s"&gt;Pods&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s"&gt;value:&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s"&gt;periodSeconds:&lt;/span&gt; &lt;span class="mi"&gt;15&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
        &lt;span class="na"&gt;scaleDown&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
          &lt;span class="na"&gt;stabilizationWindowSeconds&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;60&lt;/span&gt;
          &lt;span class="na"&gt;policies&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="s"&gt;type:&lt;/span&gt; &lt;span class="s"&gt;Pods&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s"&gt;value:&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s"&gt;periodSeconds:&lt;/span&gt; &lt;span class="mi"&gt;30&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
  &lt;span class="na"&gt;triggers&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="p"&gt;-&lt;/span&gt; &lt;span class="na"&gt;type&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s"&gt;prometheus&lt;/span&gt;
      &lt;span class="na"&gt;metadata&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
        &lt;span class="na"&gt;serverAddress&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s"&gt;http://prometheus-server.monitoring.svc:80&lt;/span&gt;
        &lt;span class="na"&gt;query&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s"&gt;sum(llamacpp:requests_deferred{namespace="ozai"}) or vector(0)&lt;/span&gt;
        &lt;span class="na"&gt;threshold&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;"2"&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The load test is a step load: &lt;strong&gt;1 user for 60 s, then 8 users for 240 s, then 1 user for 180 s.&lt;/strong&gt; Each user sends the same streamed request as in part 2 (128 tokens max), and starts the next one as soon as the previous one finishes. Every 2 seconds a sampler records the replica count and the queue size from Prometheus.&lt;/p&gt;

&lt;h2&gt;
  
  
  Run 1: no autoscaling
&lt;/h2&gt;

&lt;p&gt;First, a baseline with KEDA paused at one replica (the &lt;code&gt;autoscaling.keda.sh/paused-replicas: "1"&lt;/code&gt; annotation).&lt;/p&gt;

&lt;p&gt;With 1 user, time to first token (TTFT) is &lt;strong&gt;0.05 s&lt;/strong&gt; and the model generates &lt;strong&gt;22 tokens/s&lt;/strong&gt;. That's what one user sees when the platform is idle.&lt;/p&gt;

&lt;p&gt;With 8 users, 2 requests are served and 6 wait. &lt;strong&gt;TTFT p50 is 35 s.&lt;/strong&gt; No request failed. All of them were just slow. As in part 2, the platform returns &lt;code&gt;200 OK&lt;/code&gt; while users wait half a minute for the first word.&lt;/p&gt;

&lt;h2&gt;
  
  
  Run 2: autoscaling on waiting requests
&lt;/h2&gt;

&lt;p&gt;Same load with KEDA on:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;The queue grew to 6 waiting requests. KEDA asked for a 2nd replica about &lt;strong&gt;17 s&lt;/strong&gt; after the load started, and a 3rd about &lt;strong&gt;32 s&lt;/strong&gt; after.&lt;/li&gt;
&lt;li&gt;Each new pod was ready in about &lt;strong&gt;2 s&lt;/strong&gt;, because the model is 0.5B and already on local disk. On EKS this is the expensive part, more on that below.&lt;/li&gt;
&lt;li&gt;TTFT p50 dropped from 35 s to &lt;strong&gt;12 s&lt;/strong&gt;. Completed requests went from 48 to 83.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Then I looked at the replica timeline:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;t=  77s  desired=2  ready=1   waiting=6    &amp;lt;- 8 users arrive at t=60
t=  92s  desired=3  ready=2
t= 197s  desired=2  ready=2   waiting=4    &amp;lt;- scaled DOWN with 8 users still active
t= 241s  desired=3  ready=2   waiting=6    &amp;lt;- and back up
t= 362s  desired=2                         &amp;lt;- load is over
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;The autoscaler removed a replica in the middle of full load,&lt;/strong&gt; ran with 2 replicas for about 44 s, and then added the replica back.&lt;/p&gt;

&lt;h2&gt;
  
  
  Why it scaled down: the metric is measuring its own fix
&lt;/h2&gt;

&lt;p&gt;KEDA's Prometheus scaler gives the HPA a target average value, so desired replicas are roughly:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;desired = ceil(metric / threshold)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Put the numbers in:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;1 pod, 8 users: 2 processing, &lt;strong&gt;6 waiting&lt;/strong&gt;. &lt;code&gt;ceil(6 / 2) = 3&lt;/code&gt;. Scale up. Correct.&lt;/li&gt;
&lt;li&gt;3 pods, 8 users: 6 processing, &lt;strong&gt;2 waiting&lt;/strong&gt;. &lt;code&gt;ceil(2 / 2) = 1&lt;/code&gt;. &lt;strong&gt;Scale down.&lt;/strong&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Adding replicas moves requests from &lt;em&gt;waiting&lt;/em&gt; to &lt;em&gt;processing&lt;/em&gt;, and waiting goes down. To a metric that only counts waiting requests, a fix looks the same as the load going away. Once the 60 s stabilization window passes, the HPA removes a pod. The queue grows again and the pod comes back.&lt;/p&gt;

&lt;p&gt;The production chart in my Terraform repo has the same flaw. Its 10-minute scale-down window hides it, because the dip has to last 10 minutes before anything happens. But the flaw is still there, and with GPU nodes each flap could mean another node launch.&lt;/p&gt;

&lt;h2&gt;
  
  
  The fix: scale on requests in flight
&lt;/h2&gt;

&lt;p&gt;The question the autoscaler needs to answer is "how many replicas does this many requests need?", not "is anyone waiting?". So the metric should be &lt;strong&gt;all requests in the system&lt;/strong&gt;, processing plus waiting, and the threshold should be &lt;strong&gt;one pod's capacity&lt;/strong&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight yaml"&gt;&lt;code&gt;&lt;span class="na"&gt;query&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s"&gt;sum(llamacpp:requests_processing{namespace="ozai"} + llamacpp:requests_deferred{namespace="ozai"}) or vector(0)&lt;/span&gt;
&lt;span class="na"&gt;threshold&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;"2"&lt;/span&gt;   &lt;span class="c1"&gt;# = --parallel slots per pod
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now 8 users means 8 in flight and &lt;code&gt;ceil(8 / 2) = 4&lt;/code&gt;, capped at 3. Scaling up doesn't change the number, so it doesn't undo itself.&lt;/p&gt;

&lt;p&gt;For vLLM, the equivalent is &lt;code&gt;vllm:num_requests_running + vllm:num_requests_waiting&lt;/code&gt;. The threshold is the number of requests one replica can run at a good speed, which you get from a load test, not from &lt;code&gt;--max-num-seqs&lt;/code&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  Run 3: in-flight metric
&lt;/h2&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;8-user phase&lt;/th&gt;
&lt;th&gt;Baseline (1 replica)&lt;/th&gt;
&lt;th&gt;Waiting metric&lt;/th&gt;
&lt;th&gt;In-flight metric&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;Requests completed&lt;/td&gt;
&lt;td&gt;48&lt;/td&gt;
&lt;td&gt;83&lt;/td&gt;
&lt;td&gt;&lt;strong&gt;97&lt;/strong&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Output tokens/s (all users)&lt;/td&gt;
&lt;td&gt;25.4&lt;/td&gt;
&lt;td&gt;44.3&lt;/td&gt;
&lt;td&gt;&lt;strong&gt;51.7&lt;/strong&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;TTFT p50&lt;/td&gt;
&lt;td&gt;35.3 s&lt;/td&gt;
&lt;td&gt;12.4 s&lt;/td&gt;
&lt;td&gt;&lt;strong&gt;4.7 s&lt;/strong&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;TTFT p95&lt;/td&gt;
&lt;td&gt;35.6 s&lt;/td&gt;
&lt;td&gt;36.5 s&lt;/td&gt;
&lt;td&gt;35.9 s&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;TTFT p50 / p95 after scale-up&lt;/td&gt;
&lt;td&gt;–&lt;/td&gt;
&lt;td&gt;12.4 s / 35.5 s&lt;/td&gt;
&lt;td&gt;&lt;strong&gt;0.95 s / 23.9 s&lt;/strong&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Scale-down during load&lt;/td&gt;
&lt;td&gt;–&lt;/td&gt;
&lt;td&gt;yes&lt;/td&gt;
&lt;td&gt;&lt;strong&gt;no&lt;/strong&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Errors&lt;/td&gt;
&lt;td&gt;0&lt;/td&gt;
&lt;td&gt;0&lt;/td&gt;
&lt;td&gt;0&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;The in-flight metric scaled up faster (2nd replica at about 11 s, 3rd at about 25 s), stayed at 3 replicas for the whole load, and scaled back to 1 about 100 s after the load ended.&lt;/p&gt;

&lt;h2&gt;
  
  
  What autoscaling did not fix
&lt;/h2&gt;

&lt;p&gt;Look at TTFT p95: &lt;strong&gt;about 36 s in every run&lt;/strong&gt;, autoscaling or not. Two reasons, and both apply to vLLM too.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;1. Queued requests don't move to new pods.&lt;/strong&gt; When the 8 users arrived, the Service had already sent 8 requests to the one pod: 2 running and 6 queued inside the llama.cpp process. New pods only receive &lt;em&gt;new&lt;/em&gt; requests. The 6 queued ones waited on the old pod as if scaling never happened. On EKS, where a new replica takes minutes to start instead of 2 seconds, the requests that arrive at the start of a spike wait even longer. Autoscaling protects the next requests, not the current ones.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;2. maxReplicas is a capacity limit, and it shows up in latency.&lt;/strong&gt; 8 users need 4 pods of 2 slots each. Max was 3, so 2 requests were always waiting, which is why p95 stayed at 24 s even after scale-up. In production that limit is your budget (&lt;code&gt;maxReplicas&lt;/code&gt;, Karpenter's &lt;code&gt;gpu_limit&lt;/code&gt;, your GPU quota). Set it from a load test, not by guessing.&lt;/p&gt;

&lt;h2&gt;
  
  
  A result I didn't expect: batching on CPU added almost no throughput
&lt;/h2&gt;

&lt;p&gt;With 1 user, the pod made 23.5 tokens/s. With 2 slots busy, it made &lt;strong&gt;25.4&lt;/strong&gt; tokens/s in total, and each user got &lt;strong&gt;11&lt;/strong&gt; instead of 22. On this CPU setup, a second slot mostly splits the same speed between two people.&lt;/p&gt;

&lt;p&gt;That is &lt;strong&gt;not&lt;/strong&gt; what vLLM does on a GPU. During decoding, a GPU spends most of its time reading model weights from memory, so batching more requests into the same weight read is close to free, up to a point. That is the reason to use vLLM on a GPU. But it also means &lt;strong&gt;my laptop numbers can't be scaled up to a GPU estimate&lt;/strong&gt;, so I won't do it. The scaling &lt;em&gt;logic&lt;/em&gt; carries over to EKS. The throughput numbers don't.&lt;/p&gt;

&lt;h2&gt;
  
  
  What the lab can't show
&lt;/h2&gt;

&lt;p&gt;Things that matter on EKS that I didn't measure:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Cold start.&lt;/strong&gt; Here a new replica was ready in 2 s. On EKS: Karpenter launches a GPU instance, the node joins, a multi-GB vLLM image is pulled, 16 GB of Qwen3 8B weights stream from S3, and the GPU warms up. That's minutes, not seconds, and it's the reason the production chart adds one pod every 2 minutes. I haven't measured it, so I won't give you a number.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Scale to zero.&lt;/strong&gt; With 0 replicas, vLLM exports no metrics, so a queue trigger can never fire. The chart has an optional &lt;code&gt;wakeQuery&lt;/code&gt; on gateway traffic for this. Not tested here.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;A setting in my own chart that does nothing.&lt;/strong&gt; KEDA warned on apply: &lt;em&gt;"CooldownPeriod is only relevant when minReplicaCount = 0 or idleReplicaCount is set."&lt;/em&gt; My EKS chart sets &lt;code&gt;cooldownSeconds: 900&lt;/code&gt; with &lt;code&gt;minReplicas: 1&lt;/code&gt;, so that setting is ignored. Scale-down speed comes from the HPA &lt;code&gt;behavior&lt;/code&gt; block. You only see this warning when you apply the manifest to a real cluster, even a local one, and &lt;code&gt;terraform validate&lt;/code&gt; will never show it.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Cost: a formula, not a benchmark
&lt;/h2&gt;

&lt;p&gt;I didn't measure GPU throughput, so here is the math to use once you have it. An NVIDIA L4 instance (&lt;code&gt;g6.xlarge&lt;/code&gt;, 1 GPU, 24 GB) is &lt;strong&gt;$0.8048/hour&lt;/strong&gt; on-demand in us-east-1 at the time of writing. Spot is often less than half that, but it changes by zone and by hour.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;cost per 1M output tokens = hourly price / (tokens per second × 3600) × 1,000,000
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Average output throughput across the hour (hypothetical)&lt;/th&gt;
&lt;th&gt;On-demand $/1M tokens&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;100 tokens/s&lt;/td&gt;
&lt;td&gt;$2.24&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;500 tokens/s&lt;/td&gt;
&lt;td&gt;$0.45&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;1,000 tokens/s&lt;/td&gt;
&lt;td&gt;$0.22&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;The key word is &lt;strong&gt;average&lt;/strong&gt;. A GPU's peak throughput doesn't matter if it's idle most of the hour. With &lt;code&gt;minReplicas: 1&lt;/code&gt;, one L4 running 24/7 costs about &lt;strong&gt;$588/month&lt;/strong&gt; on-demand, even if nobody sends a request. For a small team the real cost question is usually not "how fast is vLLM?" but "how many hours a day is the GPU busy?". That's why scale-to-zero and spot instances matter more than the last 10% of tokens per second.&lt;/p&gt;

&lt;h2&gt;
  
  
  Takeaways
&lt;/h2&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;Scale LLM servers on requests in flight per replica, not on waiting requests alone.&lt;/strong&gt; A waiting-only metric goes down when scaling works, and the autoscaler removes the capacity it just added.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Autoscaling doesn't help the requests already queued.&lt;/strong&gt; Their latency depends on how fast new capacity arrives, which on GPUs is minutes. Keep a warm minimum, or accept slow first requests in a spike.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;maxReplicas is a latency decision.&lt;/strong&gt; If it's too low, p95 stays bad no matter how good the scaling metric is.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Test the scaling logic locally.&lt;/strong&gt; It cost nothing, took an afternoon, and found two problems in a design that had already passed &lt;code&gt;terraform validate&lt;/code&gt; and CI.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Don't reuse laptop throughput numbers for GPU estimates.&lt;/strong&gt; Batching works differently on CPU and GPU.&lt;/li&gt;
&lt;/ol&gt;

&lt;h2&gt;
  
  
  Run it yourself
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;kind create cluster --config k8s-lab/kind.yaml   &lt;span class="c"&gt;# mounts k8s-lab/models into the node&lt;/span&gt;
helm --kube-context kind-ozai-lab upgrade -i prometheus prometheus-community/prometheus \
  -n monitoring --create-namespace -f k8s-lab/prometheus-values.yaml
helm --kube-context kind-ozai-lab upgrade -i keda kedacore/keda -n keda --create-namespace
kubectl --context kind-ozai-lab apply -f k8s-lab/inference.yaml -f k8s-lab/scaledobject-inflight.yaml
python3 k8s-lab/loadtest.py result.json 60:1,240:8,180:1   &lt;span class="c"&gt;# needs Prometheus port-forwarded to :9090&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Where this leaves the series
&lt;/h2&gt;

&lt;p&gt;This is the last planned part. Over four parts, OzAI went from one model on a laptop to a gateway with keys and limits, a dashboard that catches slow &lt;code&gt;200 OK&lt;/code&gt; responses, RAG that answers from documents, and an autoscaling design tested under load.&lt;/p&gt;

&lt;p&gt;The one thing still missing is real GPU numbers: cold start time, vLLM throughput on an L4, and real cost per million tokens. When I run it on EKS, it will get its own short post with only measured numbers.&lt;/p&gt;

&lt;p&gt;All code, manifests and raw results are in the repo: &lt;strong&gt;&lt;a href="https://github.com/ozy31/self-hosted-llm-platform" rel="noopener noreferrer"&gt;github.com/ozy31/self-hosted-llm-platform&lt;/a&gt;&lt;/strong&gt;&lt;/p&gt;

</description>
      <category>kubernetes</category>
      <category>llm</category>
      <category>devops</category>
      <category>ai</category>
    </item>
    <item>
      <title>RAG Took My Local LLM From 1/10 to 9/10. The Last Bugs Weren't the Model.</title>
      <dc:creator>Ahmet Ozan GAZİ</dc:creator>
      <pubDate>Tue, 10 Nov 2026 16:50:35 +0000</pubDate>
      <link>https://dev.to/ozy31/rag-took-my-local-llm-from-110-to-910-the-last-bugs-werent-the-model-1df6</link>
      <guid>https://dev.to/ozy31/rag-took-my-local-llm-from-110-to-910-the-last-bugs-werent-the-model-1df6</guid>
      <description>&lt;p&gt;&lt;em&gt;From DevOps to AI Platform Engineering — Part 3&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;In &lt;a href="https://dev.to/ozy31/i-built-a-self-hosted-chatgpt-on-my-laptop-heres-what-broke-2gci"&gt;part 1&lt;/a&gt; I built &lt;strong&gt;OzAI&lt;/strong&gt;, a self-hosted LLM platform on a MacBook Pro M1 Pro (16 GB): Ollama for the models, a LiteLLM gateway with API keys, and Open WebUI. In &lt;a href="https://dev.to/ozy31/your-llm-returns-200-ok-and-still-fails-how-i-monitor-a-self-hosted-model-16il"&gt;part 2&lt;/a&gt; I added a Grafana dashboard and load tested it.&lt;/p&gt;

&lt;p&gt;Part 1 ended with a problem I couldn't fix with infrastructure. I asked OzAI for the default &lt;code&gt;lagThreshold&lt;/code&gt; of KEDA's Kafka scaler. The answer is &lt;strong&gt;10&lt;/strong&gt;. OzAI said &lt;strong&gt;1000&lt;/strong&gt;, with total confidence.&lt;/p&gt;

&lt;p&gt;This part is about fixing that with &lt;strong&gt;RAG&lt;/strong&gt;, and about measuring the fix. The headline is in the title, but the more useful part is how I got the diagnosis of the last failures wrong, and what the right diagnosis was.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fr3tf7aojbx4hazfpajfh.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fr3tf7aojbx4hazfpajfh.png" alt="RAG pipeline: question, embedding, top chunks from 165 KEDA doc chunks, prompt with context, answer with citations; accuracy went from 1/10 to 6/10, 8/10 and 9/10" width="800" height="336"&gt;&lt;/a&gt;&lt;/p&gt;




&lt;h2&gt;
  
  
  RAG in one paragraph
&lt;/h2&gt;

&lt;p&gt;An 8B model squeezes a lot of the internet into 5 GB of weights. Details get blurry, and anything newer than its training data is simply missing. &lt;strong&gt;Retrieval-Augmented Generation&lt;/strong&gt; turns a closed-book exam into an open-book one: before the model answers, you search your own documents, put the most relevant passages into the prompt, and tell the model to answer &lt;em&gt;only&lt;/em&gt; from them and cite them. The model doesn't change. What changes is what's in front of it.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;question ──► embed ──► find closest chunks ──► [chunks + question] ──► LLM ──► answer with [1] citations
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  No framework, on purpose
&lt;/h2&gt;

&lt;p&gt;LangChain or LlamaIndex would do this in ten lines. I wanted to see every moving part, so &lt;code&gt;rag/ozai_rag.py&lt;/code&gt; uses only the Python standard library, about 160 lines:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Documents:&lt;/strong&gt; 10 pages of the KEDA 2.22 docs (concepts plus 7 scalers), about 17,000 words of Markdown.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Embeddings:&lt;/strong&gt; &lt;code&gt;nomic-embed-text&lt;/code&gt;, running in the same Ollama that serves the chat models.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Vector search:&lt;/strong&gt; cosine similarity in a Python loop. With 165 chunks of 768 numbers each, a loop is instant. A vector database starts to make sense at hundreds of thousands of chunks, not hundreds.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Generation:&lt;/strong&gt; the &lt;code&gt;ozai&lt;/code&gt; model (Qwen3 8B, thinking off) through the same LiteLLM gateway, with its own API key. So RAG traffic shows up on the part 2 dashboard like any other client.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Two details mattered more than I expected.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;1. Keep the heading in every chunk.&lt;/strong&gt; A chunk that says &lt;code&gt;default: 10&lt;/code&gt; is useless if you don't know which scaler it belongs to. Every chunk starts with its file and heading:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="n"&gt;chunks&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;append&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="s2"&gt;"source"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;f&lt;/span&gt;&lt;span class="s2"&gt;"{title} &amp;gt; {heading}"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="s2"&gt;"text"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;f&lt;/span&gt;&lt;span class="s2"&gt;"[{title} &amp;gt; {heading}] {piece}"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;   &lt;span class="c1"&gt;# e.g. [scalers_apache-kafka &amp;gt; Trigger Specification] ...&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;2. Embedding models can have instructions too.&lt;/strong&gt; &lt;code&gt;nomic-embed-text&lt;/code&gt; expects a prefix: &lt;code&gt;search_document:&lt;/code&gt; for the chunks you index and &lt;code&gt;search_query:&lt;/code&gt; for the question. The model's documentation says to use them. Without them, the vectors are still produced and nothing errors, so you'd never notice. (If you've read parts 1 and 2, you know this pattern by now.)&lt;/p&gt;




&lt;h2&gt;
  
  
  Eval first: 10 questions, strict scoring
&lt;/h2&gt;

&lt;p&gt;Before writing the retrieval code, I wrote the test. Ten questions about KEDA, each with the terms a correct answer &lt;em&gt;must&lt;/em&gt; contain:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight json"&gt;&lt;code&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="nl"&gt;"q"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;"What is the default lagThreshold of the KEDA Apache Kafka scaler?"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nl"&gt;"expect"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s2"&gt;"\\b10\\b"&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="nl"&gt;"q"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;"Which SASL modes does the KEDA Apache Kafka scaler support?"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
 &lt;span class="nl"&gt;"expect"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s2"&gt;"scram_sha256"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;"scram_sha512"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;"oauthbearer"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;"gssapi"&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;An answer passes only if every pattern matches. It's crude: it can't judge style, and a lucky number could pass. But it's objective and repeatable, and it turns "RAG feels better" into a number. Running it is one command: &lt;code&gt;python3 rag/ozai_rag.py eval rag/eval_keda.json&lt;/code&gt;. Each question is asked twice, without RAG and with RAG, at temperature 0.&lt;/p&gt;

&lt;h2&gt;
  
  
  Baseline: 1/10
&lt;/h2&gt;

&lt;p&gt;Without RAG, the model got one question right. The wrong answers weren't "I don't know." They were detailed, formatted, confident and invented:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Kafka &lt;code&gt;lagThreshold&lt;/code&gt; default: &lt;strong&gt;1000&lt;/strong&gt; (docs: 10).&lt;/li&gt;
&lt;li&gt;RabbitMQ &lt;code&gt;mode&lt;/code&gt; values: &lt;strong&gt;&lt;code&gt;pull&lt;/code&gt; (default)&lt;/strong&gt;, and more. That mode doesn't exist. The real ones are &lt;code&gt;QueueLength&lt;/code&gt;, &lt;code&gt;MessageRate&lt;/code&gt; and others.&lt;/li&gt;
&lt;li&gt;Cron scaler parameters: &lt;strong&gt;&lt;code&gt;schedule&lt;/code&gt; (required)&lt;/strong&gt;. The real parameters are &lt;code&gt;timezone&lt;/code&gt;, &lt;code&gt;start&lt;/code&gt;, &lt;code&gt;end&lt;/code&gt; and &lt;code&gt;desiredReplicas&lt;/code&gt;.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;This is the dangerous kind of wrong. It looks exactly like a right answer.&lt;/p&gt;

&lt;h2&gt;
  
  
  v1: 6/10
&lt;/h2&gt;

&lt;p&gt;The first RAG version used 300-word chunks and put the top 4 into the prompt. Accuracy went from 1 to 6.&lt;/p&gt;

&lt;p&gt;The behavior changed too. When the context didn't contain the answer, the model said so:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;The &lt;code&gt;lagThreshold&lt;/code&gt; in the KEDA Apache Kafka scaler is not explicitly mentioned in the provided context. Therefore, I don't know.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;For a platform, an honest "I don't know" is a huge improvement over an invented answer.&lt;/p&gt;

&lt;p&gt;The four failures had a common cause: the &lt;strong&gt;right page, but the wrong chunk&lt;/strong&gt;. The chunk with the actual parameter list wasn't in the top 4.&lt;/p&gt;

&lt;h2&gt;
  
  
  Smaller chunks: still 6/10
&lt;/h2&gt;

&lt;p&gt;The obvious fix was smaller chunks (150 words) and more of them (top 6). Retrieval looked different, but the score didn't move. I'm including this because it's tempting to skip the experiments that didn't work. This one told me the problem wasn't granularity.&lt;/p&gt;

&lt;h2&gt;
  
  
  Hybrid search: 8/10
&lt;/h2&gt;

&lt;p&gt;Look at the questions again: &lt;code&gt;lagThreshold&lt;/code&gt;, &lt;code&gt;mode&lt;/code&gt;, &lt;code&gt;protocol&lt;/code&gt;, &lt;code&gt;listLength&lt;/code&gt;. They contain &lt;strong&gt;exact identifiers&lt;/strong&gt;. Embeddings capture meaning well, but they're weak at "this exact token must appear." Classic keyword search is the opposite.&lt;/p&gt;

&lt;p&gt;So I added a keyword score: the share of the question's terms that appear in a chunk, weighted by how rare each term is (IDF, so &lt;code&gt;lagThreshold&lt;/code&gt; counts much more than &lt;code&gt;scaler&lt;/code&gt;). The final score is &lt;code&gt;cosine + 0.3 × keyword&lt;/code&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;keyword_scores&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;question&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;chunks&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="n"&gt;terms&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="n"&gt;t&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;lower&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;t&lt;/span&gt; &lt;span class="k"&gt;in&lt;/span&gt; &lt;span class="n"&gt;re&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;findall&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;r&lt;/span&gt;&lt;span class="s2"&gt;"[A-Za-z_][A-Za-z0-9_]{2,}"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;question&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="no"&gt;STOPWORDS&lt;/span&gt;
    &lt;span class="n"&gt;texts&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;c&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s2"&gt;"text"&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;lower&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;c&lt;/span&gt; &lt;span class="k"&gt;in&lt;/span&gt; &lt;span class="n"&gt;chunks&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
    &lt;span class="n"&gt;idf&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="n"&gt;t&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;math&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;len&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;texts&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;/&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="nb"&gt;sum&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;t&lt;/span&gt; &lt;span class="k"&gt;in&lt;/span&gt; &lt;span class="n"&gt;x&lt;/span&gt; &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;x&lt;/span&gt; &lt;span class="k"&gt;in&lt;/span&gt; &lt;span class="n"&gt;texts&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt; &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;t&lt;/span&gt; &lt;span class="k"&gt;in&lt;/span&gt; &lt;span class="n"&gt;terms&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="n"&gt;total&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;sum&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;idf&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;values&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="ow"&gt;or&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nb"&gt;sum&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;w&lt;/span&gt; &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;t&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;w&lt;/span&gt; &lt;span class="k"&gt;in&lt;/span&gt; &lt;span class="n"&gt;idf&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;items&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;t&lt;/span&gt; &lt;span class="k"&gt;in&lt;/span&gt; &lt;span class="n"&gt;x&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;/&lt;/span&gt; &lt;span class="n"&gt;total&lt;/span&gt; &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;x&lt;/span&gt; &lt;span class="k"&gt;in&lt;/span&gt; &lt;span class="n"&gt;texts&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;That gave &lt;strong&gt;8/10&lt;/strong&gt;. Search engines have combined these two signals for years. For technical documentation full of parameter names, it's not optional.&lt;/p&gt;




&lt;h2&gt;
  
  
  The last two failures: my wrong diagnosis
&lt;/h2&gt;

&lt;p&gt;Both remaining failures were RabbitMQ questions:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;em&gt;What are the possible values of &lt;code&gt;mode&lt;/code&gt;?&lt;/em&gt; The model listed 2 or 3 of the 5.&lt;/li&gt;
&lt;li&gt;
&lt;em&gt;Which protocols are accepted, and which is the default?&lt;/em&gt; The model said &lt;strong&gt;HTTP&lt;/strong&gt;. The docs say &lt;strong&gt;&lt;code&gt;auto&lt;/code&gt;&lt;/strong&gt;.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The model's own answer even explained itself: &lt;em&gt;"The default protocol is HTTP, as indicated by the examples in the context."&lt;/em&gt; I read that, decided the model had the facts and reasoned badly, and wrote it into my notes: &lt;strong&gt;"Remaining 2 failures are model reasoning, not retrieval."&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;If it's a reasoning problem, the fix is a model that reasons more. Qwen3 has a thinking mode, exposed on my gateway as &lt;code&gt;ozai-think&lt;/code&gt;, so I tried that on both questions:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight json"&gt;&lt;code&gt;&lt;span class="n"&gt;Model&lt;/span&gt;        &lt;span class="n"&gt;mode&lt;/span&gt; &lt;span class="n"&gt;question&lt;/span&gt;      &lt;span class="n"&gt;protocol&lt;/span&gt; &lt;span class="n"&gt;question&lt;/span&gt;
&lt;span class="n"&gt;ozai&lt;/span&gt;         &lt;span class="n"&gt;wrong&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;17&lt;/span&gt; &lt;span class="n"&gt;s&lt;/span&gt;        &lt;span class="n"&gt;wrong&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;21&lt;/span&gt; &lt;span class="n"&gt;s&lt;/span&gt;
&lt;span class="n"&gt;ozai&lt;/span&gt;&lt;span class="err"&gt;-&lt;/span&gt;&lt;span class="n"&gt;think&lt;/span&gt;   &lt;span class="n"&gt;wrong&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;27&lt;/span&gt; &lt;span class="n"&gt;s&lt;/span&gt;        &lt;span class="n"&gt;wrong&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;41&lt;/span&gt; &lt;span class="n"&gt;s&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Slower and still wrong. (One gotcha from part 1 came back here: my script capped answers at 400 tokens, and a thinking model can spend that whole budget thinking and return an empty answer. I made the cap configurable before running this test, or it wouldn't have meant anything.)&lt;/p&gt;

&lt;p&gt;Then I did what I should have done first: &lt;strong&gt;I printed the six chunks the model actually received.&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;1  scalers_rabbitmq-queue &amp;gt; AMQP protocol
2  scalers_rabbitmq-queue &amp;gt; HTTP protocol (with `QueueLength` trigger)
3  scalers_rabbitmq-queue &amp;gt; HTTP protocol (with `QueueLength` trigger)
4  scalers_rabbitmq-queue &amp;gt; HTTP protocol (with `ExpectedQueueConsumptionTime` trigger)
5  scalers_rabbitmq-queue &amp;gt; HTTP protocol (with `MessageRate` and `QueueLength` triggers)
6  scalers_rabbitmq-queue &amp;gt; AMQP protocol with username and password authorization
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Every one of them is a YAML &lt;strong&gt;example&lt;/strong&gt;. The section that actually lists the five modes and says &lt;code&gt;Default value is auto&lt;/code&gt;, &lt;em&gt;Trigger Specification&lt;/em&gt;, wasn't there. It ranked between &lt;strong&gt;14th and 30th&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;The model hadn't reasoned badly. It had been handed six examples, most of them using HTTP, and made a sensible guess. No amount of thinking can recover a fact that isn't in the context. &lt;strong&gt;The failures were retrieval failures all along.&lt;/strong&gt; My notes were wrong, and I corrected them in the repo.&lt;/p&gt;

&lt;h3&gt;
  
  
  Why the examples won
&lt;/h3&gt;

&lt;p&gt;The RabbitMQ page has &lt;strong&gt;eleven&lt;/strong&gt; configuration examples. Every one contains the words &lt;code&gt;mode&lt;/code&gt;, &lt;code&gt;protocol&lt;/code&gt; and &lt;code&gt;RabbitMQ&lt;/code&gt;, so every one scores well on both embeddings and keywords. They're also nearly identical to each other. Together they push the one paragraph with the answer out of the top 6.&lt;/p&gt;

&lt;h2&gt;
  
  
  MMR: 9/10
&lt;/h2&gt;

&lt;p&gt;The standard fix for "the top results are all the same thing" is &lt;strong&gt;Maximal Marginal Relevance (MMR)&lt;/strong&gt;. Instead of taking the top 6 by score, you pick them one at a time, and each pick is penalized for being similar to chunks you've already picked:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="n"&gt;pool&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;picked&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;scored&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="mi"&gt;5&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="n"&gt;k&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
&lt;span class="k"&gt;while&lt;/span&gt; &lt;span class="n"&gt;pool&lt;/span&gt; &lt;span class="ow"&gt;and&lt;/span&gt; &lt;span class="nb"&gt;len&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;picked&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="n"&gt;k&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="n"&gt;best&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;max&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;pool&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;key&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="k"&gt;lambda&lt;/span&gt; &lt;span class="n"&gt;x&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="no"&gt;MMR&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="n"&gt;x&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
               &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="no"&gt;MMR&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="nb"&gt;max&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;cosine&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;x&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s2"&gt;"vec"&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;p&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s2"&gt;"vec"&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;p&lt;/span&gt; &lt;span class="k"&gt;in&lt;/span&gt; &lt;span class="n"&gt;picked&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;default&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="n"&gt;picked&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;append&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;best&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="n"&gt;pool&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;remove&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;best&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;With &lt;code&gt;MMR = 0.7&lt;/code&gt; (70% relevance, 30% diversity), each near-copy of an example that's already picked gets pushed down a little, which is enough to make room for a chunk from a different section. I also tried simply taking more chunks (top 12, no MMR). That recovered only one of the two answers, with twice as much context.&lt;/p&gt;

&lt;p&gt;With MMR and the same budget of 6 chunks, the &lt;code&gt;mode&lt;/code&gt; question now gets all five values right, and none of the other nine questions got worse:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Config                                        Accuracy   Avg latency   Avg prompt tokens
No RAG (model memory)                           1/10       ~18-20 s            31
v1: 300-word chunks, top 4, embeddings          6/10        9.2 s            1264
v2a: 150-word chunks, top 6, embeddings         6/10        7.7 s               -
v2b: + hybrid keyword score                     8/10        9.9 s            1487
v3:  + MMR diversity (0.7)                      9/10       10.6 s            1413
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Notice the latency column. &lt;strong&gt;RAG answers were about twice as fast as no-RAG answers&lt;/strong&gt;, even with 45 times more prompt tokens. Without context, the model writes long, invented explanations. With context and an instruction to cite, it writes short answers. On hardware that serves one request at a time (part 2), shorter answers mean a shorter queue for everyone.&lt;/p&gt;

&lt;h2&gt;
  
  
  The one I didn't fix
&lt;/h2&gt;

&lt;p&gt;The &lt;code&gt;protocol&lt;/code&gt; question still fails. The &lt;em&gt;Trigger Specification&lt;/em&gt; section is split across three chunks, and MMR brings in one of them, but not the one that says &lt;code&gt;Default value is auto&lt;/code&gt;. I tried a few more settings (MMR from 0.5 to 0.7, top 6 to 8). None of them got it.&lt;/p&gt;

&lt;p&gt;So I stopped. With ten questions, every tweak that fixes one question is a tweak fitted to that question. Keep going and you're tuning the test, not the system. The honest score is 9/10, and the real fixes are structural:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;a &lt;strong&gt;reranker&lt;/strong&gt;: a second model that reads the top 30 candidates together with the question and reorders them,&lt;/li&gt;
&lt;li&gt;or &lt;strong&gt;structure-aware chunking&lt;/strong&gt;: one chunk per parameter in a parameter list, instead of fixed word counts.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;One more detail from that question: with MMR, &lt;code&gt;ozai-think&lt;/code&gt; still answered wrong, but this time it said &lt;em&gt;"the context does not explicitly state the default, so it is inferred."&lt;/em&gt; The thinking mode didn't make the model more correct, but it made it more honest about its uncertainty. That's worth something, though it isn't worth 80 seconds per answer.&lt;/p&gt;




&lt;h2&gt;
  
  
  Takeaways for DevOps engineers
&lt;/h2&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;Write the eval before the pipeline.&lt;/strong&gt; Ten questions with strict, automatic scoring turned every change into a number. Without it, I would have kept "smaller chunks" because it &lt;em&gt;felt&lt;/em&gt; better.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;When a RAG answer is wrong, print the retrieved chunks before blaming the model.&lt;/strong&gt; I skipped this step and wrote down a wrong conclusion. It's the RAG version of "check the logs before you restart the service."&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Exact identifiers need keyword search.&lt;/strong&gt; Technical docs are full of names like &lt;code&gt;lagThreshold&lt;/code&gt;. Embeddings alone miss them.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Near-duplicate content crowds out the answer.&lt;/strong&gt; Examples, changelogs and templates all look alike. Diversity in retrieval (MMR) matters as much as relevance.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Stop tuning before you overfit a small test set.&lt;/strong&gt; At some point the next fix is a better method, not a better parameter.&lt;/li&gt;
&lt;/ol&gt;

&lt;h2&gt;
  
  
  Next: part 4
&lt;/h2&gt;

&lt;p&gt;Everything so far runs on a laptop, serves one request at a time, and uses an 8B model. In &lt;a href="https://dev.to/ozy31/i-autoscaled-my-llm-on-queue-depth-it-scaled-down-under-full-load-5do5"&gt;part 4&lt;/a&gt; I autoscale the model server on queue depth with &lt;strong&gt;KEDA&lt;/strong&gt; (yes, the same KEDA from this article's test set) in a local Kubernetes cluster and load test it. The first design scaled down under full load. Part 4 shows why, and the one-line metric fix.&lt;/p&gt;

&lt;p&gt;All code, the eval set and every result file are in the repo: &lt;strong&gt;&lt;a href="https://github.com/ozy31/self-hosted-llm-platform" rel="noopener noreferrer"&gt;github.com/ozy31/self-hosted-llm-platform&lt;/a&gt;&lt;/strong&gt;&lt;/p&gt;

</description>
      <category>ai</category>
      <category>rag</category>
      <category>llm</category>
      <category>devops</category>
    </item>
    <item>
      <title>Your LLM Returns 200 OK and Still Fails. How I Monitor a Self-Hosted Model.</title>
      <dc:creator>Ahmet Ozan GAZİ</dc:creator>
      <pubDate>Fri, 06 Nov 2026 17:21:17 +0000</pubDate>
      <link>https://dev.to/ozy31/your-llm-returns-200-ok-and-still-fails-how-i-monitor-a-self-hosted-model-16il</link>
      <guid>https://dev.to/ozy31/your-llm-returns-200-ok-and-still-fails-how-i-monitor-a-self-hosted-model-16il</guid>
      <description>&lt;p&gt;&lt;em&gt;From DevOps to AI Platform Engineering — Part 2&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;In &lt;a href="https://dev.to/ozy31/i-built-a-self-hosted-chatgpt-on-my-laptop-heres-what-broke-2gci"&gt;part 1&lt;/a&gt; I built &lt;strong&gt;OzAI&lt;/strong&gt;, a self-hosted ChatGPT-style platform on a MacBook Pro M1 Pro with 16 GB of RAM:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Ollama runs open-weight models (Qwen3 8B, Qwen2.5-Coder 3B).&lt;/li&gt;
&lt;li&gt;A LiteLLM gateway handles API keys and rate limits.&lt;/li&gt;
&lt;li&gt;Open WebUI is the chat interface.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The biggest lesson from part 1 was uncomfortable: &lt;strong&gt;the worst failures returned HTTP 200.&lt;/strong&gt; Prompts were silently truncated, answers came back empty because the model spent its whole budget "thinking," and some answers were confidently wrong. None of these showed up as errors.&lt;/p&gt;

&lt;p&gt;So this part is about the question every DevOps engineer asks next: &lt;em&gt;how do I see what's going on?&lt;/em&gt; I'll cover which metrics matter for an LLM, how I built a dashboard without paying for an enterprise license, and what a load test told me about capacity.&lt;/p&gt;




&lt;h2&gt;
  
  
  Request rate and error rate are not enough
&lt;/h2&gt;

&lt;p&gt;For a normal web API, I'd start with the RED metrics: rate, errors, duration. For an LLM they're necessary but they miss most of the story. A single "duration" number hides two very different phases:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;Prefill.&lt;/strong&gt; The model reads the whole prompt. This determines &lt;strong&gt;time to first token (TTFT)&lt;/strong&gt;, how long the user stares at an empty screen.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Decode.&lt;/strong&gt; The model generates the answer one token at a time. This determines &lt;strong&gt;tokens per second&lt;/strong&gt;, how fast the text streams.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;A 30-second request can be fine (a long answer streaming at full speed) or terrible (29 seconds in a queue, then a short answer). You can only tell them apart if you measure the phases separately.&lt;/p&gt;

&lt;p&gt;These are the metrics I settled on:&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Metric&lt;/th&gt;
&lt;th&gt;What it tells you&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;TTFT p50 / p95&lt;/td&gt;
&lt;td&gt;Queueing and prompt size. The number users &lt;em&gt;feel&lt;/em&gt;.&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Decode tokens/s per request&lt;/td&gt;
&lt;td&gt;Is the hardware healthy? Is it shared?&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;End-to-end latency p50 / p95&lt;/td&gt;
&lt;td&gt;Total cost of a request&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Prompt and completion tokens&lt;/td&gt;
&lt;td&gt;Truncation (see part 1), cost, context usage&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Requests per API key&lt;/td&gt;
&lt;td&gt;Who is using the platform&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;Token counts are in that list on purpose. In part 1, the only clue to a serious bug was that every request had &lt;strong&gt;exactly 2,050 prompt tokens&lt;/strong&gt;. A flat line on a token graph is an alert waiting to be written.&lt;/p&gt;




&lt;h2&gt;
  
  
  Plan A: Prometheus. It didn't work out.
&lt;/h2&gt;

&lt;p&gt;My DevOps reflex was Prometheus. LiteLLM has a Prometheus integration, so I enabled it:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight yaml"&gt;&lt;code&gt;&lt;span class="na"&gt;litellm_settings&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
  &lt;span class="na"&gt;callbacks&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="pi"&gt;[&lt;/span&gt;&lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="s"&gt;prometheus"&lt;/span&gt;&lt;span class="pi"&gt;]&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The &lt;code&gt;/metrics&lt;/code&gt; endpoint came up, but there were no &lt;code&gt;litellm_*&lt;/code&gt; metrics in it. It turns out the Prometheus integration is an &lt;strong&gt;Enterprise feature&lt;/strong&gt; in LiteLLM. The open-source image accepts the setting and quietly does nothing, which fits the theme of this series.&lt;/p&gt;

&lt;h2&gt;
  
  
  Plan B: the gateway already logs every request
&lt;/h2&gt;

&lt;p&gt;LiteLLM writes a row to Postgres for every request, in a table called &lt;code&gt;LiteLLM_SpendLogs&lt;/code&gt;. It's meant for cost tracking, but it has everything I need:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;startTime&lt;/code&gt;: the request arrived&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;completionStartTime&lt;/code&gt;: the first token was sent&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;endTime&lt;/code&gt;: the last token was sent&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;prompt_tokens&lt;/code&gt;, &lt;code&gt;completion_tokens&lt;/code&gt;, &lt;code&gt;model_group&lt;/code&gt;, &lt;code&gt;status&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;the API key's alias, inside a JSON &lt;code&gt;metadata&lt;/code&gt; column&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;From these three timestamps, the important metrics are just subtraction:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;TTFT          = completionStartTime - startTime
decode tok/s  = completion_tokens / (endTime - completionStartTime)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;So the plan became &lt;strong&gt;Grafana reading the request log directly with SQL&lt;/strong&gt;.&lt;/p&gt;

&lt;h3&gt;
  
  
  A read-only role first
&lt;/h3&gt;

&lt;p&gt;Grafana shouldn't be able to touch the gateway's database. It gets a role that can read one table and nothing else:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="k"&gt;CREATE&lt;/span&gt; &lt;span class="k"&gt;ROLE&lt;/span&gt; &lt;span class="n"&gt;grafana_ro&lt;/span&gt; &lt;span class="n"&gt;LOGIN&lt;/span&gt; &lt;span class="n"&gt;PASSWORD&lt;/span&gt; &lt;span class="s1"&gt;'...'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;GRANT&lt;/span&gt; &lt;span class="k"&gt;CONNECT&lt;/span&gt; &lt;span class="k"&gt;ON&lt;/span&gt; &lt;span class="k"&gt;DATABASE&lt;/span&gt; &lt;span class="n"&gt;litellm&lt;/span&gt; &lt;span class="k"&gt;TO&lt;/span&gt; &lt;span class="n"&gt;grafana_ro&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;GRANT&lt;/span&gt; &lt;span class="k"&gt;USAGE&lt;/span&gt; &lt;span class="k"&gt;ON&lt;/span&gt; &lt;span class="k"&gt;SCHEMA&lt;/span&gt; &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="k"&gt;TO&lt;/span&gt; &lt;span class="n"&gt;grafana_ro&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;GRANT&lt;/span&gt; &lt;span class="k"&gt;SELECT&lt;/span&gt; &lt;span class="k"&gt;ON&lt;/span&gt; &lt;span class="nv"&gt;"LiteLLM_SpendLogs"&lt;/span&gt; &lt;span class="k"&gt;TO&lt;/span&gt; &lt;span class="n"&gt;grafana_ro&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The keys table, the users table and the budgets are invisible to Grafana. If the dashboard is ever compromised, the blast radius is a list of timestamps and token counts.&lt;/p&gt;

&lt;h3&gt;
  
  
  TTFT percentiles in SQL
&lt;/h3&gt;

&lt;p&gt;Postgres has &lt;code&gt;percentile_cont&lt;/code&gt;, so percentiles are exact, not estimated from histogram buckets:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="k"&gt;SELECT&lt;/span&gt;
  &lt;span class="err"&gt;$&lt;/span&gt;&lt;span class="n"&gt;__timeGroupAlias&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;"startTime"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="err"&gt;$&lt;/span&gt;&lt;span class="n"&gt;__interval&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt;
  &lt;span class="n"&gt;percentile_cont&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="mi"&gt;5&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;  &lt;span class="n"&gt;WITHIN&lt;/span&gt; &lt;span class="k"&gt;GROUP&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;ORDER&lt;/span&gt; &lt;span class="k"&gt;BY&lt;/span&gt; &lt;span class="k"&gt;extract&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;epoch&lt;/span&gt; &lt;span class="k"&gt;FROM&lt;/span&gt; &lt;span class="nv"&gt;"completionStartTime"&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="nv"&gt;"startTime"&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt; &lt;span class="k"&gt;AS&lt;/span&gt; &lt;span class="n"&gt;p50&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="n"&gt;percentile_cont&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="mi"&gt;95&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="n"&gt;WITHIN&lt;/span&gt; &lt;span class="k"&gt;GROUP&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;ORDER&lt;/span&gt; &lt;span class="k"&gt;BY&lt;/span&gt; &lt;span class="k"&gt;extract&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;epoch&lt;/span&gt; &lt;span class="k"&gt;FROM&lt;/span&gt; &lt;span class="nv"&gt;"completionStartTime"&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="nv"&gt;"startTime"&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt; &lt;span class="k"&gt;AS&lt;/span&gt; &lt;span class="n"&gt;p95&lt;/span&gt;
&lt;span class="k"&gt;FROM&lt;/span&gt; &lt;span class="nv"&gt;"LiteLLM_SpendLogs"&lt;/span&gt;
&lt;span class="k"&gt;WHERE&lt;/span&gt; &lt;span class="err"&gt;$&lt;/span&gt;&lt;span class="n"&gt;__timeFilter&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;"startTime"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;AND&lt;/span&gt; &lt;span class="n"&gt;status&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s1"&gt;'success'&lt;/span&gt;
  &lt;span class="k"&gt;AND&lt;/span&gt; &lt;span class="nv"&gt;"endTime"&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="nv"&gt;"completionStartTime"&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;interval&lt;/span&gt; &lt;span class="s1"&gt;'100 milliseconds'&lt;/span&gt;   &lt;span class="c1"&gt;-- streamed requests only, see below&lt;/span&gt;
&lt;span class="k"&gt;GROUP&lt;/span&gt; &lt;span class="k"&gt;BY&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt; &lt;span class="k"&gt;ORDER&lt;/span&gt; &lt;span class="k"&gt;BY&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Decode speed works the same way. I also skip very short answers, because dividing a handful of tokens by a few milliseconds produces nonsense:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="k"&gt;SELECT&lt;/span&gt;
  &lt;span class="err"&gt;$&lt;/span&gt;&lt;span class="n"&gt;__timeGroupAlias&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;"startTime"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="err"&gt;$&lt;/span&gt;&lt;span class="n"&gt;__interval&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt;
  &lt;span class="k"&gt;avg&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;completion_tokens&lt;/span&gt; &lt;span class="o"&gt;/&lt;/span&gt; &lt;span class="k"&gt;nullif&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;extract&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;epoch&lt;/span&gt; &lt;span class="k"&gt;FROM&lt;/span&gt; &lt;span class="nv"&gt;"endTime"&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="nv"&gt;"completionStartTime"&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt; &lt;span class="k"&gt;AS&lt;/span&gt; &lt;span class="n"&gt;tok_s&lt;/span&gt;
&lt;span class="k"&gt;FROM&lt;/span&gt; &lt;span class="nv"&gt;"LiteLLM_SpendLogs"&lt;/span&gt;
&lt;span class="k"&gt;WHERE&lt;/span&gt; &lt;span class="err"&gt;$&lt;/span&gt;&lt;span class="n"&gt;__timeFilter&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;"startTime"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;AND&lt;/span&gt; &lt;span class="n"&gt;status&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s1"&gt;'success'&lt;/span&gt; &lt;span class="k"&gt;AND&lt;/span&gt; &lt;span class="n"&gt;completion_tokens&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="mi"&gt;10&lt;/span&gt;
  &lt;span class="k"&gt;AND&lt;/span&gt; &lt;span class="nv"&gt;"endTime"&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="nv"&gt;"completionStartTime"&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;interval&lt;/span&gt; &lt;span class="s1"&gt;'100 milliseconds'&lt;/span&gt;
&lt;span class="k"&gt;GROUP&lt;/span&gt; &lt;span class="k"&gt;BY&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt; &lt;span class="k"&gt;ORDER&lt;/span&gt; &lt;span class="k"&gt;BY&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Requests per API key come from the JSON column: &lt;code&gt;metadata-&amp;gt;&amp;gt;'user_api_key_alias'&lt;/code&gt;.&lt;/p&gt;

&lt;h3&gt;
  
  
  Dashboards as code
&lt;/h3&gt;

&lt;p&gt;Nothing is clicked together in the UI. The datasource and the dashboard are provisioned from files in the repo, so &lt;code&gt;docker compose up&lt;/code&gt; produces the same dashboard every time:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;grafana/
  provisioning/datasources/litellm.yaml   # Postgres, read-only user
  provisioning/dashboards/ozai.yaml       # "load dashboards from this folder"
  dashboards/ozai-gateway.json            # the dashboard itself
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;One gotcha cost me a restart loop. I first wrote the datasource in compact YAML flow style, with the password as &lt;code&gt;$__env{GRAFANA_DB_PASSWORD}&lt;/code&gt; inside &lt;code&gt;{ }&lt;/code&gt;. The braces collide with YAML's own syntax and provisioning fails to parse. Block-style YAML with a plain &lt;code&gt;$GRAFANA_DB_PASSWORD&lt;/code&gt; works:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight yaml"&gt;&lt;code&gt;&lt;span class="na"&gt;apiVersion&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="m"&gt;1&lt;/span&gt;
&lt;span class="na"&gt;datasources&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
  &lt;span class="pi"&gt;-&lt;/span&gt; &lt;span class="na"&gt;name&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;LiteLLM&lt;/span&gt;
    &lt;span class="na"&gt;uid&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;litellm-pg&lt;/span&gt;
    &lt;span class="na"&gt;type&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;grafana-postgresql-datasource&lt;/span&gt;
    &lt;span class="na"&gt;url&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;postgres:5432&lt;/span&gt;
    &lt;span class="na"&gt;user&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;grafana_ro&lt;/span&gt;
    &lt;span class="na"&gt;jsonData&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
      &lt;span class="na"&gt;database&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;litellm&lt;/span&gt;
      &lt;span class="na"&gt;sslmode&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;disable&lt;/span&gt;
    &lt;span class="na"&gt;secureJsonData&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
      &lt;span class="na"&gt;password&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;$GRAFANA_DB_PASSWORD&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Faexiekf999jgnb4wdlzd.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Faexiekf999jgnb4wdlzd.png" alt="The OzAI Grafana dashboard during two load test runs: requests by API key, TTFT p50/p95, latency p50/p95, decode tokens per second and tokens by model" width="800" height="432"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  The dashboard had a silent bug too
&lt;/h3&gt;

&lt;p&gt;The first version of the decode speed panel showed a peak of &lt;strong&gt;13,000 tokens per second&lt;/strong&gt;, on a laptop that tops out around 22.&lt;/p&gt;

&lt;p&gt;The cause was a second API key that sent normal, non-streaming requests. A non-streaming answer reaches the client in one piece, so the gateway logs &lt;code&gt;completionStartTime&lt;/code&gt; and &lt;code&gt;endTime&lt;/code&gt; almost at the same moment:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;key          TTFT      decode time   completion tokens
loadtest     6.37 s    6.38 s        128      &amp;lt;- streamed
demo-friend  5.24 s    0.000 s       100      &amp;lt;- not streamed
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;100 tokens divided by about 0 seconds is a huge number, and one such row ruins the average. These requests also mess up TTFT in a quieter way. Their "time to first token" is really the time for the &lt;em&gt;whole&lt;/em&gt; answer, so they push the TTFT percentiles up.&lt;/p&gt;

&lt;p&gt;The fix is the extra &lt;code&gt;WHERE&lt;/code&gt; line in both queries above: TTFT and decode speed only count streamed requests, where the two timestamps are meaningfully apart. Request counts, latency and tokens still include everything.&lt;/p&gt;

&lt;p&gt;It's the same lesson as part 1, one layer up. Nothing errored. The panel just showed a confident, wrong number. &lt;strong&gt;Check your metric definitions against raw rows before you trust a dashboard.&lt;/strong&gt;&lt;/p&gt;




&lt;h2&gt;
  
  
  Load testing an LLM
&lt;/h2&gt;

&lt;p&gt;Generic HTTP load tools like &lt;code&gt;ab&lt;/code&gt; or &lt;code&gt;hey&lt;/code&gt; measure the whole response. To get TTFT you have to &lt;strong&gt;stream&lt;/strong&gt; and record the moment the first content token arrives. I wrote a small load generator in plain Python (threads plus &lt;code&gt;urllib&lt;/code&gt;, no dependencies). It's about 80 lines and lives in the repo as &lt;code&gt;loadtest/bench.py&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Each simulated user sends the same prompt ("Explain how Kubernetes Horizontal Pod Autoscaler works.") with &lt;code&gt;max_tokens: 128&lt;/code&gt; through the gateway, using its own API key. I ran 1, 2, 4 and 8 concurrent users.&lt;/p&gt;

&lt;h3&gt;
  
  
  My first result was wrong
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Users   TTFT p50   TTFT max   Total tok/s
1         0.5 s      0.5 s       18.4
2        57.7 s     60.8 s        3.8     &amp;lt;- ???
4        16.1 s     25.3 s       16.3
8        21.7 s     43.1 s       20.9
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Two users took longer than eight. The cause was me: &lt;strong&gt;I was chatting in Open WebUI while the test ran&lt;/strong&gt;, and as part 1 showed, Open WebUI sends extra background requests (title, tags, follow-ups) after every answer. My own chat was a noisy neighbor on a GPU that can only do one thing at a time. I only figured that out by digging through the gateway's request log by hand.&lt;/p&gt;

&lt;p&gt;That's a benchmarking lesson I already knew from web systems, and I still ignored it: &lt;strong&gt;benchmark on an idle system, and make it easy to see who else is using it.&lt;/strong&gt; This run is the reason the dashboard has a "requests by API key" panel. With it, the same contamination is visible at a glance: the load-test key and the Open WebUI key show up as separate lines.&lt;/p&gt;

&lt;h3&gt;
  
  
  The clean run
&lt;/h3&gt;

&lt;p&gt;On an idle machine:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Users   TTFT p50   TTFT max   Latency max   Per-user tok/s   Total tok/s
1         0.3 s      0.3 s        6.6 s          19.5            19.5
2         3.4 s      6.7 s       13.0 s          14.5            19.7
4         9.3 s     18.6 s       24.6 s           8.7            20.8
8        21.3 s     43.3 s       49.1 s           4.7            20.7
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;I ran it again four days later, right after starting the stack, and got almost the same numbers, with one difference:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Users   TTFT p50   TTFT max   Total tok/s
1         7.1 s      7.1 s        9.7    &amp;lt;- cold start
2         3.2 s      6.3 s       20.9
4         9.4 s     18.6 s       20.8
8        22.2 s     44.3 s       20.4
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  What the numbers say
&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;1. Total throughput is a constant, about 20 tokens per second.&lt;/strong&gt; One user, two users, eight users: the hardware produces the same amount of text. More users get thinner slices of it. Decoding is limited by memory bandwidth. Every token requires reading the model's weights from memory, and the GPU is already doing that as fast as it can for one request.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;2. TTFT is a queue.&lt;/strong&gt; The worst TTFT is roughly &lt;em&gt;(your position in the queue) × (time for one answer)&lt;/em&gt;. One 128-token answer takes about 6.5 seconds, so the 8th user waits about 7 × 6.5 ≈ 45 seconds. The measured value was 43–44 seconds. In practice, requests are served one at a time.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;3. Cold starts are real.&lt;/strong&gt; In the second run, the first request took 7 seconds to produce a token instead of 0.3. Ollama had just started and had to load 5 GB of weights into memory first. Ollama also unloads an idle model after 5 minutes by default (&lt;code&gt;keep_alive&lt;/code&gt;), so on a quiet platform &lt;em&gt;the first user after a break&lt;/em&gt; always pays this price. On a dashboard it shows up as a lone TTFT spike after an idle period. Now I know it isn't an incident.&lt;/p&gt;

&lt;h3&gt;
  
  
  Capacity, in DevOps terms
&lt;/h3&gt;

&lt;p&gt;Say the SLO is "TTFT p95 under 5 seconds" for answers of about 128 tokens. With requests served one at a time, this laptop meets that SLO at &lt;strong&gt;one concurrent request&lt;/strong&gt;. Two concurrent users already push the second one past 6 seconds.&lt;/p&gt;

&lt;p&gt;That doesn't mean only one person can use it. Real users read, type and think between messages, so a handful of people can share it comfortably. But it does mean this setup can't handle a burst, and adding CPU, RAM or replicas on the same machine won't change that. The fix is a different serving engine (continuous batching in vLLM) on different hardware, which is part 3.&lt;/p&gt;




&lt;h2&gt;
  
  
  What this setup can't see
&lt;/h2&gt;

&lt;p&gt;SQL over request logs gives exact per-request numbers, but it only sees what the gateway sees. It has some blind spots:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;No queue depth.&lt;/strong&gt; I have to infer queueing from TTFT. Ollama doesn't expose how many requests are waiting.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;No GPU metrics.&lt;/strong&gt; No utilization, no memory, no temperature.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;No KV cache usage.&lt;/strong&gt; That's the resource that actually limits concurrency on a real inference server.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;No alerting yet.&lt;/strong&gt; The dashboards are for looking, not paging anyone.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;For production, I'd keep the gateway log for per-key usage and add the serving engine's own metrics. vLLM exposes Prometheus metrics natively, including &lt;code&gt;vllm:num_requests_waiting&lt;/code&gt; and KV cache usage. NVIDIA's DCGM exporter covers the GPU. Those are what I'll use on Kubernetes in part 3, and queue depth is also exactly the signal to autoscale on.&lt;/p&gt;

&lt;p&gt;The first alerts I'd write, based on what actually went wrong in this project:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;TTFT p95 above the SLO for 5 minutes (queueing or cold starts).&lt;/li&gt;
&lt;li&gt;Error rate above zero for any key (auth, rate limits, budget misconfiguration, like the $0 budget from part 1).&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Prompt tokens stuck at the same value across many requests.&lt;/strong&gt; It looks strange, but it's the alert that would have caught the silent truncation bug on day one.&lt;/li&gt;
&lt;/ol&gt;




&lt;h2&gt;
  
  
  Takeaways for DevOps engineers
&lt;/h2&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;Split latency into TTFT and decode speed.&lt;/strong&gt; One "duration" number hides whether users are waiting in a queue or reading a long answer.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Your gateway's request log is a metrics source.&lt;/strong&gt; If Prometheus isn't available, a read-only SQL datasource gives you exact percentiles in an afternoon.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Tag traffic by API key.&lt;/strong&gt; It turned a confusing benchmark into a one-glance diagnosis.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;An LLM server's throughput is fixed by the hardware.&lt;/strong&gt; Load tests on a single GPU don't find a breaking point. They find a queue, and they tell you how long it gets.&lt;/li&gt;
&lt;/ol&gt;

&lt;h2&gt;
  
  
  Next: part 3
&lt;/h2&gt;

&lt;p&gt;Everything so far runs on a laptop and serves requests one at a time. In part 3 I move the platform to &lt;strong&gt;Amazon EKS with GPU nodes&lt;/strong&gt;, swap Ollama for &lt;strong&gt;vLLM&lt;/strong&gt; with continuous batching, autoscale on queue depth with &lt;strong&gt;KEDA&lt;/strong&gt;, and run the same load test to see whether 8 users finally get more than 20 tokens per second. I'll also compare the cost with paying per token.&lt;/p&gt;

&lt;p&gt;All configs, the dashboard JSON, the load test and raw benchmark results are in the repo: &lt;strong&gt;&lt;a href="https://github.com/ozy31/self-hosted-llm-platform" rel="noopener noreferrer"&gt;github.com/ozy31/self-hosted-llm-platform&lt;/a&gt;&lt;/strong&gt;&lt;/p&gt;

</description>
      <category>ai</category>
      <category>devops</category>
      <category>llm</category>
      <category>observability</category>
    </item>
    <item>
      <title>I Built a Self-Hosted ChatGPT on My Laptop. Here's What Broke.</title>
      <dc:creator>Ahmet Ozan GAZİ</dc:creator>
      <pubDate>Mon, 02 Nov 2026 07:57:12 +0000</pubDate>
      <link>https://dev.to/ozy31/i-built-a-self-hosted-chatgpt-on-my-laptop-heres-what-broke-2gci</link>
      <guid>https://dev.to/ozy31/i-built-a-self-hosted-chatgpt-on-my-laptop-heres-what-broke-2gci</guid>
      <description>&lt;p&gt;&lt;em&gt;From DevOps to AI Platform Engineering — Part 1&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;I'm a DevOps engineer. I've spent years running Kubernetes clusters, writing Terraform and keeping production alive. Lately, every job interview has a new question in it: &lt;em&gt;"What have you done with AI?"&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;Calling the OpenAI API from a script didn't feel like a good answer. So I set myself a different goal: &lt;strong&gt;run an open-weight LLM myself and serve it to other people like a real platform&lt;/strong&gt;, with API keys, rate limits, usage tracking and numbers I measured myself.&lt;/p&gt;

&lt;p&gt;This is part 1 of a 3-part series. Everything here runs on one laptop, a MacBook Pro M1 Pro with 16 GB of RAM. Part 2 covers observability and load testing. Part 3 moves the same platform to GPUs on Amazon EKS.&lt;/p&gt;

&lt;p&gt;I call the platform &lt;strong&gt;OzAI&lt;/strong&gt;. To be clear from the start: OzAI is &lt;em&gt;not&lt;/em&gt; a model I trained. It's a self-hosted platform built on open-weight models (Qwen3 8B and Qwen2.5-Coder 3B). The engineering is in serving them.&lt;/p&gt;




&lt;h2&gt;
  
  
  The architecture
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fnkhborf5dgf9oecqowyo.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fnkhborf5dgf9oecqowyo.png" alt="OzAI architecture: browser, VS Code and scripts talk to a LiteLLM gateway in Docker Compose, which calls Ollama running natively on the macOS host" width="799" height="453"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Ollama&lt;/strong&gt; runs the models. It runs natively on macOS, not in Docker (more on that below).&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;LiteLLM&lt;/strong&gt; is the gateway: an OpenAI-compatible API with per-user virtual keys, model allow-lists, rate limits and a request log in Postgres.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Open WebUI&lt;/strong&gt; is a ChatGPT-style UI that talks only to the gateway.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;SearXNG&lt;/strong&gt; is a self-hosted search engine, so the model can answer questions about recent events.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Grafana&lt;/strong&gt; shows dashboards built on the gateway's request log (part 2).&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Everything except Ollama runs in Docker Compose and binds to &lt;code&gt;127.0.0.1&lt;/code&gt; only. Nothing is exposed to the internet yet.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Decision #1: Ollama runs on the host, not in a container.&lt;/strong&gt; Docker on macOS runs containers inside a Linux VM with no access to the Apple GPU. Ollama in a container would run on the CPU only. On the host it uses Metal, and the containers reach it at &lt;code&gt;host.docker.internal:11434&lt;/code&gt;. It looks less tidy in a compose file, but it's the difference between a usable system and an unusable one.&lt;/p&gt;




&lt;h2&gt;
  
  
  Step 1: How fast is a laptop, really?
&lt;/h2&gt;

&lt;p&gt;Before building anything, I measured. Here is one request per model, 200 max tokens, no concurrency:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Model                    Disk     Cold load   Prompt eval   Generation
qwen2.5-coder:3b (Q4)    1.9 GB   2.1 s       120 tok/s     53.3 tok/s
qwen3:8b (Q4_K_M)        5.2 GB   3.9 s        87 tok/s     22.4 tok/s
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Two things surprised me:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;The 8B model is less than half as fast as the 3B one.&lt;/strong&gt; Generating tokens is limited by memory bandwidth, not compute. For every token, the model's weights have to be read from memory again. A model 2.7× bigger is roughly 2.4× slower.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;22 tokens per second is fine for one person.&lt;/strong&gt; That's faster than most people read. The real question is what happens with more than one user, which comes later.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Quantization (&lt;code&gt;Q4_K_M&lt;/code&gt; = roughly 4 bits per weight) is what makes an 8B model fit in 5.2 GB at all. Without it, the same model needs around 16 GB just for its weights, which is all the RAM in this laptop.&lt;/p&gt;




&lt;h2&gt;
  
  
  Step 2: A gateway, not just a model
&lt;/h2&gt;

&lt;p&gt;Running a model is easy. Letting &lt;em&gt;other people&lt;/em&gt; use it safely is the platform engineering part, and that's where LiteLLM comes in.&lt;/p&gt;

&lt;p&gt;The model list maps friendly names to real models:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight yaml"&gt;&lt;code&gt;&lt;span class="na"&gt;model_list&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
  &lt;span class="pi"&gt;-&lt;/span&gt; &lt;span class="na"&gt;model_name&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;ozai&lt;/span&gt;            &lt;span class="c1"&gt;# fast chat&lt;/span&gt;
    &lt;span class="na"&gt;litellm_params&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
      &lt;span class="na"&gt;model&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;ollama_chat/qwen3:8b&lt;/span&gt;
      &lt;span class="na"&gt;api_base&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;http://host.docker.internal:11434&lt;/span&gt;
      &lt;span class="na"&gt;reasoning_effort&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;none&lt;/span&gt;
      &lt;span class="na"&gt;num_ctx&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="m"&gt;8192&lt;/span&gt;
  &lt;span class="pi"&gt;-&lt;/span&gt; &lt;span class="na"&gt;model_name&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;ozai-think&lt;/span&gt;      &lt;span class="c1"&gt;# same model, thinking on&lt;/span&gt;
    &lt;span class="na"&gt;litellm_params&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
      &lt;span class="na"&gt;model&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;ollama_chat/qwen3:8b&lt;/span&gt;
      &lt;span class="na"&gt;api_base&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;http://host.docker.internal:11434&lt;/span&gt;
      &lt;span class="na"&gt;num_ctx&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="m"&gt;8192&lt;/span&gt;
  &lt;span class="pi"&gt;-&lt;/span&gt; &lt;span class="na"&gt;model_name&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;ozai-coder&lt;/span&gt;
    &lt;span class="na"&gt;litellm_params&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
      &lt;span class="na"&gt;model&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;ollama_chat/qwen2.5-coder:3b&lt;/span&gt;
      &lt;span class="na"&gt;api_base&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;http://host.docker.internal:11434&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Then every client gets its own virtual key with its own limits:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;curl localhost:4000/key/generate &lt;span class="se"&gt;\&lt;/span&gt;
  &lt;span class="nt"&gt;-H&lt;/span&gt; &lt;span class="s2"&gt;"Authorization: Bearer &lt;/span&gt;&lt;span class="nv"&gt;$LITELLM_MASTER_KEY&lt;/span&gt;&lt;span class="s2"&gt;"&lt;/span&gt; &lt;span class="se"&gt;\&lt;/span&gt;
  &lt;span class="nt"&gt;-d&lt;/span&gt; &lt;span class="s1"&gt;'{"key_alias":"demo-friend","models":["ozai"],"rpm_limit":10}'&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;I tested it the way I'd test any API gateway:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;No key                              -&amp;gt; 401
Friend key asking for ozai-coder    -&amp;gt; 403 (not on its allow-list)
12 requests in a minute, limit 10   -&amp;gt; 9 x 200, then 429
                                       (one earlier request counted in the same window)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Any tool that speaks the OpenAI API can now use OzAI by changing two settings: the base URL and the key. That turned out to matter a lot for the IDE integration below.&lt;/p&gt;




&lt;h2&gt;
  
  
  What broke (the useful part)
&lt;/h2&gt;

&lt;p&gt;Nothing below threw an error. Every one of these problems showed up as "it works, but badly."&lt;/p&gt;

&lt;h3&gt;
  
  
  1. A "thinking" model made every answer take two minutes
&lt;/h3&gt;

&lt;p&gt;Qwen3 is a reasoning model. Before it answers, it writes a hidden chain of thought. One simple question produced over 600 streamed reasoning chunks before the first word of the actual answer. The full response took about two minutes, partly because it was queued behind another request on the same GPU.&lt;/p&gt;

&lt;p&gt;There was a second, quieter problem. With a small &lt;code&gt;max_tokens&lt;/code&gt;, the model spent the whole budget on thinking and &lt;strong&gt;returned an empty answer&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Fix:&lt;/strong&gt; two aliases for the same weights. &lt;code&gt;ozai&lt;/code&gt; has thinking turned off (&lt;code&gt;reasoning_effort: none&lt;/code&gt;) and is the default. &lt;code&gt;ozai-think&lt;/code&gt; is there for hard questions. Users choose speed or depth by picking a model name. Nobody has to know about prompt flags.&lt;/p&gt;

&lt;h3&gt;
  
  
  2. A $0 budget blocked everything
&lt;/h3&gt;

&lt;p&gt;I gave a test key &lt;code&gt;max_budget: 0&lt;/code&gt;, assuming that meant "no spending allowed." Local models cost $0, so I expected every request to pass. Instead, every request was rejected:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Budget has been exceeded! Current cost: 0.0, Max budget: 0.0
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The check is &lt;code&gt;spend &amp;gt;= budget&lt;/code&gt;, and 0 ≥ 0. For self-hosted models, either leave the budget unset or set a custom per-token price so the budget means something.&lt;/p&gt;

&lt;h3&gt;
  
  
  3. The UI was quietly competing with its own users
&lt;/h3&gt;

&lt;p&gt;The chat UI felt slow even with a single user. The gateway log explained why. After every answer, Open WebUI sends &lt;em&gt;extra&lt;/em&gt; requests to the same model: one to generate a chat title, one for tags, one for follow-up suggestions.&lt;/p&gt;

&lt;p&gt;On one GPU, those requests queue in front of the user's next question.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Fix:&lt;/strong&gt; In Open WebUI's admin settings, point background tasks at the small 3B model (&lt;code&gt;ozai-coder&lt;/code&gt;) and turn off tags and follow-ups. After that, answers started almost immediately again.&lt;/p&gt;

&lt;h3&gt;
  
  
  4. More users don't get more throughput
&lt;/h3&gt;

&lt;p&gt;I wrote a small load generator (Python threads streaming from the gateway) and measured time to first token (TTFT) and throughput at different concurrency levels. Each request asked for 128 tokens:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Users   TTFT p50   TTFT max   Per-user tok/s   Total tok/s
1         0.3 s      0.3 s        19.5            19.5
2         3.4 s      6.7 s        14.5            19.7
4         9.3 s     18.6 s         8.7            20.8
8        21.3 s     43.3 s         4.7            20.7
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Total throughput stays flat at about 20 tokens per second.&lt;/strong&gt; More users just split the same output into smaller slices. The worst TTFT is roughly &lt;em&gt;your place in the queue × one answer&lt;/em&gt;. In practice, requests are processed one at a time.&lt;/p&gt;

&lt;p&gt;For a DevOps engineer this was the key lesson: &lt;strong&gt;an LLM server does not scale like a web server.&lt;/strong&gt; Adding users doesn't use idle capacity, because the hardware is already working at full speed for one request. Production inference servers like vLLM fix this with &lt;em&gt;continuous batching&lt;/em&gt;, which runs many requests through the model in the same pass. That's part 3.&lt;/p&gt;

&lt;h3&gt;
  
  
  5. The model is confidently wrong, and has no idea what day it is
&lt;/h3&gt;

&lt;p&gt;An 8B model has to compress a lot of world knowledge into 5 GB, and it shows. Ask it a precise technical question and it answers with total confidence, even when it's wrong. The KEDA Kafka scaler's default &lt;code&gt;lagThreshold&lt;/code&gt; is &lt;strong&gt;10&lt;/strong&gt;. OzAI says &lt;strong&gt;1000&lt;/strong&gt;:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fypj0e3jpjky2v1o2eb2a.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fypj0e3jpjky2v1o2eb2a.png" alt="OzAI confidently answering that KEDA's default Kafka lagThreshold is 1000 (the docs say 10), then saying its knowledge cutoff is October 2023 and it has no real-time F1 data" width="800" height="357"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The second answer shows the other limit. The model only knows what was in its training data, and it reports a knowledge cutoff of October 2023. The current F1 season is out of reach.&lt;/p&gt;

&lt;p&gt;Both problems have the same cure: stop relying on what the model remembers, and give it the facts at question time. Later in this series I add document RAG and measure it. On 10 KEDA questions like this one, accuracy goes from 1/10 to 8/10.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Fix (part one of RAG):&lt;/strong&gt; web search through a &lt;strong&gt;self-hosted SearXNG&lt;/strong&gt; instance. Open WebUI searches, pulls in the relevant parts of the top results and gives them to the model as context. No search API keys, and no queries sent to a third-party AI provider.&lt;/p&gt;

&lt;p&gt;It still didn't work. That led to the best bug of the project.&lt;/p&gt;

&lt;h3&gt;
  
  
  6. The bug with no error: silent prompt truncation
&lt;/h3&gt;

&lt;p&gt;Web search was on and SearXNG was returning results. The model still said "I don't have access to live data."&lt;/p&gt;

&lt;p&gt;The gateway's request log had the clue. &lt;strong&gt;Every Open WebUI request had exactly 2,050 prompt tokens.&lt;/strong&gt; It didn't matter whether the question was short or long, or whether search was on or off.&lt;/p&gt;

&lt;p&gt;So I ran a test. I sent about 5,000 tokens of filler text with a secret word at the very end:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Sent:   ~5,000 tokens, secret word at the END
Got:    prompt_tokens: 2050, answer: BANANA
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The model found the word, but the gateway reported only 2,050 prompt tokens. About 3,000 tokens had disappeared, and since the end survived, they came from the beginning. Ollama was silently cutting long prompts to fit its context window. It &lt;strong&gt;kept the end and dropped the beginning&lt;/strong&gt;, with no error and no warning. Open WebUI puts the search results at the &lt;em&gt;start&lt;/em&gt; of the prompt, so they were cut every single time.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Fix:&lt;/strong&gt; one line, &lt;code&gt;num_ctx: 8192&lt;/code&gt;, on the gateway model. Then I moved the secret word to the &lt;em&gt;start&lt;/em&gt; of the prompt and repeated the test:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Sent:   ~5,000 tokens, secret word at the START
Got:    prompt_tokens: 5430, answer: BANANA
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The whole prompt now reached the model. Memory use went from 5.6 GB to 6.2 GB.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fy8esubav0glr82ok863a.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fy8esubav0glr82ok863a.png" alt="OzAI calling the search_web tool and answering a question about the 2026 Formula 1 season from search results" width="799" height="170"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;This is the lesson I'd put on a poster for anyone moving from DevOps to AI infrastructure: &lt;strong&gt;in LLM serving, "no error" doesn't mean "correct."&lt;/strong&gt; A web server that drops half a request fails loudly. A model server that drops half a prompt just gives a worse answer. Watch your token counts.&lt;/p&gt;

&lt;h3&gt;
  
  
  7. IDE integration: the gateway was the wrong path for autocomplete
&lt;/h3&gt;

&lt;p&gt;Because the gateway speaks the OpenAI API, connecting VS Code (with the Continue extension) took one config block. Chat and code edits go through the gateway with their own key, so that usage shows up in the logs.&lt;/p&gt;

&lt;p&gt;Autocomplete was different. Code completion uses &lt;em&gt;fill-in-the-middle&lt;/em&gt; (FIM): the model gets the code before and after the cursor and fills the gap. Through the gateway, the coder model answered like a chatbot instead:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;"Certainly! The Fibonacci sequence is a series of numbers where..."
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The gateway's completions endpoint was dropping the FIM parameters (&lt;code&gt;suffix&lt;/code&gt;, &lt;code&gt;raw&lt;/code&gt;) on the way to Ollama. Called directly, Ollama completed the function correctly:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;fibonacci&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;n&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;n&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;=&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;n&lt;/span&gt;
    &lt;span class="k"&gt;else&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nf"&gt;fibonacci&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;n&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="nf"&gt;fibonacci&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;n&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Fix:&lt;/strong&gt; chat goes through the gateway, and autocomplete goes straight to Ollama. That's the dashed line in the architecture diagram. It's a trade-off: autocomplete requests don't appear in the usage logs. For a local setup, I'll take working completions over complete logs.&lt;/p&gt;

&lt;p&gt;Cursor is another story. It calls custom models from its own servers, so it can't reach &lt;code&gt;localhost&lt;/code&gt;. That needs a public, authenticated endpoint, which is coming later with Cloudflare Tunnel and Access.&lt;/p&gt;




&lt;h2&gt;
  
  
  What I'd tell another DevOps engineer
&lt;/h2&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;The gateway is where your skills transfer.&lt;/strong&gt; Keys, quotas, rate limits, routing, request logs: this is API platform work you already know. The model is just another upstream.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Measure before you optimize.&lt;/strong&gt; One table of tokens per second at 1, 2, 4 and 8 users explained more than any blog post I read.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;LLMs fail quietly.&lt;/strong&gt; Truncated prompts, empty answers caused by thinking, confident hallucinations. None of them return a 500. Your monitoring has to look at token counts and answer quality, not just status codes.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Small local models are fast and cheap, but not reliable as a source of facts.&lt;/strong&gt; Give them context (search, documents) instead of trusting what they remember.&lt;/li&gt;
&lt;/ol&gt;

&lt;h2&gt;
  
  
  What's next
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Part 2: observability and load testing.&lt;/strong&gt; A Grafana dashboard for TTFT, latency percentiles and tokens per second, built from the gateway's request log. Plus why LiteLLM's Prometheus metrics weren't an option.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Part 3: from laptop to production.&lt;/strong&gt; vLLM on Amazon EKS with GPU nodes, autoscaling on queue depth with KEDA, and the cost of self-hosting compared with paying per token.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;All configs, benchmarks and notes are in the repo: &lt;strong&gt;&lt;a href="https://github.com/ozy31/self-hosted-llm-platform" rel="noopener noreferrer"&gt;github.com/ozy31/self-hosted-llm-platform&lt;/a&gt;&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;em&gt;If you're making the same move from DevOps into AI infrastructure, I'd like to hear what surprised you.&lt;/em&gt;&lt;/p&gt;

</description>
      <category>ai</category>
      <category>devops</category>
      <category>llm</category>
      <category>selfhosted</category>
    </item>
    <item>
      <title>AWS Site-to-Site VPN Connection and Distrubution over AWS Transit Gateway.</title>
      <dc:creator>Ahmet Ozan GAZİ</dc:creator>
      <pubDate>Tue, 24 Jun 2025 11:40:03 +0000</pubDate>
      <link>https://dev.to/ozy31/aws-site-to-site-vpn-connection-and-distrubution-over-aws-transit-gateway-5465</link>
      <guid>https://dev.to/ozy31/aws-site-to-site-vpn-connection-and-distrubution-over-aws-transit-gateway-5465</guid>
      <description>&lt;h1&gt;
  
  
  AWS Site-to-Site VPN and Transit Gateway Cross-Account Routing
&lt;/h1&gt;

&lt;h2&gt;
  
  
  Introduction
&lt;/h2&gt;

&lt;p&gt;In enterprise cloud infrastructures, establishing secure and scalable network connections between different AWS accounts and VPCs (Virtual Private Cloud) is of critical importance. AWS provides powerful services such as &lt;strong&gt;Site-to-Site VPN&lt;/strong&gt; and &lt;strong&gt;Transit Gateway&lt;/strong&gt; to meet this need. In this article, we will explain step by step how to route network traffic between multiple AWS accounts using Site-to-Site VPN and Transit Gateway.&lt;/p&gt;




&lt;h2&gt;
  
  
  1. Basic Concepts
&lt;/h2&gt;

&lt;h3&gt;
  
  
  Site-to-Site VPN
&lt;/h3&gt;

&lt;p&gt;AWS Site-to-Site VPN creates an encrypted tunnel between your on-premises data center and your AWS VPC. This enables secure data transmission between your on-premises network and cloud environment.&lt;/p&gt;

&lt;h3&gt;
  
  
  Transit Gateway
&lt;/h3&gt;

&lt;p&gt;AWS Transit Gateway allows you to connect multiple VPCs and on-premises networks through a central hub. This structure simplifies network management and increases scalability.&lt;/p&gt;




&lt;h2&gt;
  
  
  2. Scenario and Architecture Definition
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Account A:&lt;/strong&gt; Main AWS account connected to on-premises data center.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Account B and C:&lt;/strong&gt; VPCs located in different AWS accounts.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Objective:&lt;/strong&gt; Provide secure and centralized access from on-premises data center to VPCs in Account B and C.&lt;/li&gt;
&lt;/ul&gt;




&lt;h2&gt;
  
  
  3. Step-by-Step Setup
&lt;/h2&gt;

&lt;h3&gt;
  
  
  3.1. Creating Transit Gateway (Account A)
&lt;/h3&gt;

&lt;ol&gt;
&lt;li&gt;Sign in to &lt;strong&gt;AWS Management Console&lt;/strong&gt; with Account A.&lt;/li&gt;
&lt;li&gt;Follow the steps: &lt;strong&gt;VPC Dashboard&lt;/strong&gt; &amp;gt; &lt;strong&gt;Transit Gateways&lt;/strong&gt; &amp;gt; &lt;strong&gt;Create Transit Gateway&lt;/strong&gt;.&lt;/li&gt;
&lt;li&gt;Give the Transit Gateway a meaningful name and configure necessary settings.&lt;/li&gt;
&lt;li&gt;Note the Transit Gateway ID.&lt;/li&gt;
&lt;/ol&gt;

&lt;h3&gt;
  
  
  3.2. Creating Transit Gateway Attachments
&lt;/h3&gt;

&lt;h4&gt;
  
  
  a) Connecting Account A's VPC to Transit Gateway
&lt;/h4&gt;

&lt;ul&gt;
&lt;li&gt;Create a new attachment from the &lt;strong&gt;Transit Gateway Attachments&lt;/strong&gt; section in VPC Dashboard.&lt;/li&gt;
&lt;li&gt;Select the relevant VPC in Account A and connect it to the Transit Gateway.&lt;/li&gt;
&lt;/ul&gt;

&lt;h4&gt;
  
  
  b) Creating VPC Attachments in Other Accounts (B and C)
&lt;/h4&gt;

&lt;ul&gt;
&lt;li&gt;In Account B and C, enable Transit Gateway sharing through &lt;strong&gt;Resource Access Manager (RAM)&lt;/strong&gt;.&lt;/li&gt;
&lt;li&gt;Share the Transit Gateway from Account A.&lt;/li&gt;
&lt;li&gt;In Account B and C, connect their own VPCs to the Transit Gateway using the shared Transit Gateway.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  3.3. Site-to-Site VPN Setup
&lt;/h3&gt;

&lt;ol&gt;
&lt;li&gt;Create &lt;strong&gt;Customer Gateway&lt;/strong&gt; and &lt;strong&gt;Virtual Private Gateway&lt;/strong&gt; in Account A.&lt;/li&gt;
&lt;li&gt;Create &lt;strong&gt;Site-to-Site VPN Connection&lt;/strong&gt; and enter your on-premises network information.&lt;/li&gt;
&lt;li&gt;Connect the VPN connection to the Transit Gateway (create Attachment).&lt;/li&gt;
&lt;/ol&gt;

&lt;h3&gt;
  
  
  3.4. Route Table and Routing
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;Add routes to each VPC's route table to route traffic to other VPCs and on-premises network through the Transit Gateway.&lt;/li&gt;
&lt;li&gt;Define rules in the Transit Gateway Route Table to route incoming traffic to the correct VPC or VPN.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  3.5. Security Groups and NACLs
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;Configure security groups and network access control lists (NACL) to allow relevant ports and protocols.&lt;/li&gt;
&lt;/ul&gt;




&lt;h2&gt;
  
  
  4. Testing and Validation
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Test the connection by pinging or SSH connecting to resources (e.g., EC2) in Account B and C from the on-premises network.&lt;/li&gt;
&lt;li&gt;Monitor traffic using AWS VPC Flow Logs and CloudWatch.&lt;/li&gt;
&lt;/ul&gt;




&lt;h2&gt;
  
  
  5. Best Practices
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Segment Transit Gateway Route Tables to ensure network isolation.&lt;/li&gt;
&lt;li&gt;Securely manage resource sharing with IAM and RAM.&lt;/li&gt;
&lt;li&gt;Use two tunnels for high availability in VPN connections.&lt;/li&gt;
&lt;/ul&gt;




&lt;h2&gt;
  
  
  Conclusion
&lt;/h2&gt;

&lt;p&gt;With AWS Site-to-Site VPN and Transit Gateway, you can establish a centralized, secure, and scalable network architecture between different AWS accounts and on-premises networks. This structure greatly simplifies network management in large-scale and multi-account architectures.&lt;/p&gt;




&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F6w5p2caq7nzi84f7fmsy.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F6w5p2caq7nzi84f7fmsy.png" alt="Image description" width="800" height="527"&gt;&lt;/a&gt;&lt;/p&gt;






&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

</description>
    </item>
    <item>
      <title>MFA AWS VIA TERMINAL</title>
      <dc:creator>Ahmet Ozan GAZİ</dc:creator>
      <pubDate>Mon, 20 May 2024 10:48:12 +0000</pubDate>
      <link>https://dev.to/ozy31/mfa-aws-via-terminal-406h</link>
      <guid>https://dev.to/ozy31/mfa-aws-via-terminal-406h</guid>
      <description>&lt;p&gt;In today's digital landscape, securing access to cloud services is paramount, particularly as cyber threats become increasingly sophisticated. Amazon Web Services (AWS), a leading cloud service provider, offers robust security features to protect sensitive data and infrastructure. One essential security measure is Multi-Factor Authentication (MFA), which enhances the traditional username and password login process by requiring an additional verification step. This essay will explore the process of configuring and using MFA to access AWS from a local terminal, highlighting the steps involved, the benefits of this security approach, and practical tips for seamless integration. By implementing MFA, users can significantly bolster their security posture, ensuring that access to their AWS environments is both secure and efficient.&lt;br&gt;
 In this article I want draw your attention to some point which we can make it happen with ease. First of all we need to do some configuration like installing necessary packages such as &lt;code&gt;os, subprocess, pyotp, json, base64, binascli, configparser, dotenv&lt;/code&gt;. These packages are prerequisites for making the scripts work . &lt;br&gt;
As a next step we need to define the environment variables which we'll us it in our &lt;code&gt;bash&lt;/code&gt; scripts. To do seo we create a .env file to keep all of our environmental variables to call them in the cli commands.&lt;br&gt;
&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fisjpvcuzixpikj3vh2th.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fisjpvcuzixpikj3vh2th.png" alt="Image description" width="800" height="170"&gt;&lt;/a&gt;&lt;br&gt;
When configuring Multi-Factor Authentication (MFA) to securely access Amazon Web Services (AWS) from a local terminal, one of the crucial steps involves creating a .env file. This file, short for "environment variables," is essential for securely storing and managing sensitive information, such as AWS access keys, secret keys, and MFA device serial numbers. By using a .env file, users can avoid hard-coding these credentials directly into their scripts or applications, which enhances security and simplifies configuration management. The .env file should be placed in the root directory of the project and structured in a way that each variable is declared on a new line.&lt;br&gt;
 When it comes to creating a script for all of these configuration, naturally we need to acquire a snappy script which includes as follows,&lt;br&gt;
1- Imports and Environment Setup:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Imports the necessary modules.&lt;/li&gt;
&lt;li&gt;Loads environment variables from the .env file.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;2- Configuration Reading:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Reads AWS credentials from the specified source profile in the AWS credentials file.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;3-Validation:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Verifies that the TOTP secret is valid and base32 encoded.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;4- MFA Token Generation:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Generates a current MFA token using the TOTP secret.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;5- Session Token Retrieval:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Uses AWS STS to obtain a session token with the MFA token.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;6- Credentials Update:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Updates the AWS credentials file with the temporary session credentials.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Additionally, ensure the ~/.aws/profile under the $HOME directory is correctly configured for your AWS CLI.&lt;/p&gt;

&lt;p&gt;Here is the one example for script that we may use;&lt;br&gt;
&lt;code&gt;aws-login.py&lt;/code&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import os
import subprocess
import pyotp
import json
import base64
import binascii
import configparser
from dotenv import load_dotenv

# Load environment variables from .env file
load_dotenv()

# Retrieve variables from the environment
mfa_serial_number = os.getenv('MFA_SERIAL_NUMBER')
totp_secret = os.getenv('TOTP_SECRET')
source_profile = os.getenv('SOURCE_PROFILE', 'dev-mfa')
aws_profile = os.getenv('AWS_PROFILE', 'default')

# Read credentials from the source profile
config = configparser.ConfigParser()
credentials_file_path = os.path.expanduser('~/.aws/credentials')
config.read(credentials_file_path)

if source_profile not in config:
    print(f"Profile '{source_profile}' not found in {credentials_file_path}.")
    exit(1)

aws_access_key_id = config[source_profile].get('aws_access_key_id')
aws_secret_access_key = config[source_profile].get('aws_secret_access_key')

if not aws_access_key_id or not aws_secret_access_key:
    print(f"Missing credentials in profile '{source_profile}'.")
    exit(1)

# Ensure TOTP secret is base32 encoded
try:
    base64.b32decode(totp_secret, casefold=True)
except binascii.Error:
    print("The provided TOTP secret is not valid base32 encoded. Please check the secret.")
    exit(1)

# Generate MFA token
totp = pyotp.TOTP(totp_secret)
mfa_token = totp.now()

# Get temporary session token using MFA
try:
    response = subprocess.check_output([
        'aws', 'sts', 'get-session-token',
        '--serial-number', mfa_serial_number,
        '--token-code', mfa_token,
        '--output', 'json',
        '--profile', source_profile
    ])
except subprocess.CalledProcessError as e:
    print(f"Error getting session token: {e}")
    exit(1)

# Parse the response
response_json = json.loads(response)
credentials = response_json['Credentials']

# Update the AWS credentials file with the temporary credentials
if aws_profile not in config.sections():
    config.add_section(aws_profile)

config[aws_profile]['aws_access_key_id'] = credentials['AccessKeyId']
config[aws_profile]['aws_secret_access_key'] = credentials['SecretAccessKey']
config[aws_profile]['aws_session_token'] = credentials['SessionToken']

with open(credentials_file_path, 'w') as configfile:
    config.write(configfile)

print(f"🙌🏻 Welcome to Starlet Technologies AWS Cloud Services. All logins and acitivities are monitoring and recording.")
print("⏰ Your AWS CLi session will expire at:", credentials['Expiration'])

## Verify the identity of the temporary credentials
#try:
#    identity = subprocess.check_output(['aws', 'sts', 'get-caller-identity', '--profile', aws_profile])
#    print("Current Identity:", identity.decode('utf-8'))
#except subprocess.CalledProcessError as e:
#    print(f"Error verifying identity: {e}")
#    exit(1)
#
## Attempt to list S3 buckets
#try:
#    s3_buckets = subprocess.check_output(['aws', 's3', 'ls', '--profile', aws_profile])
#    print("S3 Buckets:", s3_buckets.decode('utf-8'))
#except subprocess.CalledProcessError as e:
#    print(f"Error listing S3 buckets: {e}")
#    exit(1)

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In order to make above script run, we use&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;python aws-login.py
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;As a expected output we can see ;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fho39y4r7hzbiuizhx1r4.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fho39y4r7hzbiuizhx1r4.png" alt="Image description" width="800" height="62"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;For the troubleshooting purposes we recommend you to check if you have registered profile under &lt;code&gt;~/.aws/credentials&lt;/code&gt; file.&lt;/p&gt;

</description>
    </item>
  </channel>
</rss>
