Repository navigation
[AI-4110] Round model call times to the microsecond - #2577
Conversation
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
left a comment
There was a problem hiding this comment.
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)/1000printed at most 3 decimals for k = 0..2e8 and for 2e7 random k < 1e15. RoundMsof two µs/1000 values equals(a+b)/1000exactly in 5e7 random pairs.- Billing reads neither DurationMs nor LatencyMs. The
> 0checks inrouting/stats.go:552,556cannot flip, because a positive sum of µs values is at least 0.001. RoundMshandles zero and negative values.-0cannot happen, because headers are clamped to 0 or more.routingalready ownsMsSince, 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}, |
There was a problem hiding this comment.
[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.
There was a problem hiding this comment.
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.
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>
|
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
left a comment
There was a problem hiding this comment.
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.RoundMsthree ways.math.Trunc,math.Floorand identity each fail bothTestModelCallTimesCarryNoFloatNoise(llmrouter) andTestTheLegsFromSpeechEndCarryNoFloatNoise(agent). Trunc and Floor give 1900.735, identity gives 1900.7359999999999. - R1.2 fixed (e0711bc).
sts/turn.go:92now usesMicroseconds()/1000. RestoringSeconds()*1000failsTestAWaitCarriesNoFloatNoise(expected 2235.932, actual 2235.9320000000002). A grep ofSeconds() * 1000in non-test acceleration/ finds only themshelper instt/sttsuite/sttsuite.go:273, a test-suite package that is not logged. The pattern is covered. - R1.Q1 closed. R1.Q2 (grok
AudioDurationMstruncation) goes to a ticket after merge.
New findings
- [Nit] (unchanged code)
agent/speaking.go:96heardBycomputesfloat64(ns)/1e6forinterrupted.HeardMs(an event payload, not a_mslog 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).
Why
The router logs
ttft_msandduration_msof 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 as1921.7259999999999. Nessy shows that string on anagent_model_callsrow. The same sum is handed tollm.CallTiming(OnTiming) and stored asstat.LatencyMs/stat.DurationMs.The failure line (
duration_ms,session.go:245) is not affected: it isfloat64(µs) / 1000with nothing added, and a correctly rounded division prints its shortest decimal (1234567/1000is1234.567). It needs no change.How it works
routing.RoundMssits next torouting.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 inllmrouter/session.goforttftMsanddurationMs.agent/turns.go:speechEndToAudioandspeechEndToAudibleadd the transcriber's latency to a measured leg and are logged invoice turn timing(speech_end_to_audio_ms,speech_end_to_audible_ms), so they are rounded the same way.Sweep: float
_mslog attributes inacceleration/(non-test)llmrouter/session.go:406,407ttft_ms,duration_msagent/turns.go:668,677speech_end_to_audio_ms,speech_end_to_audible_msllmrouter/session.go:220failureduration_msµs / 1000llm/stream.go:578,614TimeToFirstTokenMs,DurationMsµs / 1000ttsrouter/session.go:400,512,514,sttrouter/session.go:528,stsrouter/session.go:345duration_msµs / 1000stsrouter/session.go:335ttfb_ms(alsostat.LatencyMsat :471, native turn TTFB atagent/native.go:772), fromsts/turn.go:92Seconds() * 1000µs / 1000agent/streamedge/streamedge.go:187,200,213,214,377,383µs / 1000agent/speculate.go:402,409,agent/turns.go:703(msBetween,leg)µs / 1000stt/cartesia,stt/inworld,stt/parakeetProcessingTimeMsstt/grok/grok.go:514AudioDurationMs: message.Duration * 1000audio_msthroughint64(...)(sttrouter/session.go:515), so it truncates and does not print noiseValidation
TestModelCallTimesCarryNoFloatNoise(llmrouter/cancelled_test.go) runsSession.observewith 722 µs of headers, a 1900.014 ms first token and a 1905.014 ms duration (a pair that sums to1900.7359999999999and1905.7359999999999, asserted in the test). It reads the digits of the loggedttft_msandduration_ms, and comparesOnTimingand the stored row to1900.736/1905.736. A second case has no first token:ttft_msstays 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()*1000gives2235.9320000000002, asserted) and expects2235.932fromTurn.Complete; the old form fails it.1900.7359999999999, rounds to1900.736, truncates to1900.735), so aTruncorFloormutant ofRoundMsfails them too.RoundMsas identity,RoundMsto 2 decimals, each of the four call sites unrounded.go test ./internal/llmrouter/... ./internal/routing/... ./internal/agent/... ./internal/chatlog/,go vetwith and without-tags integrationon the same packages. The full integration run is left to CI.Review
ttfb_mshad the same noise). Both fixed in 6874b48 and e0711bc.Trunc,Floorand identity mutants ofRoundMsand the oldSeconds()*1000form all fail their tests.AudioDurationMstruncation (sttrouter/session.go:515), out of scope here.🤖 Generated with Claude Code