Daily Team LLM Spend and P95 Latency From Gateway Logs
Last updated: November 29, 2026
Quick Overview
Using gateway request logs and a model price table whose prices change over time, write SQL that reports each team's daily cost, request count, error rate and p95 latency, plus day-over-day cost change.
Truefoundry
November 29, 20260
0
0 solved
Using gateway request logs and a model price table whose prices change over time, write SQL that reports each team's daily cost, request count, error rate and p95 latency, plus day-over-day cost change.
TrueFoundry's gateway gives platform teams cost and latency observability per team, model and key, which is what lets them set budgets. The interviewer checks a time-correct price join, careful handling of failed requests, percentile aggregation and window functions.
What the Interviewer Expects
- Join each request to the price that was effective at request time
- Compute cost from input and output tokens separately
- Exclude failed requests from latency percentiles but count them for error rate
- Use a window function for day-over-day change
- Discuss timezone and late-arriving logs
Key Topics to Cover
How to Approach This
- Clarify the schema and expected output format before writing queries.
- Use CTEs (WITH clauses) to break complex queries into readable steps.
- Consider window functions (ROW_NUMBER, RANK, LAG, LEAD) for ranking and sequential analysis.
- Watch for NULLs, duplicates, and edge cases in JOINs and GROUP BY.
- For pandas, prefer vectorized operations over row-by-row iteration.
Possible Follow-up Questions
- How would you build this as an incremental materialized view in a columnar store?
- How do cached responses affect cost and latency reporting?
- How would you alert when a team is on pace to exceed its monthly budget?
- Why is averaging per-replica p95 values wrong?
Sharpen Your Skills on Codemia
Practice similar problems with our interactive workspace, get AI feedback, and track your progress.
Practice SQL ProblemsSample Answer
Clarify the Data
gateway_logs(request_id, ts, team_id, model, input_tokens, output_tokens, latency_ms, status_code) with one row per request, and model_prices(model, input_per_1k, output_per_1k, effective_from). P...
Approach
- Build price intervals with
LEAD(effective_from)so each price has a valid range. - Join logs to the interval containing
ts(an as-of join). - Aggregate per team and day: cost from successful ...