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

Community : Add OpenAI prompt caching and reasoning tokens tracking #27135

Conversation

vignesh14052002
Copy link
Contributor

Added Token tracking for OpenAI's prompt caching and reasoning tokens
Costs updated from https://openai.com/api/pricing/

usage example

from langchain_community.callbacks import get_openai_callback
from langchain_openai import ChatOpenAI
llm = ChatOpenAI(model_name="o1-mini",temperature=1)

with get_openai_callback() as cb:
    response = llm.invoke("hi "*1500)
    print(cb)

Output

Tokens Used: 1720
	Prompt Tokens: 1508
		Prompt Tokens Cached: 1408
	Completion Tokens: 212
		Reasoning Tokens: 192
Successful Requests: 1
Total Cost (USD): $0.0049559999999999995

Copy link

vercel bot commented Oct 5, 2024

The latest updates on your projects. Learn more about Vercel for Git ↗︎

1 Skipped Deployment
Name Status Preview Comments Updated (UTC)
langchain ⬜️ Ignored (Inspect) Visit Preview Dec 18, 2024 4:24pm

@dosubot dosubot bot added size:M This PR changes 30-99 lines, ignoring generated files. community Related to langchain-community labels Oct 5, 2024
@vignesh14052002
Copy link
Contributor Author

Its been a week, can you review this? @baskaryan @efriis @eyurtsev

Copy link
Member

@efriis efriis left a comment

Choose a reason for hiding this comment

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

unfortunately this is a breaking change due to the removal of is_completion. Will wait for @baskaryan to return from vacation to think about how we want to handle.

@vignesh14052002
Copy link
Contributor Author

unfortunately this is a breaking change due to the removal of is_completion. Will wait for @baskaryan to return from vacation to think about how we want to handle.

@efriis will it be okay, if I revert it and use another Boolean is_cached_prompt ?

@zep-hyr
Copy link
Contributor

zep-hyr commented Oct 23, 2024

@efriis @baskaryan I am waiting for this PR to be processed. Could you provide an update on its progress?
cc. @vignesh14052002

# Conflicts:
#	libs/community/langchain_community/callbacks/openai_info.py
@ccurme ccurme assigned ccurme and unassigned baskaryan Dec 18, 2024
@dosubot dosubot bot added size:L This PR changes 100-499 lines, ignoring generated files. and removed size:M This PR changes 30-99 lines, ignoring generated files. labels Dec 18, 2024
Copy link
Collaborator

@ccurme ccurme left a comment

Choose a reason for hiding this comment

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

@baskaryan @efriis I made some modifications here such that:

  • Token type (prompt, completion, cached) is represented with an enum.
  • is_completion is deprecated in favor of this enum. Passing is_completion=True will generate a warning.

Warnings will only be generated for users if they are using get_openai_token_cost_for_model or standardize_model_name with is_completion=True directly. These functions are not in our docs. Using the callback as documented won't generate a deprecation warning.

lmk if any thoughts.

@dosubot dosubot bot added the lgtm PR looks good. Use to confirm that a PR is ready for merging. label Dec 18, 2024
@ccurme ccurme merged commit 4c9acdf into langchain-ai:master Dec 19, 2024
19 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
community Related to langchain-community lgtm PR looks good. Use to confirm that a PR is ready for merging. size:L This PR changes 100-499 lines, ignoring generated files.
Projects
Archived in project
Development

Successfully merging this pull request may close these issues.

5 participants