Token Usage Tracking for OpenAI LLMs in LangChain

https://python.langchain.com.cn/docs/modules/model_io/models/llms/how_to/token_usage_tracking

Token Usage Tracking for OpenAI LLMs in LangChain

This content is based on LangChain’s official documentation (langchain.com.cn) and explains token usage tracking—monitoring tokens consumed by LLM calls—in simplified terms. It strictly preserves all original source codes, examples, and knowledge points without any additions or modifications.

Key Note: Token usage tracking is currently only supported for the OpenAI API.

1. What is Token Usage Tracking?

Token usage tracking lets you monitor:

  • Total tokens used (prompt tokens + completion tokens).
  • Breakdown of prompt tokens (tokens in your input) and completion tokens (tokens in the LLM’s output).
  • Number of successful requests.
  • Estimated cost (in USD) for the calls.

LangChain uses the get_openai_callback() context manager to track all OpenAI LLM activity within its scope.

2. Step 1: Import Required Modules

The code below imports the necessary LangChain classes—exactly as in the original documentation:

from langchain.llms import OpenAI
from langchain.callbacks import get_openai_callback

3. Scenario 1: Track a Single LLM Call

Monitor token usage for one individual LLM request.

Code:

# Initialize the OpenAI LLM
llm = OpenAI(model_name="text-davinci-002", n=2, best_of=2)

# Use the callback context manager to track tokens
with get_openai_callback() as cb:
    result = llm("Tell me a joke")
    print(cb)  # Print the token usage summary

Output (exact as original):

Tokens Used: 42
    Prompt Tokens: 4
    Completion Tokens: 38
Successful Requests: 1
Total Cost (USD): $0.00084

4. Scenario 2: Track Multiple Consecutive Calls

Track token usage for multiple LLM requests within the same context manager (sums up all activity).

Code:

with get_openai_callback() as cb:
    result = llm("Tell me a joke")
    result2 = llm("Tell me a joke")
    print(cb.total_tokens)  # Print total tokens across both calls

Output (exact as original):

91

5. Scenario 3: Track Tokens for Chains/Agents

Token tracking works for complex workflows like agents (with multiple steps/tools)—it aggregates tokens from all LLM calls in the workflow.

Step 5.1: Import Additional Modules for the Agent

from langchain.agents import load_tools
from langchain.agents import initialize_agent
from langchain.agents import AgentType

Step 5.2: Initialize the LLM, Tools, and Agent

llm = OpenAI(temperature=0)
tools = load_tools(["serpapi", "llm-math"], llm=llm)  # Tools: Search + Calculator

agent = initialize_agent(
    tools, llm, agent=AgentType.ZERO_SHOT_REACT_DESCRIPTION, verbose=True
)

Step 5.3: Run the Agent and Track Tokens

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 detailed token and cost breakdown
    print(f"Total Tokens: {cb.total_tokens}")
    print(f"Prompt Tokens: {cb.prompt_tokens}")
    print(f"Completion Tokens: {cb.completion_tokens}")
    print(f"Total Cost (USD): ${cb.total_cost}")

Output (exact as original, including agent workflow logs):

[1m> Entering new AgentExecutor chain...[0m
[32;1m[1;3m I need to find out who Olivia Wilde's boyfriend is and then calculate his age raised to the 0.23 power.
Action: Search
Action Input: "Olivia Wilde boyfriend"[0m
Observation: [36;1m[1;3mSudeikis and Wilde's relationship ended in November 2020. Wilde was publicly served with court documents regarding child custody while she was presenting Don't Worry Darling at CinemaCon 2022. In January 2021, Wilde began dating singer Harry Styles after meeting during the filming of Don't Worry Darling.[0m
Thought:[32;1m[1;3m I need to find out Harry Styles' age.
Action: Search
Action Input: "Harry Styles age"[0m
Observation: [36;1m[1;3m29 years[0m
Thought:[32;1m[1;3m I need to calculate 29 raised to the 0.23 power.
Action: Calculator
Action Input: 29^0.23[0m
Observation: [33;1m[1;3mAnswer: 2.169459462491557
[0m
Thought:[32;1m[1;3m I now know the final answer.
Final Answer: Harry Styles, Olivia Wilde's boyfriend, is 29 years old and his age raised to the 0.23 power is 2.169459462491557.[0m
[1m> Finished chain.[0m
Total Tokens: 1506
Prompt Tokens: 1350
Completion Tokens: 156
Total Cost (USD): $0.03012

Key Takeaways

  • Use get_openai_callback() as a context manager to track token usage.
  • It works for single calls, multiple calls, and complex workflows (chains/agents).
  • Tracks total tokens, prompt/completion token breakdown, successful requests, and cost.
  • Only supported for OpenAI API.
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值