Skip to content

Capture operation.cost for OpenAI embeddings#1843

Merged
alexmojaki merged 20 commits intomainfrom
alex/gen-ai-usage-tracking-spec
Apr 8, 2026
Merged

Capture operation.cost for OpenAI embeddings#1843
alexmojaki merged 20 commits intomainfrom
alex/gen-ai-usage-tracking-spec

Conversation

@alexmojaki
Copy link
Copy Markdown
Collaborator

Summary

  • Extracts shared get_usage_attributes() function into new usage.py that handles all four usage span attributes (input_tokens, output_tokens, usage.raw, operation.cost) uniformly
  • Refactors OpenAI on_response() to call the shared function instead of inline logic
  • Fixes embeddings cost calculation — was passing api_flavor='chat' (silently failed), now correctly passes 'embeddings'
  • Error isolation: cost calculation failure doesn't prevent token/raw attributes from being set

Spec is in specs/gen-ai-usage-refactor/. This is phase 1 of a multi-phase effort to add usage tracking to all GenAI spans (streaming, Anthropic).

Test plan

  • All 61 OpenAI integration tests pass
  • All 22 Anthropic integration tests pass (unaffected)
  • pyright clean
  • Embedding tests now show operation.cost (bug fix)
  • CI passes

🤖 Generated with Claude Code

@cloudflare-workers-and-pages
Copy link
Copy Markdown

cloudflare-workers-and-pages bot commented Apr 8, 2026

Deploying logfire-docs with  Cloudflare Pages  Cloudflare Pages

Latest commit: 29f09c7
Status: ✅  Deploy successful!
Preview URL: https://ff3e4cbd.logfire-docs.pages.dev
Branch Preview URL: https://alex-gen-ai-usage-tracking-s.logfire-docs.pages.dev

View logs

devin-ai-integration[bot]

This comment was marked as resolved.

Copy link
Copy Markdown
Contributor

@devin-ai-integration devin-ai-integration bot left a comment

Choose a reason for hiding this comment

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

Devin Review found 0 new potential issues.

View 7 additional findings in Devin Review.

Open in Devin Review

devin-ai-integration[bot]

This comment was marked as resolved.

…/logfire into alex/gen-ai-usage-tracking-spec
Copy link
Copy Markdown
Contributor

@devin-ai-integration devin-ai-integration bot left a comment

Choose a reason for hiding this comment

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

Devin Review found 0 new potential issues.

View 8 additional findings in Devin Review.

Open in Devin Review

@alexmojaki alexmojaki changed the title Refactor OpenAI usage attributes into shared code Capture operation.cost for OpenAI embeddings Apr 8, 2026
Copy link
Copy Markdown
Contributor

@devin-ai-integration devin-ai-integration bot left a comment

Choose a reason for hiding this comment

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

Devin Review found 0 new potential issues.

View 8 additional findings in Devin Review.

Open in Devin Review

@alexmojaki alexmojaki merged commit 259167d into main Apr 8, 2026
17 checks passed
@alexmojaki alexmojaki deleted the alex/gen-ai-usage-tracking-spec branch April 8, 2026 15:43
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.

1 participant