Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions agents/src/agents/harness/pydantic_ai/workers_ai.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -59,6 +60,8 @@

_PROVIDER = "cloudflare"

_NO_PRICES = data_snapshot.DataSnapshot(providers=[], from_auto_update=False)

_OPTIONS = (
"max_tokens",
"temperature",
Expand All @@ -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
Expand Down Expand Up @@ -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,
Expand Down
25 changes: 25 additions & 0 deletions agents/tests/harness/test_pydantic_workers_ai.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Loading