Skip to main contentü¶úÔ∏èüîó LangChainDocsUse casesIntegrationsAPICommunityChat our docsLangSmithJS/TS DocsSearchCTRLKGet startedIntroductionInstallationQuickstartLangChain Expression LanguageInterfaceHow toCookbookLangChain Expression Language (LCEL)Why use LCEL?ModulesModel I/‚ÄãOPromptsLanguage modelsLLMsAsync APICustom LLMFake LLMHuman input LLMCachingSerializationStreamingTracking token usageChat modelsOutput parsersRetrievalChainsMemoryAgentsCallbacksModulesSecurityGuidesMoreModulesModel I/‚ÄãOLanguage modelsLLMsTracking token usageTracking token usageThis notebook goes over how to track your token usage for specific calls. It is currently only implemented for the OpenAI API.Let's first look at an extremely simple example of tracking token usage for a single LLM call.from langchain.llms import OpenAIfrom langchain.callbacks import get_openai_callbackllm = OpenAI(model_name="text-davinci-002", n=2, best_of=2)with get_openai_callback() as cb:    result = llm("Tell me a joke")    print(cb)    Tokens Used: 42        Prompt Tokens: 4        Completion Tokens: 38    Successful Requests: 1    Total Cost (USD): $0.00084Anything inside the context manager will get tracked. Here's an example of using it to track multiple calls in sequence.with get_openai_callback() as cb:    result = llm("Tell me a joke")    result2 = llm("Tell me a joke")    print(cb.total_tokens)    91If a chain or agent with multiple steps in it is used, it will track all those steps.from langchain.agents import load_toolsfrom langchain.agents import initialize_agentfrom langchain.agents import AgentTypefrom langchain.llms import OpenAIllm = OpenAI(temperature=0)tools = load_tools(["serpapi", "llm-math"], llm=llm)agent = initialize_agent(    tools, llm, agent=AgentType.ZERO_SHOT_REACT_DESCRIPTION, verbose=True)with get_openai_callback() as cb:    response = agent.run(        "Who is Olivia Wilde's boyfriend? What is his current age raised to the 0.23 power?"    )    print(f"Total Tokens:
This notebook goes over how to track your token usage for specific calls. It is currently only implemented for the OpenAI API.
This notebook goes over how to track your token usage for specific calls. It is currently only implemented for the OpenAI API. ->: Skip to main contentü¶úÔ∏èüîó LangChainDocsUse casesIntegrationsAPICommunityChat our docsLangSmithJS/TS DocsSearchCTRLKGet startedIntroductionInstallationQuickstartLangChain Expression LanguageInterfaceHow toCookbookLangChain Expression Language (LCEL)Why use LCEL?ModulesModel I/‚ÄãOPromptsLanguage modelsLLMsAsync APICustom LLMFake LLMHuman input LLMCachingSerializationStreamingTracking token usageChat modelsOutput parsersRetrievalChainsMemoryAgentsCallbacksModulesSecurityGuidesMoreModulesModel I/‚ÄãOLanguage modelsLLMsTracking token usageTracking token usageThis notebook goes over how to track your token usage for specific calls. It is currently only implemented for the OpenAI API.Let's first look at an extremely simple example of tracking token usage for a single LLM call.from langchain.llms import OpenAIfrom langchain.callbacks import get_openai_callbackllm = OpenAI(model_name="text-davinci-002", n=2, best_of=2)with get_openai_callback() as cb:    result = llm("Tell me a joke")    print(cb)    Tokens Used: 42        Prompt Tokens: 4        Completion Tokens: 38    Successful Requests: 1    Total Cost (USD): $0.00084Anything inside the context manager will get tracked. Here's an example of using it to track multiple calls in sequence.with get_openai_callback() as cb:    result = llm("Tell me a joke")    result2 = llm("Tell me a joke")    print(cb.total_tokens)    91If a chain or agent with multiple steps in it is used, it will track all those steps.from langchain.agents import load_toolsfrom langchain.agents import initialize_agentfrom langchain.agents import AgentTypefrom langchain.llms import OpenAIllm = OpenAI(temperature=0)tools = load_tools(["serpapi", "llm-math"], llm=llm)agent = initialize_agent(    tools, llm, agent=AgentType.ZERO_SHOT_REACT_DESCRIPTION, verbose=True)with get_openai_callback() as cb:    response = agent.run(        "Who is Olivia Wilde's boyfriend? What is his current age raised to the 0.23 power?"    )    print(f"Total Tokens: