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.
720

被折叠的 条评论
为什么被折叠?



