Exposing the Invisible: How One Message Uncovered KV Cache Metrics for a Production LLM Inference Stack
Introduction
In the high-stakes world of production large language model (LLM) inference, visibility into system internals is not a luxury—it is a necessity. When a model serves thousands of requests across eight GPUs with a 2.58 million token KV cache, the question "how full is the cache?" becomes existential: a full cache means preempted requests, degraded service, and frustrated users. This article examines a single message from a complex engineering session—message 12729—where an AI assistant, responding to exactly this question, located the relevant metrics, enabled them, and laid the groundwork for a full monitoring stack. The message is deceptively brief: a reasoning block and a single bash command. But behind it lies a detective story of searching through source code, understanding metric naming conventions, making architectural decisions about monitoring strategy, and ultimately transforming an opaque system into one that can report its own internal state.
The Context: A Production Inference Stack Under Tension
To understand message 12729, one must first understand the environment in which it operates. The assistant was managing a DeepSeek-V4-Flash deployment on a machine with 8× RTX PRO 6000 Blackwell GPUs, configured with prefill-decode (PD) disaggregation. In this architecture, prefill work (processing input prompts) runs on GPUs 0–3, while decode work (generating tokens) runs on GPUs 4–7, connected through a router on port 30001. The decode server alone held a KV cache of 2.58 million tokens at 512K context length—a massive memory footprint spanning approximately 83 GiB per GPU.
This deployment had already undergone extensive optimization. The assistant had designed custom MMA sparse-MLA decode kernels, fixed an indexer bottleneck that was recomputing scores over the full 262K-position context every decode step (yielding a ~17× throughput improvement), deployed PD disaggregation with systemd services enabled at boot, and resolved tool-calling quality issues by wiring the correct chat template and reasoning parser. The system was running, but it was running blind—at least from a monitoring perspective.
When the user asked at message 12724, "do we have any metrcis / prometheus / grafana that we can use to see how full kv-cache is?", they were asking about a specific blind spot. The router exposed metrics on port 29001, but those were request-level metrics—HTTP connection counts, request durations, worker health. None of them answered the fundamental question: how close was the KV cache to capacity?
The Investigation Preceding Message 12729
The assistant's response in message 12729 was not a spontaneous answer. It was the culmination of a four-message investigation spanning messages 12725 through 12728, each building on the previous.
Message 12725 began with a reconnaissance phase. The assistant queried the router's Prometheus endpoint on port 29001, finding metrics like smg_http_requests_total, smg_router_request_duration_seconds, and smg_worker_cb_consecutive_failures—useful for operational monitoring but silent on KV cache state. It also checked the decode server's /metrics endpoint, which returned "Not Found": metrics were disabled. The assistant then searched for metric names in the sglang source code, looking for patterns like sglang:*token*, sglang:*cache*, sglang:*kv*, but the initial search of collector.py came up empty because the file wasn't at the expected path.
Message 12726 refined the search. The assistant broadened the grep pattern and searched more aggressively, but still found nothing—the metrics collector wasn't where the assistant assumed it would be.
Message 12727 changed strategy entirely. Instead of guessing paths, the assistant used find to locate all metric-related Python files across the sglang source tree. This revealed the actual locations: python/sglang/srt/observability/metrics_collector.py, python/sglang/srt/observability/request_metrics_exporter.py, and others. The assistant also found files referencing token_usage and cache_usage—the very metrics the user needed.
Message 12728 extracted the specific gauge names. The assistant found sglang:full_token_usage (the fraction of KV pool used, ranging from 0 to 1), sglang:kv_used_tokens, sglang:kv_available_tokens, and sglang:max_total_num_tokens (the total capacity). It also discovered related metrics like sglang:cache_hit_rate, sglang:num_running_reqs, and sglang:num_queue_reqs. The missing piece was simply that --enable-metrics had not been passed to the server launch commands.
This four-message arc—from reconnaissance to discovery to extraction—is a textbook example of systematic debugging. Each step informed the next: the router metrics told the assistant what wasn't available, the failed search told it the file structure was different than expected, the broader search located the actual files, and the final grep extracted the exact metric names. Message 12729 is the payoff: the action taken based on that accumulated knowledge.
Message 12729: The Reasoning and the Action
The message itself consists of two parts: an Agent Reasoning block and a single bash command. The reasoning block reveals the assistant's thought process in detail.
First, the assistant summarizes what it found: "I found the key KV cache metrics—sglang:full_token_usage is the one that directly shows how full the cache is as a fraction between 0 and 1." This identification is crucial. The user asked "how full" the cache is, and full_token_usage is the exact metric that answers that question. The assistant correctly identifies that the raw counts (kv_used_tokens, kv_available_tokens) and the capacity (max_total_num_tokens) are supplementary—useful for detailed analysis, but the single-number answer is the usage fraction.
Second, the assistant reasons about how to expose these metrics: "To expose these metrics, I need to enable --enable-metrics on both the prefill and decode servers, then the /metrics endpoint will expose all the Prometheus gauges." This is a design decision with implications. The assistant could have taken several approaches:
- Direct query approach: Enable metrics, then query the raw
/metricsendpoint withcurland show the user the numbers. This would answer the immediate question but provide no historical trending or visualization. - Full Prometheus/Grafana setup: Install Prometheus to scrape the metrics endpoint, install Grafana to build dashboards. This would provide rich visualization but take significant time and introduce new services to manage.
- Staged approach: Enable metrics first, verify they work, then assess whether the user wants the full stack. The assistant chose a staged approach, as revealed in the reasoning: "I'm thinking through whether to just enable metrics and show them the raw endpoint, or go ahead and set up the full Prometheus + Grafana stack since they mentioned it explicitly. Let me take a staged approach: enable metrics on both servers, verify the KV metrics are exposed, show them the key values via a simple curl query, then ask if they want the full monitoring stack or if the raw metrics are sufficient." This is a mature engineering judgment. The assistant recognizes that the user's primary need is information ("how full is the KV cache?"), not infrastructure. Enabling metrics and showing the raw values satisfies the immediate need with minimal risk. The full monitoring stack can follow if the user wants persistent visibility. Third, the assistant checks whether Prometheus or Grafana already exist on the system: "check if prometheus and grafana are already running on the host so I know whether to point them to an existing setup or help them install it." This is another smart decision—no point installing a duplicate monitoring stack if one already exists. The bash command that follows executes on these decisions. It does two things:
- Checks for existing monitoring infrastructure:
which prometheus grafana-server,systemctl list-unit-files | grep -iE "prometheus|grafana",docker ps | grep -iE "prom|grafana". All return nothing—"not installed." - Enables metrics on both servers: Uses
sedto add--enable-metricsto the prefill and decode launch scripts. The sed patterns are precise: they match the existing flag string (--moe-runner-backend triton --mem-fraction-static 0.80for prefill,0.85for decode) and append--enable-metricsto it. The grep confirms that each script now contains exactly one instance ofenable-metrics. The decision to modify the shell scripts rather than the systemd service files or the running processes is pragmatic. The servers are already running under systemd; modifying the scripts means the next restart (or the next boot, since services are enabled) will pick up the change. The assistant could have restarted the services immediately, but the reasoning suggests a staged approach: enable first, verify, then potentially restart.
Assumptions and Their Implications
Every engineering decision rests on assumptions, and message 12729 is no exception. Several assumptions are worth examining.
Assumption 1: --enable-metrics exposes all the KV cache metrics found in the source code. The assistant found metric names like sglang:full_token_usage in the source, but it has not yet verified that these metrics actually appear in the /metrics output when the flag is enabled. There could be conditional logic—metrics that only register under certain conditions, or that require additional flags to activate. The assistant's staged approach mitigates this: the next step (not shown in this message) would be to query the endpoint and verify the metrics appear.
Assumption 2: The metrics are meaningful under the PD disaggregation architecture. The KV cache lives on the decode server (GPUs 4–7), but the prefill server also has a KV cache for its own context. The assistant enabled metrics on both servers. Is sglang:full_token_usage on the prefill server meaningful, or does it just show zero utilization because prefill doesn't maintain a long-running cache? The assistant doesn't distinguish, but the user's question specifically concerns the decode-side KV cache where the 2.58M tokens reside.
Assumption 3: The sed modification is safe and reversible. The assistant uses sed -i to modify the shell scripts in place. If the pattern doesn't match (e.g., due to whitespace differences or comment lines), the script could be left unchanged or corrupted. The grep verification (checking that each script has one instance of enable-metrics) provides a sanity check, but it doesn't confirm the flag is in the right position within the command line.
Assumption 4: The user wants a staged approach rather than immediate full setup. The assistant's reasoning explicitly weighs the options and chooses staging. This is reasonable given the context—the user asked "do we have any metrics," not "set up Grafana"—but it means the full monitoring stack remains a future task.
The Thinking Process: A Window into Engineering Judgment
The Agent Reasoning block in message 12729 is particularly valuable because it reveals the assistant's meta-cognition—its awareness of its own decision-making process. Several aspects stand out.
Prioritization of the user's actual need. The assistant recognizes that the user asked "how full" the cache is, and immediately identifies sglang:full_token_usage as the direct answer. It doesn't get lost in the complexity of all available metrics; it finds the one that maps to the user's question.
Awareness of the trade-off between speed and completeness. The assistant explicitly considers two paths (raw metrics vs. full stack) and chooses the one that delivers value fastest. This is classic engineering trade-off reasoning: the raw metrics answer the question now; the dashboard can wait.
Systematic verification. The assistant doesn't just enable metrics blindly. It checks for existing infrastructure, modifies the scripts, and verifies the modification took effect. Each step has a verification mechanism.
Documentation of findings. The reasoning block serves as a record of what was found and why decisions were made. This is valuable for future debugging—if the metrics don't appear, the reasoning trail shows exactly what was done and what assumptions were made.
Input Knowledge and Output Knowledge
To fully understand message 12729, one needs specific input knowledge:
- Understanding of Prometheus metric naming conventions: The assistant knows that
sglang:full_token_usageis a gauge (a single numeric value that can go up and down) and that it will be exposed on a/metricsendpoint in Prometheus text format. - Knowledge of sglang's architecture: The assistant knows that
--enable-metricsis the flag that activates the metrics endpoint, that the metrics are exposed on the same port as the API server, and that the KV cache metrics live on the decode server. - Understanding of PD disaggregation: The assistant knows that prefill and decode are separate processes with separate KV caches, and that both need metrics enabled.
- Shell scripting and system administration: The assistant knows how to use
sedfor precise string replacement, how to check for installed binaries withwhich, and how to verify systemd unit files. The output knowledge created by this message includes: - The shell scripts have been modified to include
--enable-metricson both prefill and decode servers. - Prometheus and Grafana are confirmed not installed on the target machine, establishing the baseline for any future monitoring setup.
- The metric names are documented in the reasoning block, serving as a reference for future queries and dashboard configuration.
- A decision has been made to proceed with a staged approach: enable metrics first, then assess whether the full stack is needed.
The Broader Significance
Message 12729 is, on its surface, a simple operational task: add a flag to two shell scripts and check for installed software. But its significance lies in what it represents: the moment when an opaque system becomes transparent.
Before this message, the KV cache was a black box. The assistant knew its theoretical capacity (2.58M tokens) and could infer usage from GPU memory consumption (83 GiB on decode GPUs), but there was no direct, programmatic way to answer "how full is it?" After this message, that question has a direct answer: sglang:full_token_usage on the decode server's /metrics endpoint.
This transformation from opaque to transparent is a recurring theme in production engineering. Every system starts as a black box; the work of monitoring is the work of opening that box, instrumenting its internals, and making its state visible. Message 12729 is a small but concrete step in that process—a step that, combined with the Prometheus and Grafana setup that follows in subsequent messages, will give the user a real-time dashboard of KV cache utilization, request rates, latency distributions, and more.
Conclusion
Message 12729 captures a pivotal moment in a complex engineering session: the transition from investigation to action. Building on four messages of systematic source-code archaeology, the assistant identified the exact metrics that answer the user's question about KV cache fullness, reasoned about the best approach to expose them, and executed the changes with care and verification. The message demonstrates the value of methodical investigation, the importance of understanding user intent, and the judgment required to balance speed against completeness. For anyone building or operating production LLM inference systems, it offers a case study in how to turn a simple question—"how full is the cache?"—into concrete, actionable monitoring infrastructure.