Skip to content

MAF-21239: feat(deploy): chart the request-observed KV cache hit per model - #189

Closed
hhk7734 wants to merge 6 commits into
mainfrom
MAF-21239-kv-cache-hit-panels
Closed

hhk7734 wants to merge 6 commits into
mainfrom
MAF-21239-kv-cache-hit-panels

Conversation

@hhk7734

@hhk7734 hhk7734 commented Sep 29, 2026 •

Copy link
Copy Markdown
Member

JIRA: MAF-21239
Artifact: MAF-21239 KV cache hit dashboard panels

Summary

This PR adds a "KV cache hit (request-observed)" row to the mif-aigateway dashboard. It shows, per model, how many input tokens the engines served from their KV cache, how much of the traffic that number covers, and how close the gateway's KV index prediction came. The panels read the per-request histograms that the Heimdall AI Gateway exports from its next release, and fall back to the older counters on gateways from v0.15.0 to v0.17.0.

Metrics used

Metric Records
aigateway_engine_input_tokens the engine's input token count, for every request whose engine returned usage
aigateway_engine_cached_tokens the input tokens the engine served from its KV cache, for requests whose engine response included cached_tokens
aigateway_predicted_cached_tokens the input tokens the gateway's KV index held as cached on the picked prefill worker, read before dispatch

All three are histograms with only the model label, so the row has no per-endpoint breakdown.

Panels

id Panel Series
84 KV cache hit ratio by model measured engine_cached_sum / engine_input_sum, predicted predicted_sum / engine_input_sum
85 Cached-token coverage by model engine engine_cached_count / engine_input_count, prediction predicted_count / engine_cached_count
86 Engine input, cached and predicted tokens/sec by model the three _sum rates
87 Cached tokens per request p50/p95 by model histogram_quantile over engine_cached_tokens_bucket

The measured hit ratio is a lower bound that is exact at 100% engine coverage, because a request without cached_tokens still adds its input tokens. The panel descriptions and the website docs say how to read the ratio with its coverage, and how it differs from the engine's own aggregate gauge.

During a rolling update, each gateway pod exports either the histograms or the older counters. The measured series take whichever set a pod has with or before summing by model. The predicted series and the per-request distribution need the histograms, and the predicted ratio divides only by newer pods' input tokens.

The Tokenizer cache and Odin InferenceService rows move down by 17 grid units. #188 changes only panels 77 to 79, so the two PRs do not overlap.

Test plan (head 41081e9)

  • hack/dashboards/verify_aigateway_kv_cache_hit.py scrapes four fake gateways (two on the histograms, two on the older counters) with a local Prometheus 3.15 and runs all nine panel targets. Every target returns the expected value.
  • Removing the older-counter fallback from the measured hit ratio fails the check. Removing the or 0 term from the engine coverage also fails it, because a model whose older gateway never saw cached_tokens disappears instead of showing 0%.
  • make helm-lint and helm template pass, and the rendered ConfigMap contains the new queries.
  • npm run build in website passes.
  • Send a controlled cache-miss request and a cache-hit request, and check that the dashboard values match cached_tokens in the final responses.
  • After merge, deploy the chart built by CI/CD to the target environment and check that the panels load in Grafana.

🤖 Generated with Claude Code

hhk7734 and others added 6 commits September 29, 2026 20:40
…model

Add a "KV cache hit (request-observed)" section to the AIGateway dashboard.
It reads the gateway's aigateway_prompt_tokens_observed_total and
aigateway_cached_tokens_reports_total, which count the cached_tokens that
each final engine response reported. The hit ratio uses the engine's own
prompt token count as its denominator, and a coverage panel shows how much
traffic reported at all. A model with no reports shows 0% coverage instead
of disappearing.

hack/dashboards/verify_aigateway_kv_cache_hit.py runs the panel queries
against fake gateways and a local Prometheus.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01Lykmj8mm4YfRenW75U6Jvu
…anels

Describe the new dashboard section and separate the three signals that are
all called a cache hit rate: the request-observed ratio, the engine's own
aggregate gauge, and the scheduler's predicted rate at routing time.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01Lykmj8mm4YfRenW75U6Jvu
The gateway never runs its post-stream bookkeeping for a request that the
client cancels mid-stream, so neither aigateway_requests_total nor the two
cache counters see it. The gateway test
a_client_cancel_leaves_no_trace_in_either_cache_counter asserts this. The
panel description and the docs said such a request counts as absent.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01Lykmj8mm4YfRenW75U6Jvu
…tails flag

MoAI Engine returns cached_tokens for every request and ignores
--enable-prompt-tokens-details, like SGLang. Only a vLLM worker started
without the flag leaves coverage below 100%.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01Lykmj8mm4YfRenW75U6Jvu
… histograms

The gateway now exports aigateway_engine_input_tokens,
aigateway_engine_cached_tokens and aigateway_predicted_cached_tokens as
per-request histograms. The four panels read them:

- KV cache hit ratio by model: measured and predicted
- Cached-token coverage by model: engine and prediction coverage
- Engine input, cached and predicted tokens/sec by model
- Cached tokens per request p50/p95 by model

Measured series fall back per pod to the older counters, so gateways that
still export them keep their values during a rolling update. The check
script now models that mixed rollout.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01Lykmj8mm4YfRenW75U6Jvu
@hhk7734

hhk7734 commented Sep 30, 2026

Copy link
Copy Markdown
Member Author

The dashboard was removed in #191

@hhk7734 hhk7734 closed this Sep 30, 2026
@hhk7734
hhk7734 deleted the MAF-21239-kv-cache-hit-panels branch September 30, 2026 08:22
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant