From 1ce1451828fe0b582fcb0490f75f2dc86762d3ab Mon Sep 17 00:00:00 2001 From: Viswa Marepalli Date: Thu, 12 Nov 2026 12:53:42 +0000 Subject: [PATCH] fix(agents): keep WorkersAIModel inside the Durable Object memory limit pydantic-ai's Model.__init__ calls preload_pricing_data(), which imports genai_prices.data and grows the Pyodide heap from 65 MB to 136 MB. That is over the 128 MB Durable Object limit, so every PydanticAIHarness turn on Cloudflare reset the object. WorkersAIModel now installs an empty price snapshot unless the app set its own, so usage.cost stays None. Co-Authored-By: Claude Opus 5.5 --- .../agents/harness/pydantic_ai/workers_ai.py | 12 +++++++++ .../tests/harness/test_pydantic_workers_ai.py | 25 +++++++++++++++++++ 2 files changed, 37 insertions(+) diff --git a/agents/src/agents/harness/pydantic_ai/workers_ai.py b/agents/src/agents/harness/pydantic_ai/workers_ai.py index 397a237..8d2bd5b 100644 --- a/agents/src/agents/harness/pydantic_ai/workers_ai.py +++ b/agents/src/agents/harness/pydantic_ai/workers_ai.py @@ -13,6 +13,7 @@ from datetime import UTC, datetime from typing import TYPE_CHECKING, Any, ClassVar, Literal +from genai_prices import data_snapshot from pydantic_ai import ( BinaryContent, CachePoint, @@ -59,6 +60,8 @@ _PROVIDER = "cloudflare" +_NO_PRICES = data_snapshot.DataSnapshot(providers=[], from_auto_update=False) + _OPTIONS = ( "max_tokens", "temperature", @@ -81,6 +84,11 @@ class WorkersAIModel(workers_ai.WorkersAIModel, Model): and tool calls token by token, and skip whitespace-only text ahead of the answer. + Building one turns off Pydantic AI's price lookup for the isolate, so + ``usage.cost`` stays ``None``: the price table doesn't fit in a Durable + Object's memory. Call ``genai_prices.data_snapshot.set_custom_snapshot`` + first to keep your own prices. + Parameters ---------- name @@ -119,6 +127,10 @@ def __init__( profile: ModelProfileSpec | None = None, clean_text: Callable[[str], str] | None = None, ) -> None: + # Pydantic AI loads genai-prices' bundled price table in Model.__init__ + # and on every answer. It takes ~70 MB of a Durable Object's 128 MB. + if data_snapshot._custom_snapshot is None: + data_snapshot.set_custom_snapshot(_NO_PRICES) super().__init__( name, binding=binding, diff --git a/agents/tests/harness/test_pydantic_workers_ai.py b/agents/tests/harness/test_pydantic_workers_ai.py index 035bdf6..1a76d3a 100644 --- a/agents/tests/harness/test_pydantic_workers_ai.py +++ b/agents/tests/harness/test_pydantic_workers_ai.py @@ -357,3 +357,28 @@ def _text_deltas(events: list[object]) -> list[str]: for event in events if isinstance(event, ChunkEvent) and isinstance(event.chunk, TextDelta) ] + + +def test_building_the_model_skips_the_price_table( + monkeypatch: pytest.MonkeyPatch, +) -> None: + from genai_prices import data_snapshot + + monkeypatch.setattr(data_snapshot, "_custom_snapshot", None) + data_snapshot._bundled_snapshot.cache_clear() + WorkersAIModel("@cf/model", binding=FakeAI()) + + assert data_snapshot.get_snapshot().providers == [] + assert data_snapshot._bundled_snapshot.cache_info().currsize == 0 + + +def test_building_the_model_keeps_a_custom_price_table( + monkeypatch: pytest.MonkeyPatch, +) -> None: + from genai_prices import data_snapshot + + mine = data_snapshot.DataSnapshot(providers=[], from_auto_update=True) + monkeypatch.setattr(data_snapshot, "_custom_snapshot", mine) + WorkersAIModel("@cf/model", binding=FakeAI()) + + assert data_snapshot.get_snapshot() is mine