Skip to content

Commit 8652e93

Browse files
authored
Add operation.cost span attribute to model request spans, rename ModelResponse.price() to .cost() (#2767)
1 parent 0787eac commit 8652e93

File tree

4 files changed

+71
-56
lines changed

4 files changed

+71
-56
lines changed

pydantic_ai_slim/pydantic_ai/messages.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1024,8 +1024,12 @@ class ModelResponse:
10241024
provider_response_id: str | None = None
10251025
"""request ID as specified by the model provider. This can be used to track the specific request to the model."""
10261026

1027-
def price(self) -> genai_types.PriceCalculation:
1028-
"""Calculate the price of the usage.
1027+
@deprecated('`price` is deprecated, use `cost` instead')
1028+
def price(self) -> genai_types.PriceCalculation: # pragma: no cover
1029+
return self.cost()
1030+
1031+
def cost(self) -> genai_types.PriceCalculation:
1032+
"""Calculate the cost of the usage.
10291033
10301034
Uses [`genai-prices`](https://github.com/pydantic/genai-prices).
10311035
"""

pydantic_ai_slim/pydantic_ai/models/instrumented.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -420,10 +420,15 @@ def _record_metrics():
420420
return
421421

422422
self.instrumentation_settings.handle_messages(messages, response, system, span)
423+
try:
424+
cost_attributes = {'operation.cost': float(response.cost().total_price)}
425+
except LookupError:
426+
cost_attributes = {}
423427
span.set_attributes(
424428
{
425429
**response.usage.opentelemetry_attributes(),
426430
'gen_ai.response.model': response_model,
431+
**cost_attributes,
427432
}
428433
)
429434
span.update_name(f'{operation} {request_model}')

tests/models/test_anthropic.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -252,7 +252,7 @@ async def test_async_request_prompt_caching(allow_model_requests: None):
252252
)
253253
last_message = result.all_messages()[-1]
254254
assert isinstance(last_message, ModelResponse)
255-
assert last_message.price().total_price == snapshot(Decimal('0.00003488'))
255+
assert last_message.cost().total_price == snapshot(Decimal('0.00003488'))
256256

257257

258258
async def test_async_request_text_response(allow_model_requests: None):

0 commit comments

Comments
 (0)