Skip to content

Commit 40a0008

Browse files
committed
Add operation.cost span attribute
1 parent 66fa21a commit 40a0008

File tree

2 files changed

+64
-53
lines changed

2 files changed

+64
-53
lines changed

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.price().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}')

0 commit comments

Comments
 (0)