Skip to main content
Open In ColabOpen on GitHub

如何跟踪LLM的令牌使用情况

跟踪token使用情况以计算成本是将您的应用程序投入生产的重要部分。本指南将介绍如何从您的LangChain模型调用中获取此信息。

Prerequisites

本指南假设您熟悉以下概念:

使用 LangSmith

你可以使用LangSmith来帮助跟踪你的LLM应用中的令牌使用情况。请参阅LangSmith快速入门指南

使用回调函数

有一些特定于API的回调上下文管理器,允许您跟踪跨多个调用的令牌使用情况。您需要检查是否适用于您的特定模型。

如果您的模型没有这样的集成,您可以通过调整OpenAI回调管理器的实现来创建一个自定义的回调管理器。

OpenAI

首先,我们来看一个非常简单的例子,用于跟踪单个聊天模型调用的令牌使用情况。

danger

回调处理程序目前不支持为传统语言模型(例如,langchain_openai.OpenAI)流式传输令牌计数。如需在流式传输上下文中获得支持,请参考聊天模型的相应指南这里

单次调用

from langchain_community.callbacks import get_openai_callback
from langchain_openai import OpenAI

llm = OpenAI(model_name="gpt-3.5-turbo-instruct")

with get_openai_callback() as cb:
result = llm.invoke("Tell me a joke")
print(result)
print("---")
print()

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}")
API Reference:get_openai_callback | OpenAI


Why don't scientists trust atoms?

Because they make up everything.
---

Total Tokens: 18
Prompt Tokens: 4
Completion Tokens: 14
Total Cost (USD): $3.4e-05

多次调用

在上下文管理器内的任何内容都会被跟踪。这里有一个使用它来顺序跟踪链中多个调用的示例。这也适用于可能使用多个步骤的代理。

from langchain_community.callbacks import get_openai_callback
from langchain_core.prompts import PromptTemplate
from langchain_openai import OpenAI

llm = OpenAI(model_name="gpt-3.5-turbo-instruct")

template = PromptTemplate.from_template("Tell me a joke about {topic}")
chain = template | llm

with get_openai_callback() as cb:
response = chain.invoke({"topic": "birds"})
print(response)
response = chain.invoke({"topic": "fish"})
print("--")
print(response)


print()
print("---")
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}")


Why did the chicken go to the seance?

To talk to the other side of the road!
--


Why did the fish need a lawyer?

Because it got caught in a net!

---
Total Tokens: 50
Prompt Tokens: 12
Completion Tokens: 38
Total Cost (USD): $9.400000000000001e-05

流处理

danger

get_openai_callback 目前不支持为旧版语言模型(例如 langchain_openai.OpenAI)流式传输的令牌计数。如果您想在流式传输上下文中正确计数令牌,有几种选择:

请注意,在流式上下文中使用旧版语言模型时,令牌计数不会更新:

from langchain_community.callbacks import get_openai_callback
from langchain_openai import OpenAI

llm = OpenAI(model_name="gpt-3.5-turbo-instruct")

with get_openai_callback() as cb:
for chunk in llm.stream("Tell me a joke"):
print(chunk, end="", flush=True)
print(result)
print("---")
print()

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}")
API Reference:get_openai_callback | OpenAI


Why don't scientists trust atoms?

Because they make up everything!

Why don't scientists trust atoms?

Because they make up everything.
---

Total Tokens: 0
Prompt Tokens: 0
Completion Tokens: 0
Total Cost (USD): $0.0

这个页面有帮助吗?