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
Data Manipulation (SQL/Python)
Data Engineer
Truefoundry
November 29, 2026
Data Engineer
Technical Screen
Data Manipulation (SQL/Python)
Medium

0

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
SQL
as-of join
percentile_cont
window functions
cost attribution
How to Approach This
  1. Clarify the schema and expected output format before writing queries.
  2. Use CTEs (WITH clauses) to break complex queries into readable steps.
  3. Consider window functions (ROW_NUMBER, RANK, LAG, LEAD) for ranking and sequential analysis.
  4. Watch for NULLs, duplicates, and edge cases in JOINs and GROUP BY.
  5. 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 Problems
Sample 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
  1. Build price intervals with LEAD(effective_from) so each price has a valid range.
  2. Join logs to the interval containing ts (an as-of join).
  3. Aggregate per team and day: cost from successful ...

Submit Your Answer
Markdown supported

Related Questions