Skip to content

关于 Gemini LLM Token 在 Langsmith 显示为 0 的问题 #599

@white-loub

Description

@white-loub

Describe the bug

使用 Langsmith 监控 Graph,当 Gemini ChatModel 进行非流式调用(Non-streaming Generate)时,Langsmith Trace 详情页中的 Token Usage 统计数据显示为 0(Input, Output, Total 均为 0),无法正确反映实际的 Token 消耗。

To Reproduce

Steps to reproduce the behavior:

  1. 配置 components/model/gemini 作为 ChatModel,配置 callbacks/langsmith。
  2. 发起一次非流式的 Generate 调用。
  3. 在 Langsmith 控制台点击该 LLM 节点的 Trace。
  4. 查看 Metadata 标签页下的 Usage 信息。
  5. 现象:显示 "0 tokens"。

Expected behavior

Langsmith 应正确显示 Gemini API 返回的 Token 消耗数据(Prompt Tokens, Completion Tokens, Total Tokens)。

Version:

github.com/cloudwego/eino-ext/callbacks/langsmith (latest)
github.com/cloudwego/eino-ext/components/model/gemini (latest)

Environment:

GOVERSION='go1.25.3'

Additional context

我尝试在本地修复。在 Langsmith OnEnd 回调方法中,检查输出是否为 *model.CallbackOutput,如果是,则将其中的 TokenUsage 提取出来,并按照 Langsmith 的格式 (input_tokens, output_tokens, total_tokens) 写入到 extra.metadata.usage_metadata 字段中。问题似乎得到了解决。

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions