Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(cost-tracking): trace all usage keys #1021

Merged
merged 11 commits into from
Dec 16, 2024
Merged

feat(cost-tracking): trace all usage keys #1021

merged 11 commits into from
Dec 16, 2024

Conversation

hassiebp
Copy link
Contributor

@hassiebp hassiebp commented Dec 4, 2024

  • update fern
  • add token detail extraction
  • update openai integration

Important

Enhances cost-tracking by introducing detailed usage and cost tracking fields, updating OpenAI integration, and improving type safety with enum usage.

  • Behavior:
    • Introduces usage_details and cost_details fields in Observation, ObservationsView, and Usage classes for detailed usage and cost tracking.
    • Deprecates old usage field in favor of usage_details and cost_details.
    • Updates OpenAI integration to handle new usage and cost tracking.
  • Models:
    • Adds OpenAiUsageSchema and UsageDetails for detailed usage tracking.
    • Updates MediaContentType to use an enum for better type safety.
  • Misc:
    • Increases comment content limit from 500 to 3000 characters in create_comment_request.py.
    • Updates langfuse/client.py and langfuse/decorators/langfuse_decorator.py to support new usage tracking.
    • Updates callback handlers in langfuse/callback/langchain.py and langfuse/llama_index/_event_handler.py to utilize new usage details.

This description was created by Ellipsis for 54fac8e. It will automatically update as commits are pushed.

@hassiebp hassiebp linked an issue Dec 9, 2024 that may be closed by this pull request
@hassiebp hassiebp marked this pull request as ready for review December 13, 2024 08:47
Copy link

@greptile-apps greptile-apps bot left a comment

Choose a reason for hiding this comment

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

Disclaimer: Experimental PR review

PR Summary

This PR enhances cost tracking capabilities across the Langfuse Python SDK by introducing more granular usage and cost monitoring features.

  • Introduces new usage_details and cost_details fields in key classes while deprecating older usage field for more detailed token and cost tracking
  • Adds OpenAiUsageSchema for structured token usage tracking with prompt/completion token details
  • Increases comment content character limit from 500 to 3000 in CreateCommentRequest
  • Updates LangChain and LlamaIndex handlers to support granular token usage tracking from various LLM providers
  • Maintains backward compatibility for V2 self-hosters while introducing new cost tracking features

20 file(s) reviewed, 1 comment(s)
Edit PR Review Bot Settings | Greptile

APPLICATION_XML = "application/xml"
APPLICATION_OCTET_STREAM = "application/octet-stream"

def visit(
Copy link

Choose a reason for hiding this comment

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

logic: visit() method lacks exhaustive check or default case. Consider adding a default handler or raising NotImplementedError for unhandled cases to prevent silent failures if new enum values are added.

Copy link

@greptile-apps greptile-apps bot left a comment

Choose a reason for hiding this comment

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

Disclaimer: Experimental PR review

PR Summary

(updates since last review)

This PR continues to enhance the cost tracking implementation with additional changes focused on the OpenAI integration and usage tracking. Here's a summary of the latest changes:

  • Added new OpenAiUsageSchema class in /ingestion/types/open_ai_usage_schema.py with detailed token tracking fields for prompt and completion tokens
  • Updated OpenAI integration in openai.py to handle both old and new usage formats for backward compatibility
  • Enhanced error handling in OpenAI integration to include zero-value cost details for error cases
  • Added proper type safety with enum usage for MediaContentType instead of string literals
  • Improved token parsing in LangChain handler to support various LLM provider formats (Anthropic, Bedrock, Vertex AI, IBM)

20 file(s) reviewed, 2 comment(s)
Edit PR Review Bot Settings | Greptile

class OpenAiUsageSchema(pydantic_v1.BaseModel):
prompt_tokens: int
completion_tokens: int
total_tokens: int
Copy link

Choose a reason for hiding this comment

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

logic: total_tokens should validate that it equals prompt_tokens + completion_tokens

Comment on lines 120 to 122
self._get_generation_client(event.span_id).update(
usage=usage, end_time=_get_timestamp()
usage=usage, usage_details=usage, end_time=_get_timestamp()
)
Copy link

Choose a reason for hiding this comment

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

logic: passing same 'usage' value to both usage and usage_details fields may cause issues with backward compatibility since usage field is deprecated

@hassiebp hassiebp merged commit cf9ec3e into main Dec 16, 2024
10 of 11 checks passed
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.

bug: Missing cached token pricing column from models
1 participant