方寸 Portal

AI Engineer Portal

Your personal operating system for career transition.

Private mode

Exercise

Transform JSONL trace logs into aggregated metrics

Transform JSONL Trace Logs into Aggregated Metrics

AI services emit structured logs for every LLM call: model used, input/output token counts, latency, feature name, and whether the call succeeded. These logs are the raw material for operational dashboards, cost attribution, and debugging regressions.

What to build

Implement aggregate_traces(lines: list[str]) -> AggregateReport that parses JSONL trace logs and produces a structured report.

Input format — each line is a JSON object with:

  • request_id: str
  • model: str
  • feature: str
  • input_tokens: int
  • output_tokens: int
  • latency_ms: int
  • success: bool
  • error: str | null

AggregateReport dataclass fields:

  • total_calls: int
  • success_count: int
  • failure_count: int
  • success_rate: float — rounded to 4 decimal places
  • avg_latency_ms: float — across all calls
  • p95_latency_ms: float — 95th percentile latency
  • total_input_tokens: int
  • total_output_tokens: int
  • by_model: dict[str, ModelStats] — per-model breakdown
  • by_feature: dict[str, FeatureStats] — per-feature breakdown

ModelStats: call_count, success_rate, avg_latency_ms, total_tokens

FeatureStats: call_count, success_rate, avg_latency_ms, avg_tokens_per_call

Why this matters

Operational insight from trace data is one of the most direct ways to improve an AI system. Engineers who can write this transformation quickly can answer production questions in minutes instead of filing tickets to a data team.

Constraints

  • Skip malformed lines (log a warning, do not abort).
  • p95_latency_ms should use numpy-free percentile computation.
  • All float fields rounded to 2 decimal places except success_rate (4 places).

Data Transformation / medium / Step 9 of 16

Practice stage

Transform and summarize data safely

Hint

Think like an evaluation or trace pipeline: group clearly, preserve the data story, and make edge cases visible instead of clever.

Success criteria
  • - Handles missing or noisy records predictably
  • - Produces summary output that is easy to inspect
  • - Would be safe to rerun in a script workflow
Review checklist
  • - Do grouped metrics stay readable?
  • - Would malformed rows be debuggable?
  • - Did I choose names that explain the transformation?

Practice

Generate a variation

Generate a new exercise variation to deepen understanding or practice a related concept.

Attempt history

Recent submissions

Before you submit, decide what a strong answer should make obvious to the reviewer.

No attempts yet.