AI benchmarking: Latency, stages, and cost analysis
I built a command-line benchmark and added backend timing instrumentation for an AI application. The benchmark tracked file upload and ingestion and measured how answers streamed back, separating model execution from the rest of the user’s wait. I also used runtime telemetry and controlled replays to analyze agent cost and latency.
- Problem
- The client’s timer already included the time the server spent decoding a file. Adding the two would count that work twice. For cost, a total token count hid how much context the agent wrote to the cache and how much it reused.
- Decision
- I matched each file’s server timings to its request and reported the unexplained time separately. To evaluate a shorter prompt, I replayed the same questions and checked the cost per question alongside the tools the agent chose.
- Result
- I used the cost analysis to recommend changes to prompt length, tool access, and API calls. We tested the system in production and used those findings to upgrade it.
- Context
- Sonatus - AI performance tooling
- Period
- 2026
- My focus & role
- Benchmark tooling, streaming-client integration, structured reports, comparison workflows, and agent cost analysis.
- Technology
- Python · HTTP · SSE
Account for the whole request
Concept model - synthetic data- Setup0.4 s
- Upload1.6 s
- Ingest3.0 s
- Agent4.5 s
- Delivery0.5 s
- Observed elapsed time
- 10.0 s
- Accounted stages
- 10.0 s
- Unaccounted time
- 0.0 s
The sequential stage intervals account for the complete synthetic request. Child timings would explain a stage, not add to this total.
Measure the workflow the user actually waits for
A diagnostic question could involve creating a conversation, uploading a vehicle log, processing the file, running an agent, and delivering its response. Measuring only the model call left several substantial parts of that workflow unexplained. A slow answer did not identify whether the problem was transfer, ingestion, orchestration, or output delivery.
I implemented the benchmark CLI, its report and comparison workflow, and the backend changes exposing per-file ingest statistics and stage timings.
Define the boundaries before collecting durations
The runner exercised the application protocol: authenticate, create a project and conversation, submit a file hash, upload through a presigned URL, confirm the upload, submit a question, and consume the answer stream. Recording these boundaries separated the client’s wait from the processing performed after a file reached the server.
One run, several measurement boundaries
Authenticate, create project and conversation, record elapsed time
Submit file hash → upload through a presigned URL → confirm
File ingest statistics and available processing-stage timings
Question submission, streamed events, answer completion
Join by existing conversation and file identifiers; retain gaps
I added ingest statistics to the session-status response so the report could explain work inside that server-side interval. The additional information was scoped to the relevant processing path, with an allowlist for fields exposed by the status endpoint. Instrumentation needed to explain timing without turning a status API into a dump of internal execution data.
Client elapsed time and server stage durations answer different questions. The client records how long an operation took from its perspective. Server timings explain portions of that wait. Adding nested server timings to the already measured client interval would count the same work twice.
Use existing identifiers and a tolerant streaming client
I used existing conversation and file identifiers to correlate a run. The conversation identifier also served as the run identifier. This avoided requiring new propagation headers, middleware, or a tracing rollout before the benchmark could produce useful results.
The existing identifiers let me correlate events for this workflow, rather than establish distributed tracing for every service. The report still had to retain intervals that the available measurements could not explain.
I also chose not to reuse an existing streaming client whose strict event-tag model rejected unknown frames. A benchmark observing an evolving protocol should not fail simply because an additional frame type appears. The CLI used its own focused client boundary so it could consume the stream without inheriting that restriction. This added a small amount of protocol-specific code to maintain, but kept the measurement tool independent of a more restrictive application model.
Missing instrumentation is not zero elapsed time
The report included an unaccounted-time check against the overall elapsed duration. A plausible set of stage numbers can still omit an interval; keeping that residual visible makes gaps in the measurement model inspectable. The interactive example above demonstrates this distinction using a deliberately simple sequential trace.
Ingest statistics were absent before the first ingest. Cache-hit paths could also provide only a subset of the fields produced by a complete decode. I treated these as meaningful states rather than filling unavailable fields with zeros. Otherwise, a cached path could appear fully measured when some stages had simply not run or had not reported a duration.
Interpret the timing record
No measurement is available. Do not infer a zero-duration ingest.
Report available fields and the cache state; preserve omissions.
Use them to explain the parent interval, not inflate the total.
Make a comparison reproducible enough to interpret
The CLI included a comparison command so engineers could examine runs using the same report structure. Cold and warm executions could differ because ingestion reused cached work, while upload duration remained sensitive to transfer conditions. A lower total alone did not establish which implementation change caused it.
Keeping the input, configuration, and stage boundaries explicit made it possible to ask a narrower question: which portion of this workflow changed? I added automated tests around the benchmark package and the affected backend timing paths.
Connect latency to what the agent actually does
Runtime telemetry showed me which tools the agent used and how much time it spent on calls. To evaluate a prompt change, I removed repeated instructions, counted tokens in the fixed part of the prompt, and replayed the same questions. I checked each run’s tool choices against the expected set so I could see whether the shorter prompt still sent the agent to the right tool.
The replay harness recorded wall time, model usage, tool-call sequence, and API operation timing. Request metadata was sufficient for measuring operation counts and payload sizes; recording credentials or complete request bodies was unnecessary.
How can the same token count have a different cost?
Synthetic example · invented ratesConsider only the cached portion of a prompt across two replays of one question. For this example, writing 1,000 tokens costs four credits and reading 1,000 costs one credit. All other usage stays fixed.
Write 2,000 tokens (8 credits) and read 2,000 tokens (2 credits).
Write 1,000 tokens (4 credits) and read 3,000 tokens (3 credits).
I used those comparisons to recommend changes to the prompt, the tools available to the agent, and the API calls behind them. We tested the system in production and used the results to upgrade it.
Project artifacts
Measurement sources in the cost and latency analysis
Original report excerpt
A practical baseline for engineering decisions
The delivered tooling connected a user-visible workflow to measurable stages and made missing attribution explicit. Engineers could inspect where a request spent time, compare runs, and decide whether the next change belonged in upload, ingestion, agent behavior, or delivery. The stage timings gave engineers more detail about the cause of a slow response than the total latency alone.
Related case: AI Technician’s streaming architecture explains the client, execution, and persistence boundaries around long-running AI work.