Skip to content

refactor(ai): replace PydanticAgent with pydantic-ai Agent - #4237

Merged
dan-petty merged 5 commits into
mainfrom
refactor/2489-replace-pydanticagent-with-pydantic-ai-agent
Nov 9, 2026
Merged

dan-petty merged 5 commits into
mainfrom
refactor/2489-replace-pydanticagent-with-pydantic-ai-agent

Conversation

@dan-petty

Copy link
Copy Markdown
Owner

Replace the hand-rolled agent loop and memory classes with native pydantic-ai Agent. Centralize agent construction in build_agent with per-request spend tracking.

Closes #2489

Change: openspec/changes/archive/2026-11-09-2489-replace-pydanticagent-with-pydantic-ai-agent/

@dan-petty dan-petty added type/refactor Type refactor: a new structure, same behavior scope/ai AI review pipeline, personas, prompts, LLM client, or RAG labels Nov 9, 2026
@dan-petty
dan-petty force-pushed the refactor/2489-replace-pydanticagent-with-pydantic-ai-agent branch from f79d575 to aaaf059 Compare November 9, 2026 16:35
@dan-petty
dan-petty marked this pull request as ready for review November 9, 2026 16:56
@dan-petty
dan-petty merged commit 5edd7a5 into main Nov 9, 2026
26 checks passed
@dan-petty
dan-petty deleted the refactor/2489-replace-pydanticagent-with-pydantic-ai-agent branch November 9, 2026 16:57
dan-petty pushed a commit that referenced this pull request Nov 9, 2026
🤖 I have created a release *beep* *boop*
---


##
[0.101.1](v0.101.0...v0.101.1)
(2026-11-09)


### Bug Fixes

* **devcontainer:** a rejected unlock password becomes the keyring
password ([#4249](#4249))
([cc3d22e](cc3d22e))
* **roadmap:** count the reported input in the run window estimate
([#4238](#4238))
([af9c27a](af9c27a))
* **roadmap:** send one tool call at a time to a Claude group
([#4243](#4243))
([b855601](b855601))


### Code Refactoring

* **ai:** replace PydanticAgent with pydantic-ai Agent
([#4237](#4237))
([5edd7a5](5edd7a5))

---
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>
dan-petty added a commit that referenced this pull request Nov 11, 2026
The spend hook of build_agent writes one spend record for each model
request since #4237, but it adds no count to
devops_cli_ai_model_answers_total and gives the ledger no cached prompt
tokens. The stage, refine and alternatives runs go through that hook, so
the Service sends no answer count after the rollout of #4237. No AI
series names the job row that sent a request.

The hook now counts each reply that names its model and passes the
cached prompt tokens. model_answer_labels holds the fallback rule and
the row for the hook and for record_run_spend. track_request_spend adds
the row of request_scope to the request, token and spend series, and the
runner Job applies its plan in the scope of the implement row.
dan-petty added a commit that referenced this pull request Nov 11, 2026
Since #4237, the stage, refine and alternatives runs send no
`devops_cli_ai_model_answers_total`. The hook of `build_agent` writes
one spend record for each request, but adds no answer count and passes
no cached prompt tokens, so a cached prompt costs the full input rate.
No AI series names the job row of a request.

- The hook counts each reply that names its model and passes
`cache_read_tokens`.
- `model_answer_labels` holds the fallback rule and the row for the hook
and `record_run_spend`.
- `track_request_spend` adds `row` (the `request_scope` row or `other`)
to the request, token and spend series.
- The runner Job applies its plan in the scope of the row `implement`.

Only the implement step calls `record_run_spend`, and its agent has no
hook, so a request counts one time.

Closes #4475

Change:
`openspec/changes/archive/2026-11-11-4475-stage-and-refine-send-no-answer/`
dan-petty added a commit that referenced this pull request Nov 11, 2026
## 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/`
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/refactor Type refactor: a new structure, same behavior

Projects

None yet

Development

Successfully merging this pull request may close these issues.

refactor(ai): Replace PydanticAgent with pydantic-ai Agent

1 participant