Skip to content

[AI-4110] Round model call times to the microsecond - #2577

Merged
kanat merged 4 commits into
mainfrom
kanat/ai-4110-model-call-ms-rounding
Nov 27, 2026
Merged

kanat merged 4 commits into
mainfrom
kanat/ai-4110-model-call-ms-rounding

Conversation

@kanat

@kanat kanat commented Nov 26, 2026 •

Copy link
Copy Markdown
Collaborator

Why

The router logs ttft_ms and duration_ms of a model call as the raw float sum of the time it waited for response headers and the stream's own time (acceleration/internal/llmrouter/session.go:404-406). Both parts are whole microseconds divided by 1000, but their float64 sum is not: 0.722 ms of headers plus a 1921.004 ms first token prints as 1921.7259999999999. Nessy shows that string on an agent_model_calls row. The same sum is handed to llm.CallTiming (OnTiming) and stored as stat.LatencyMs / stat.DurationMs.

The failure line (duration_ms, session.go:245) is not affected: it is float64(µs) / 1000 with nothing added, and a correctly rounded division prints its shortest decimal (1234567/1000 is 1234.567). It needs no change.

How it works

headersMs (µs/1000) + response.TimeToFirstTokenMs / DurationMs
        │
        └─ routing.RoundMs: round to the microsecond, once
              ├─ "model call timing" log: ttft_ms, duration_ms
              ├─ llm.CallTiming (OnTiming)
              └─ routing.Stat: LatencyMs, DurationMs

routing.RoundMs sits next to routing.MsSince, which measures every latency in microseconds. The sweep found one more place with the same pattern, so it uses the same helper.

Changes

  • routing.RoundMs (new, 3 lines) and its use in llmrouter/session.go for ttftMs and durationMs.
  • agent/turns.go: speechEndToAudio and speechEndToAudible add the transcriber's latency to a measured leg and are logged in voice turn timing (speech_end_to_audio_ms, speech_end_to_audible_ms), so they are rounded the same way.

Sweep: float _ms log attributes in acceleration/ (non-test)

Site Built from Noisy?
llmrouter/session.go:406,407 ttft_ms, duration_ms float sum yes, fixed
agent/turns.go:668,677 speech_end_to_audio_ms, speech_end_to_audible_ms float sum yes, fixed
llmrouter/session.go:220 failure duration_ms µs / 1000 no
llm/stream.go:578,614 TimeToFirstTokenMs, DurationMs µs / 1000 no
ttsrouter/session.go:400,512,514, sttrouter/session.go:528, stsrouter/session.go:345 duration_ms µs / 1000 no
stsrouter/session.go:335 ttfb_ms (also stat.LatencyMs at :471, native turn TTFB at agent/native.go:772), from sts/turn.go:92 Seconds() * 1000 yes, fixed to µs / 1000
agent/streamedge/streamedge.go:187,200,213,214,377,383 µs / 1000 no
agent/speculate.go:402,409, agent/turns.go:703 (msBetween, leg) µs / 1000 no
stt/cartesia, stt/inworld, stt/parakeet ProcessingTimeMs provider or adapter value, not traced unverified
stt/grok/grok.go:514 AudioDurationMs: message.Duration * 1000 float product maybe; it is logged as audio_ms through int64(...) (sttrouter/session.go:515), so it truncates and does not print noise

Validation

  • TestModelCallTimesCarryNoFloatNoise (llmrouter/cancelled_test.go) runs Session.observe with 722 µs of headers, a 1900.014 ms first token and a 1905.014 ms duration (a pair that sums to 1900.7359999999999 and 1905.7359999999999, asserted in the test). It reads the digits of the logged ttft_ms and duration_ms, and compares OnTiming and the stored row to 1900.736 / 1905.736. A second case has no first token: ttft_ms stays 0.
  • TestTheLegsFromSpeechEndCarryNoFloatNoise (agent/turn_latency_test.go) does the same for the two speech-end legs.
  • TestAWaitCarriesNoFloatNoise (sts/sts_test.go) sets a wait of 2235932 µs (Seconds()*1000 gives 2235.9320000000002, asserted) and expects 2235.932 from Turn.Complete; the old form fails it.
  • The llmrouter and agent tests use 722 µs + 1900.014 ms (1900.7359999999999, rounds to 1900.736, truncates to 1900.735), so a Trunc or Floor mutant of RoundMs fails them too.
  • Mutations, each killed: RoundMs as identity, RoundMs to 2 decimals, each of the four call sites unrounded.
  • Run: go test ./internal/llmrouter/... ./internal/routing/... ./internal/agent/... ./internal/chatlog/, go vet with and without -tags integration on the same packages. The full integration run is left to CI.
  • Not checked: staging log lines for a real call (after deploy).

Review

  • Round 1 NO-GO: R1.1 (test pair could not tell rounding from truncating), R1.2 (stsrouter ttfb_ms had the same noise). Both fixed in 6874b48 and e0711bc.
  • Round 2 GO at a8e4657: Trunc, Floor and identity mutants of RoundMs and the old Seconds()*1000 form all fail their tests.
  • Follow-up ticket: grok AudioDurationMs truncation (sttrouter/session.go:515), out of scope here.

🤖 Generated with Claude Code

A float sum of two latencies printed as 1921.7259999999999. The model call's
ttft_ms and duration_ms, and the speech-end legs of a voice turn, are now rounded
once where they are summed, so the log line, the live timing and the stored row agree.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

@kanat kanat left a comment

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

VERDICT: NO-GO

