Skip to content

Conversation

alexmojaki
Copy link
Contributor

@alexmojaki alexmojaki commented Sep 2, 2025

This gives the cost of a request in USD as calculated by genai-prices, if possible.

ModelResponse.price() is now deprecated in favour of ModelResponse.cost() which does the same thing.

Copy link

github-actions bot commented Sep 2, 2025

Docs Preview

commit: 0c19a2b
Preview URL: https://baebaf74-pydantic-ai-previews.pydantic.workers.dev

@alexmojaki alexmojaki marked this pull request as ready for review September 2, 2025 17:27
@alexmojaki alexmojaki requested a review from Copilot September 2, 2025 17:27
@alexmojaki alexmojaki changed the title Add operation.cost span attribute Add operation.cost span attribute to model request spans Sep 2, 2025
Copy link

@Copilot Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

This PR adds support for tracking operation costs in OpenTelemetry spans by including an operation.cost attribute when pricing information is available for AI model requests.

  • Adds operation.cost span attribute to track the financial cost of AI model operations
  • Updates test fixtures to use realistic OpenAI models with known pricing (gpt-4o variants)
  • Handles pricing lookup errors gracefully by omitting the cost attribute when pricing is unavailable

Reviewed Changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 1 comment.

File Description
pydantic_ai_slim/pydantic_ai/models/instrumented.py Adds cost tracking logic with error handling for pricing lookups
tests/models/test_instrumented.py Updates test model names and adds expected cost attributes to test assertions

Tip: Customize your code reviews with copilot-instructions.md. Create the file or learn how to get started.

@DouweM DouweM self-assigned this Sep 2, 2025
@alexmojaki alexmojaki changed the title Add operation.cost span attribute to model request spans Add operation.cost span attribute to model request spans, rename ModelResponse.price() to .cost() Sep 3, 2025
@alexmojaki alexmojaki merged commit 8652e93 into main Sep 4, 2025
60 of 62 checks passed
@alexmojaki alexmojaki deleted the alex/operation.cost branch September 4, 2025 09:31
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants