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
8 changes: 6 additions & 2 deletions pydantic_ai_slim/pydantic_ai/messages.py
Original file line number Diff line number Diff line change
Expand Up @@ -1024,8 +1024,12 @@ class ModelResponse:
provider_response_id: str | None = None
"""request ID as specified by the model provider. This can be used to track the specific request to the model."""

def price(self) -> genai_types.PriceCalculation:
"""Calculate the price of the usage.
@deprecated('`price` is deprecated, use `cost` instead')
def price(self) -> genai_types.PriceCalculation: # pragma: no cover
return self.cost()

def cost(self) -> genai_types.PriceCalculation:
"""Calculate the cost of the usage.

Uses [`genai-prices`](https://github.com/pydantic/genai-prices).
"""
Expand Down
5 changes: 5 additions & 0 deletions pydantic_ai_slim/pydantic_ai/models/instrumented.py
Original file line number Diff line number Diff line change
Expand Up @@ -420,10 +420,15 @@ def _record_metrics():
return

self.instrumentation_settings.handle_messages(messages, response, system, span)
try:
cost_attributes = {'operation.cost': float(response.cost().total_price)}
except LookupError:
cost_attributes = {}
span.set_attributes(
{
**response.usage.opentelemetry_attributes(),
'gen_ai.response.model': response_model,
**cost_attributes,
}
)
span.update_name(f'{operation} {request_model}')
Expand Down
2 changes: 1 addition & 1 deletion tests/models/test_anthropic.py
Original file line number Diff line number Diff line change
Expand Up @@ -252,7 +252,7 @@ async def test_async_request_prompt_caching(allow_model_requests: None):
)
last_message = result.all_messages()[-1]
assert isinstance(last_message, ModelResponse)
assert last_message.price().total_price == snapshot(Decimal('0.00003488'))
assert last_message.cost().total_price == snapshot(Decimal('0.00003488'))


async def test_async_request_text_response(allow_model_requests: None):
Expand Down
Loading