Skip to content

fix(ai): record the duration of each agent model request - #4717

Merged
dan-petty merged 2 commits into
mainfrom
fix/4504-agent-request-duration
Nov 11, 2026
Merged

dan-petty merged 2 commits into
mainfrom
fix/4504-agent-request-duration

Conversation

@dan-petty

Copy link
Copy Markdown
Owner

What

The spend hook of build_agent records the seconds of each model request in duration_seconds. A before hook keeps the monotonic start time in a ContextVar. The after hook writes the record with the seconds since then. A request that raises runs no after hook, so it writes no record.

Why

Since #4237 the stage, refine and alternatives runs write their records through this hook. It passed no duration, so every record held 0.0 and the mean duration of a spend report was too low.

Both hooks are async def: pydantic-ai runs a plain def hook in an executor, where the ContextVar would not reach the after hook. The record is written with asyncio.to_thread, so the SQLite write stays off the event loop.

Closes #4504
Change: openspec/changes/archive/2026-11-11-4504-agent-requests-record-no-duration-in/

The spend hook of build_agent wrote duration_seconds 0 for each request since #4237. It now times each request with a monotonic clock in a before and after hook pair.

Closes #4504
@dan-petty dan-petty added type/bug Type bug: the code does not do what it must scope/ai AI review pipeline, personas, prompts, LLM client, or RAG labels Nov 11, 2026
@dan-petty

Copy link
Copy Markdown
Owner Author

devops ci fail-to-pass --base origin/main: 4 tests fail on base and pass on the change (test_a_request_records_the_time_of_its_model_request, test_each_request_of_a_run_records_its_own_duration, test_a_failed_model_request_writes_no_record_and_the_next_request_is_timed_from_its_start, test_the_timed_record_keeps_the_fields_of_the_request). On base they fail because the hook has no clock. The 6 other tests of the file pass on both.

Local run after the change: tests/ai/client, tests/roadmap/refine and tests/telemetry/test_library_spans.py give 911 passed; tests/ai/client/test_agent_factory.py and tests/ai/spend give 158 passed.

Not in this change: the backend breakdown of the spend report lists only rows with served_by, and the hook passes none. That is #4716.

@dan-petty
dan-petty marked this pull request as ready for review November 11, 2026 18:00
@dan-petty
dan-petty merged commit 1930b71 into main Nov 11, 2026
32 checks passed
@dan-petty
dan-petty deleted the fix/4504-agent-request-duration branch November 11, 2026 18:03
dan-petty pushed a commit that referenced this pull request Nov 11, 2026
🤖 I have created a release *beep* *boop*
---


##
[0.112.3](v0.112.2...v0.112.3)
(2026-11-11)


### Bug Fixes

* **ai:** record the duration of each agent model request
([#4717](#4717))
([1930b71](1930b71))
* **devcontainer:** poll the Docker daemon through the clock
([#4721](#4721))
([be32268](be32268))
* **roadmap:** confine repair fix turns to the plan files
([#4689](#4689))
([d2e7597](d2e7597))
* **roadmap:** name the nearest test of a module
([#4696](#4696))
([b746c95](b746c95))
* **roadmap:** scoped intake reads a bounded page count
([#4740](#4740))
([f89ccc5](f89ccc5))
* **roadmap:** stage context reads runner facts from the package
([#4723](#4723))
([05e09dc](05e09dc))
* **roadmap:** stage holds an item where openspec is not set up
([#4749](#4749))
([45bd54a](45bd54a))
* **roadmap:** trace gate reads a tool call citation
([#4694](#4694))
([5c71ad4](5c71ad4))


### Performance Improvements

* **ci:** cancel superseded CodeQL runs and idle triggers
([#4763](#4763))
([c951368](c951368))


### Code Refactoring

* **ai:** delete orphan prompts and the prompt fallbacks
([#4710](#4710))
([38a1b13](38a1b13))

---
This PR was generated with [Release
Please](https://github.com/googleapis/release-please). See
[documentation](https://github.com/googleapis/release-please#release-please).

Co-authored-by: dan-petty-release[bot] <346998822+dan-petty-release[bot]@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

scope/ai AI review pipeline, personas, prompts, LLM client, or RAG type/bug Type bug: the code does not do what it must

Projects

None yet

Development

Successfully merging this pull request may close these issues.

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

1 participant