Build a Usage and Quality Metrics Console for an LLM Chatbot
System DesignModel answerLast updated
Design a metrics pipeline and console for a multi-tenant LLM chatbot that ingests about 5k events/s and supports sliceable, low-latency queries with 30-day detail and 1-year rollups.
Scenario
Your company runs a conversational assistant powered by large language models. Hundreds of enterprise customers (tenants) use it, and the platform serves traffic from several geographic regions. You are asked to design the metrics console that tenants and internal operators use to understand how the assistant is behaving.
What the console must report
- Number of conversations/requests handled
- Response latency (the product team wants to know whether a plain average is enough)
- Token consumption per request
- Failure rate for each model
Scale
- Roughly 5,000 requests per second in aggregate, with bursts above that
- Several hundred tenants
- Each request can be described by: tenant, model version, region, client platform (web or mobile), outcome, and error code
Functional requirements
- Capture each chat request (latency, token counts, success or failure) in near real time.
- Chart the metrics over 1-minute, 1-hour, and 1-day buckets.
- Let users filter and group by any combination of dimensions, for example: "error rate of model X in North America for tenant Y."
Non-functional requirements
- Highly available, with fast dashboard queries.
- Storage that copes with many dimension combinations (high cardinality).
- Retention: 30 days of fine-grained data and 1 year of daily rollups.
What to cover in your answer
- How the chat service is instrumented without hurting user-facing latency
- The streaming and buffering path from the service to storage
- Choice of storage and aggregation approach (time-series database versus an analytical column store), and how rollups are produced
- How you keep high-cardinality dimensions, such as tenant, from overwhelming the system
- Tenant isolation, caching, failure behavior, and the main trade-offs
State any assumptions and rough capacity numbers you rely on.
Hints
Hint 1
Start with arithmetic: 5k events/s is about 430M events/day. Estimate bytes per event and compression to see whether the 30-day raw data fits on a small cluster.
Hint 2
Ask whether latency is reported as an average or a percentile. Percentiles cannot be averaged across buckets, so think about what you store in rollups so they can still be merged later.
Hint 3
Compare a store that creates one series per label combination with a column store where dimensions are plain columns. Which one suffers when tenant x model x region x platform multiplies?
Hint 4
Decide what must never be part of an aggregation key (request IDs, user IDs, free-text errors) and how you would still answer questions about them.
Model answer
Medium Try it yourself first — then compare your approach, complexity and edge cases.
1. Clarify and size
Clarifying questions
- Freshness: assume dashboards may lag by under a minute.
- Latency: ask for p50/p95/p99, since averages hide tail behavior.
- Access: tenants see only their own data; internal staff can see everything.
Back-of-envelope
- 5,000 events/s gives about 432M events/day; plan for about 15k/s at peak.
- At ~400 bytes per event that is roughly 170-200 GB/day raw. A column store compresses 8-10x, so about 20 GB/day.
- 30 days of raw data is therefore about 600-700 GB compressed, which fits a small replicated cluster.
- Kafka sees only about 2 MB/s on average, which is easy.
- Dimension space: 500 tenants x 20 models x 10 regions x 3 platforms x 10 error codes is about 3M theoretical combinations, but only a fraction (hundreds of thousands) are active in any minute. This is the real cardinality problem.
2. Event and rollup model
Raw event: event_id, timestamp, tenant_id, model_version, region, platform, request_id, latency_ms, prompt_tokens, completion_tokens, status, error_code.
Rollup rows are keyed by (tenant, model, region, platform, error_code, bucket_ts) and store:
- Additive measures: request count, error count, token sums, latency sum
- A mergeable quantile sketch (t-digest or similar) for latency
- Optionally a HyperLogLog state for distinct users
The key insight is that percentiles are not additive. Averaging per-minute p95s is wrong. Storing a sketch lets you merge minute buckets into hour or day buckets and still get a valid p95. Error rate is computed at query time as errors / count, never stored as a ratio.
3. Architecture
Chat service -> (async batched) -> Kafka (partition by tenant)
-> consumers -> OLAP store: raw table (30d TTL)
-> materialized rollups: 1m (30d) -> 1h -> 1d (1y)
-> consumer -> object storage (Parquet archive, replay)
-> optional stream job for alerting
Dashboard UI -> Query service (auth, tenant scoping, routing, cache) -> OLAP store
Instrumentation
- Emit one event per request at completion, including failures and timeouts, from middleware or the LLM gateway (so token counts come from the provider response).
- Push into an in-process buffer; a background thread batches to Kafka with compression.
- If the buffer fills, drop and count dropped events. Analytics must never block a chat response.
Ingestion
- Kafka decouples producers from storage, absorbs bursts, enables replay after a bug, and feeds several consumers.
- Around 24-48 partitions, 7-day retention. Keying by tenant gives locality but risks hot partitions; salt the key for very large tenants.
- Insert into the store in large batches (about 100k rows or every few seconds). Many tiny inserts hurt column stores.
- Use an idempotent or dedup key (event_id) so redelivery does not double count.
4. Storage choice
| Prometheus-style TSDB | Column-store OLAP (ClickHouse, Druid, Pinot) | |
|---|---|---|
| Model | One series per label set | Rows and columns, arbitrary GROUP BY |
| High cardinality | Each series costs memory; blows up | Dimensions are just columns |
| Ad-hoc slicing | Limited | Full SQL |
| Rollups/retention | Needs extra components | TTLs and materialized views |
| Best for | Infra metrics, alerts | Product and tenant analytics |
Recommendation: an OLAP column store for the console; keep a TSDB for low-cardinality operational metrics about the pipeline itself.
5. Aggregation, routing, retention
- Materialized views build the 1-minute rollup from raw, then 1-hour from 1-minute, then 1-day from 1-hour, using aggregate-state columns so sketches merge correctly.
- The query service routes by range: under about 6 hours use 1m, under about 7 days use 1h, otherwise 1d. Raw data is only for drill-down.
- A one-year chart then scans roughly 365 x (active combinations) rows instead of billions of events.
- TTLs: raw and 1m for 30 days, 1d for one year. Optionally keep a Parquet archive in object storage for audits.
6. High-cardinality handling
- Sort key starting with tenant_id, then model, region, time, so per-tenant queries skip most data. Use dictionary-encoded types for low-distinct-count strings.
- Bound aggregate keys. Never include request_id, user_id, or free-text messages. Map errors to a bounded error_code enum. Unbounded fields live only in raw data.
- Top-K plus "other" for dimensions that can grow without limit inside a tenant.
- Approximate distinct counts via HyperLogLog, which merge across rollups.
- Caching of normalized queries for 30-60 seconds; dashboards re-issue the same tiles constantly.
7. Security, availability, trade-offs
- The query service injects tenant_id from the auth token; tenants can never override it. Consider row-level policies as a second layer.
- Replicate each shard (2 copies) with a coordination service. If the store is down, Kafka holds events, so the console goes stale but loses no data; consumers catch up on recovery.
- Rate-limit expensive queries and cap group-by cardinality per request.
Trade-offs
- Rollups cost storage and limit which dimensions can be sliced at old ages; raw data answers anything but only for 30 days.
- Sketches give approximate percentiles (typically within about 1%) in exchange for mergeability.
- Fresher dashboards mean smaller insert batches, which costs ingest efficiency.
Common mistakes
- Averaging percentiles across buckets.
- Using a per-series TSDB for tenant x model x region metrics.
- Putting unbounded IDs in rollup keys.
- Emitting events synchronously from the request path.
- Storing error rate as a number instead of numerator and denominator.
- Forgetting dedup on replay, and forgetting tenant isolation at query time.