api-rs: persist usage_records for workflow agent turns - #1338
Open
ordemri wants to merge 3 commits into
Open
Conversation
Workflow-tier agent turns (`ctx.agent_turn` in the python workflow host) wrote no usage observation at all, unlike session-tier turns whose harness `turn.completed` events already carry a `usage` object. Workflow turns already receive the same output-line stream session turns do, so this extracts usage from that stream instead of adding a separate accounting path, and persists one `usage_records` row per completed turn (execution_id, thread_key, harness, model, input_tokens, output_tokens). Gated behind the existing telemetry trace-export config, and best-effort: a write failure is logged, never propagated, so usage accounting can't fail a workflow's actual agent turn. Fixes paradigmxyz#1111.
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 join this conversation on GitHub.
Already have an account?
Sign in to comment
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
Workflow-tier agent turns (
ctx.agent_turnin the python workflow host) wrote no usageobservation at all — unlike session-tier turns, whose harness
turn.completedeventsalready carry a
usageobject. Workflow turns already receive the same output-linestream session turns do, so this extracts usage from that existing stream rather than
adding a separate accounting mechanism, and persists it as a new
usage_recordsrow percompleted turn:
{execution_id, thread_key, harness, model, input_tokens, output_tokens}.Gated behind the existing telemetry trace-export config (a no-op when trace export is
off, so it doesn't become a second, independent form of telemetry). Best-effort: a write
failure is logged, never propagated, so usage accounting can't fail a workflow's actual
agent turn.
Fixes #1111.
Changes
usage_recordstable + index onexecution_id.SessionRuntime::pool(): exposes the Postgres pool already backing the runtime'ssession store, so callers that share that store (like workflow-tier turns) can persist
their own rows without a second connection pool.
centaur-workflows: parsesusageoffturn.completed/turn/completedevents in theturn's output-line stream and writes one row per turn.
Test plan
cargo fmt --all --checkcargo clippy --workspace --all-targets -- -D warningscargo test --workspace(ParadeDB Postgres viastart-paradedb-postgres.sh)usage_recordsrow with theexpected token counts, model, and harness.