Reviewed 330542d1f merged onto main at 25b4f15d0. The merge was clean. go build, go vet (with and without -tags integration) and the tests for llmrouter, routing, agent (including streamedge) and chatlog all pass on the merged tree. This is a draft, so the go and integration checks are skipped. CI runs at gh pr ready.

[Should fix] R1.2 acceleration/internal/sts/turn.go:92 (not in the diff): the sweep table says stsrouter ttfb_ms is built as µs / 1000 and is clean. It is not. stsrouter/session.go:335 logs complete.TimeToFirstByteMs, which comes from t.firstByteAt.Sub(t.heardAt).Seconds() * 1000. In a probe, that expression gave more than 3 decimals for 266,037 of 1,000,000 durations in whole microseconds (2.235932s gives 2235.9320000000002). The same value is stored as stat.LatencyMs (stsrouter/session.go:471) and becomes the native turn's TTFB (agent/native.go:772). The fix is one line, within the brief's size budget: float64(t.firstByteAt.Sub(t.heardAt).Microseconds()) / 1000, the form ResponseTimeMs already uses two lines below, plus a test. If you leave it out, correct the table row instead.

Checked and sound:

  • Q1: the failure line's duration_ms (session.go:220) needs no change. float64(k)/1000 printed at most 3 decimals for k = 0..2e8 and for 2e7 random k < 1e15.
  • RoundMs of two µs/1000 values equals (a+b)/1000 exactly in 5e7 random pairs.
  • Billing reads neither DurationMs nor LatencyMs. The > 0 checks in routing/stats.go:552,556 cannot flip, because a positive sum of µs values is at least 0.001.
  • RoundMs handles zero and negative values. -0 cannot happen, because headers are clamped to 0 or more.
  • routing already owns MsSince, and no existing µs rounding helper does this job.
  • 16 mutations checked, 2 survived (R1.1).

duration string
latencyMs float64
}{
"a first token": {llm.Response{Status: llm.StatusCompleted, TimeToFirstTokenMs: 1921.004, DurationMs: 1926.004},

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[Should fix] R1.1 For 722 µs + 1921.004 ms, the sum times 1000 is exactly 1921726 in float64, so this case cannot tell rounding from truncating. With RoundMs changed to math.Trunc(ms*1000)/1000 (or Floor), this test and TestTheLegsFromSpeechEndCarryNoFloatNoise both still pass. That version is 1 µs low on 1,166,039 of 10M random µs sums. Fix: add a case whose product lands just below the integer, for example 722 µs of headers + TimeToFirstTokenMs: 1900.014 gives 1900.736 (Trunc gives 1900.735). A scratch probe with that pair passes on this head and fails under the Trunc mutant. The agent test needs a pair like it.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed in 6874b48: the tests now use 722 µs + 1900.014 ms (1900.7359999999999); Round gives 1900.736, and both a Trunc and a Floor mutant of RoundMs fail TestModelCallTimesCarryNoFloatNoise and TestAgentSuite/TestTheLegsFromSpeechEndCarryNoFloatNoise.

kanat and others added 3 commits November 26, 2026 16:02
722 us + 1921.004 ms times 1000 is exactly an integer, so Trunc and Floor passed.
722 us + 1900.014 ms gives 1900.7359999999999: Round yields 1900.736, Trunc 1900.735.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
Seconds()*1000 turned 2.235932 s into 2235.9320000000002 ms; use the
Microseconds()/1000 form ResponseTimeMs uses. Feeds the ttfb_ms log line,
the stored latency and the native turn's TTFB.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
@kanat

kanat commented Nov 26, 2026

Copy link
Copy Markdown
Collaborator Author

R1.2 fixed in e0711bc: sts/turn.go now measures the wait as Microseconds()/1000. Test: TestSTSSuite/TestAWaitCarriesNoFloatNoise (2235932 µs gives 2235.932; the old Seconds()*1000 form fails it with 2235.9320000000002). The sweep table in the description is corrected.

@kanat kanat left a comment

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

VERDICT: GO

Delta round 2 at a8e4657, built and tested merged onto origin/main 53f55b6.

Prior findings

  • R1.1 fixed (6874b48). With the pair 722 us + 1900.014 ms (+1905.014 ms), I mutated routing.RoundMs three ways. math.Trunc, math.Floor and identity each fail both TestModelCallTimesCarryNoFloatNoise (llmrouter) and TestTheLegsFromSpeechEndCarryNoFloatNoise (agent). Trunc and Floor give 1900.735, identity gives 1900.7359999999999.
  • R1.2 fixed (e0711bc). sts/turn.go:92 now uses Microseconds()/1000. Restoring Seconds()*1000 fails TestAWaitCarriesNoFloatNoise (expected 2235.932, actual 2235.9320000000002). A grep of Seconds() * 1000 in non-test acceleration/ finds only the ms helper in stt/sttsuite/sttsuite.go:273, a test-suite package that is not logged. The pattern is covered.
  • R1.Q1 closed. R1.Q2 (grok AudioDurationMs truncation) goes to a ticket after merge.

New findings

  • [Nit] (unchanged code) agent/speaking.go:96 heardBy computes float64(ns)/1e6 for interrupted.HeardMs (an event payload, not a _ms log attribute). It is not a sum of microsecond values. No change asked.

Runs: sts, stsrouter, agent, llmrouter and routing suites pass on the merged head. gofmt is clean. go vet passes with and without -tags integration. This is a draft, so CI skips go and integration (#2130).

@kanat
kanat marked this pull request as ready for review November 26, 2026 21:10
@kanat
kanat merged commit 01295f8 into main Nov 27, 2026
15 of 16 checks passed
@kanat
kanat deleted the kanat/ai-4110-model-call-ms-rounding branch November 27, 2026 01:02
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant