Repository navigation
Description
Problem Description
The spend hook of build_agent (src/devops_cli/ai/client/factory.py) writes one spend record for each model request with duration_seconds 0. Before #4237, record_run_spend wrote the time of the run. The stage, refine and alternatives runs go through the hook, so the ledger has a duration of 0 for all their requests, and the mean duration of a spend report (mean_duration in src/devops_cli/ai/spend/ledger.py) is too low.
Reproduction Steps
- Run an agent of
build_agenton a model that takes 1 second to answer. - Read
duration_secondsof the ledger row: it is 0.0.
Expected vs Actual Behavior
Expected: each record holds the time of its model request.
Actual: each record of the hook holds 0.0.
Environment Diagnostics
origin/main 8fec1f6. Found during #4475.
Logs, Traces, or Terminal Output
track_request_spend(...) in _spend_hook passes no duration_seconds. pydantic-ai Hooks has before_model_request and model_request (a wrap hook), which can time the request.
Pre-Submission Checklist
- I searched open issues.
Proposed design
Problem statement
The spend hook of build_agent writes duration_seconds 0 for each model request. The stage, refine and alternatives runs use this hook, so their ledger rows have no duration. The spend report then shows a mean duration that is too low. The change records the elapsed time of each model request.
Acceptance criteria
Requirement: Model request duration
The spend hook of build_agent MUST measure a model request with a monotonic clock, not wall-clock time.
Scenario: Wall clock moves during a request
- WHEN A test monkeypatches
time.monotonicto return 10.0 before the request and 10.25 after it, then runs an agent frombuild_agenton a fake model. - THEN The newest ledger record has
duration_secondsequal to 0.25.
Requirement: One duration for each request
The spend hook SHALL record one duration for each model request and SHALL NOT reuse an earlier duration.
Scenario: Two model requests in one run
- WHEN A test runs an agent on a fake model that answers two requests with monotonic times 1.0, 1.2, 2.0 and 2.5.
- THEN The two ledger records have
duration_seconds0.2 and 0.5.
Requirement: Failed request has no spend record
The spend hook MUST write no spend record when the model request raises an error.
Scenario: A failed model request
- WHEN A test runs an agent from
build_agenton a fake model that raises an exception. - THEN The ledger holds no new record for that request.
Requirement: Spend fields are preserved
The timed spend hook SHALL keep the provider, model, token counts, cache-read tokens, request type and stage of each record.
Scenario: A request with usage and a stage
- WHEN A test runs an agent from
build_agentwith a fake model that returns usage and a set stage. - THEN The ledger record has the expected provider, model, prompt tokens, completion tokens, cache-read tokens, request type and stage.
Requirement: Mean duration report
The backend breakdown report SHALL compute mean_duration_seconds from the recorded request durations.
Scenario: Two recorded durations
- WHEN A test writes two ledger records for one
served_bywith durations 0.1 and 0.3. - THEN The backend summary reports
mean_duration_seconds0.2.
Existing Tool Considered
No alternatives check has run. pydantic-ai is the open, maintained library that covers the need with its Hooks API. The before hook can store a start time, and the after hook can compute the elapsed seconds. pydantic-ai has no built-in per-request duration field.
Key questions
- Which pydantic-ai hook pair times one model request?: Use
before_model_requestto store a monotonic start time for the request andafter_model_requestto compute the elapsed seconds and calltrack_request_spend. (best practice) [sources: src/devops_cli/ai/client/factory.py:216, src/devops_cli/ai/client/factory.py:250, src/devops_cli/roadmap/implement/implement_limits.py:270]- Chosen: before/after hook pair. The repository already uses both hooks, and the after hook receives the response for the spend fields.
- Rejected: model_request wrap hook. It also times the request, but it turns the spend hook into an async wrapper with no repository example.
- Rejected: timer around Agent.run. It measures the whole run, not each model request.
- How does the hook keep one start time for each request?: Store the start time in a
ContextVarfor the model request, read it in the after hook, and reset it after the record is written. (best practice) [sources: src/devops_cli/ai/client/factory.py:216, src/devops_cli/ai/spend/ledger.py]- Chosen: ContextVar start time. It keeps one value for each request context and needs no shared lock.
- Rejected: module-level float. Concurrent requests overwrite the value.
- Rejected: mapping keyed by request context id. It adds a shared mapping and cleanup for each request.
- What unit and precision does the ledger store for a duration?: The ledger stores seconds as a float and rounds the value to 4 decimal places. [sources: src/devops_cli/ai/spend/ledger.py]
- Which open, maintained library covers the timing need?: pydantic-ai covers the timing need with its
HooksAPI. It has no built-in per-request duration field, so the hook computes the elapsed seconds. (best practice) [sources: src/devops_cli/ai/client/factory.py:250, src/devops_cli/roadmap/implement/implement_limits.py:270]- Chosen: pydantic-ai Hooks. It is already the agent framework and supplies the model request hooks.
- Rejected: custom timer code. It would duplicate the hook path and add code to maintain.
- Rejected: OpenTelemetry span timing. It records traces, not the spend record duration.
- Does a retry get its own duration?: Yes. A retry is a separate model request, so it gets its own spend record and duration. [sources: src/devops_cli/ai/client/factory.py:216, src/devops_cli/ai/client/factory.py:250]
Fit
Fits in one pull request.
Suspected block
None.
Dependencies
None.
Activity
dan-petty commentedon Nov 11, 2026
Intake placed this item to the backlog: intake puts every new item in the backlog. A person plans the milestones.
fixprefix of the title gives the type.(ai)scope of the title gives the scope.Spend report mean_duration is wrong, but the system still functions and no data is lost.Users see an incorrect mean duration in spend reports, which misleads cost-per-second analysis.The fix is to wrap the model request with a timer in the existing hook, a small change in one file.Both fix a missing field in _spend_hook in src/devops_cli/ai/client/factory.py, but #4483 adds cache_read_tokens while this candidate adds duration_seconds.devops-cli-service commentedon Nov 11, 2026
Status set to Ready at 9087b39 (see proposed design in issue body).
devops-cli-service commentedon Nov 11, 2026
Released in v0.112.3: https://github.com/dan-petty/devops-cli/releases/tag/v0.112.3
devops-cli-service commentedon Nov 11, 2026
Live since 2026-11-11 19:36 UTC