Skip to content

api-rs: persist usage_records for workflow agent turns - #1338

Open
ordemri wants to merge 3 commits into
paradigmxyz:mainfrom
ordemri:usage-records-workflow-agent-turns
Open

api-rs: persist usage_records for workflow agent turns#1338
ordemri wants to merge 3 commits into
paradigmxyz:mainfrom
ordemri:usage-records-workflow-agent-turns

Conversation

@ordemri

@ordemri ordemri commented Aug 11, 2026

Copy link
Copy Markdown

Summary

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 existing stream rather than
adding a separate accounting mechanism, and persists it as a new usage_records row per
completed 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

  • New migration: usage_records table + index on execution_id.
  • SessionRuntime::pool(): exposes the Postgres pool already backing the runtime's
    session store, so callers that share that store (like workflow-tier turns) can persist
    their own rows without a second connection pool.
  • centaur-workflows: parses usage off turn.completed/turn/completed events in the
    turn's output-line stream and writes one row per turn.

Test plan

  • cargo fmt --all --check
  • cargo clippy --workspace --all-targets -- -D warnings
  • cargo test --workspace (ParadeDB Postgres via start-paradedb-postgres.sh)
  • New test: a workflow agent turn produces exactly one usage_records row with the
    expected token counts, model, and harness.

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

api-rs: workflow agent turns emit no usage records (parity gap vs legacy api)

1 participant