feat: track step runtime_ms on chat messages#23219
Merged
Merged
Conversation
Adds a runtime_ms column to chat_messages that records the wall-clock duration (in milliseconds) of each LLM step. This covers LLM streaming, tool execution, and retries — the full time the agent is 'alive' for a step. The value is persisted on assistant-role messages only (tool and system messages get NULL). This aligns with the existing billing pattern where total_cost_micros is stored per assistant message and aggregated with SUM() over time periods. Changes: - Migration: adds nullable runtime_ms bigint to chat_messages. - chatloop: adds Runtime field to PersistedStep, measures time.Since(stepStart) covering stream + tool execution. - chatd: passes step.Runtime.Milliseconds() to the assistant message InsertChatMessage call; all other message types get NULL. - Tests: adds runtime > 0 assertion in chatloop tests.
5ce6d93 to
7435e51
Compare
hugodutka
approved these changes
Mar 18, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to subscribe to this conversation on GitHub.
Already have an account?
Sign in.
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Adds a
runtime_mscolumn tochat_messagesthat records the wall-clock duration (in milliseconds) of each LLM step. This covers LLM streaming, tool execution, and retries — the full time the agent is "alive" for a step.This is the foundation for billing by agent alive time. The column follows the same pattern as
total_cost_micros: stored per assistant message, aggregatable withSUM()over time periods by user.Changes
runtime_ms biginttochat_messages.Runtime time.Durationfield toPersistedStep, measurestime.Since(stepStart)at the beginning of each step (covering stream + tool execution + retries).step.Runtime.Milliseconds()to the assistant messageInsertChatMessagecall; all other message types (system, user, tool) getNULL.runtime > 0assertion in chatloop tests.Billing query pattern
Once ready, aggregation mirrors the existing cost queries: