Skip to content

fix(ai): agent requests record no duration in the ledger #4504

Closed

Description

@dan-petty

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

  1. Run an agent of build_agent on a model that takes 1 second to answer.
  2. Read duration_seconds of 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.monotonic to return 10.0 before the request and 10.25 after it, then runs an agent from build_agent on a fake model.
  • THEN The newest ledger record has duration_seconds equal 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_seconds 0.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_agent on 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_agent with 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_by with durations 0.1 and 0.3.
  • THEN The backend summary reports mean_duration_seconds 0.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_request to store a monotonic start time for the request and after_model_request to compute the elapsed seconds and call track_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 ContextVar for 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 Hooks API. 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

  1. added
    source/agent
    type/bug
    scope/ai
    status/new
    value/medium
    effort/low
    on Nov 11, 2026
  2. dan-petty commented on Nov 11, 2026

    @dan-petty
    OwnerAuthor

    Intake placed this item to the backlog: intake puts every new item in the backlog. A person plans the milestones.

    • Type: type/bug. The fix prefix of the title gives the type.
    • Scope: scope/ai. The (ai) scope of the title gives the scope.
    • Priority: P2-Medium. Spend report mean_duration is wrong, but the system still functions and no data is lost.
    • Value: Medium. Users see an incorrect mean duration in spend reports, which misleads cost-per-second analysis.
    • Effort: Low. The fix is to wrap the model request with a timer in the existing hook, a small change in one file.
    • Related: fix(ai): price cached prompt tokens of build_agent runs #4483, which asks for a different change. 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.
  3. added
    priority/p2-medium
    on Nov 11, 2026
  4. added
    status/ready
    and removed
    status/new
    on Nov 11, 2026
  5. devops-cli-service commented on Nov 11, 2026

    @devops-cli-service
    Contributor

    Status set to Ready at 9087b39 (see proposed design in issue body).

  6. added
    owner/agent
    status/in-progress
    status/in-review
    and removed
    status/ready
    status/in-progress
    on Nov 11, 2026
  7. removed
    status/in-review
    owner/agent
    on Nov 11, 2026
  8. devops-cli-service commented on Nov 11, 2026

    @devops-cli-service
    Contributor
  9. devops-cli-service commented on Nov 11, 2026

    @devops-cli-service
    Contributor

    Live since 2026-11-11 19:36 UTC

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    effort/lowpriority/p2-mediumscope/aisource/agenttype/bugvalue/medium

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions