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
  1. Setup0.4 s
  2. Upload1.6 s
  3. Ingest3.0 s
  4. Agent4.5 s
  5. 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.

FIG. 01 A simplified timing-accounting example with invented durations. The original CLI measured real requests; this instrument demonstrates the accounting check without publishing internal measurements.

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

Benchmark client

Authenticate, create project and conversation, record elapsed time

Upload protocol

Submit file hash → upload through a presigned URL → confirm

Session status

File ingest statistics and available processing-stage timings

Answer stream

Question submission, streamed events, answer completion

Correlated report

Join by existing conversation and file identifiers; retain gaps

FIG. 02 Reconstructed measurement flow. Client elapsed time and server stage timings describe different boundaries; the report relates them without requiring a new distributed tracing protocol.

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 ingest record yet

No measurement is available. Do not infer a zero-duration ingest.

Partial record on a cached path

Report available fields and the cache state; preserve omissions.

Nested processing durations

Use them to explain the parent interval, not inflate the total.

FIG. 03 Interpretation rules for the recorded stage data. Missing, partial, and measured-zero values are different states.

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 rates

Consider 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.

Replay A · 10 credits

Write 2,000 tokens (8 credits) and read 2,000 tokens (2 credits).

Replay B · 7 credits

Write 1,000 tokens (4 credits) and read 3,000 tokens (3 credits).

FIG. 04 Both replays account for 4,000 cached tokens. Pricing reads and writes separately explains the difference. The original analysis also recorded fresh input and output usage. The figures here illustrate the calculation and use fictional prices.

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
Report table pairing static token counts with fixed prompt overhead, aggregate telemetry with traffic and latency, and fixed-question replays with operation timing and tool selection.View larger image (new tab)
FIG. 05 In my September 2026 analysis, traffic aggregates described observed usage. To compare tool choices or operation latency on the same questions, I used controlled replay. This crop preserves the source and purpose columns from the measurement plan.

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.