langchain.callbacks.manager.get_openai_callback¶
langchain.callbacks.manager.get_openai_callback() → Generator[OpenAICallbackHandler, None, None][source]¶
Get the OpenAI callback handler in a context manager.
which conveniently exposes token and cost information.
Returns
The OpenAI callback handler.
Return type
OpenAICallbackHandler
Example
>>> with get_openai_callback() as cb:
...     # Use the OpenAI callback handler
Examples using get_openai_callback¶
Azure
Token counting
Tracking token usage
Run arbitrary functions