LangChain LLMAdapter never reports token usage, so llm_node_tps is always absent
Summary
The LangChain plugin's LLMAdapter never populates usage on the ChatChunk objects it emits. As a consequence, for LangGraph-backed agents:
llm_node_tpsis always absent from per-turn latency metrics- there is no token usage to aggregate for session-level usage tracking
llm_node_ttft, tts_node_ttfb and playback_latency are reported normally, so this is specific to anything derived from token counts.
Environment
livekit-agents1.7.0livekit-plugins-langchain1.7.0langchain1.3.16 /langchain-core1.6.0 /langgraph1.2.11- Python 3.12.8, macOS (arm64)
Steps to reproduce
- Build a minimal LangGraph workflow and use it as the session LLM via
langchain.LLMAdapter(graph=...). - Log per-turn metrics from
ChatMessage.metrics:
@session.on("conversation_item_added")
def on_conversation_item_added(ev: ConversationItemAddedEvent) -> None:
metrics = getattr(ev.item, "metrics", None)
if metrics:
logger.info("metrics [%s] %s", ev.item.role, metrics)
- Have a few turns of conversation.
Actual behavior
llm_node_tps never appears, across every turn:
metrics [assistant] llm_node_ttft=0.750 tts_node_ttfb=1.263 playback_latency=0.095
metrics [assistant] llm_node_ttft=0.597 tts_node_ttfb=1.057 playback_latency=0.000
metrics [assistant] llm_node_ttft=0.581 tts_node_ttfb=1.123 playback_latency=0.036
Root cause
llm_node_tps is computed in livekit/agents/voice/generation.py and requires usage to have been seen on the stream:
if (
usage is not None
and first_content_at is not None
and (streaming_window := last_content_at - first_content_at) > 0
):
data.tps = usage.completion_tokens / streaming_window
usage is collected from the chunks themselves:
if chunk.usage is not None:
usage = chunk.usage
But _to_chat_chunk in livekit/plugins/langchain/langgraph.py constructs ChatChunk without usage:
return llm.ChatChunk(
id=message_id,
delta=llm.ChoiceDelta(
role="assistant",
content=content,
),
)
usage does not appear anywhere in the livekit-plugins-langchain package, so the condition can never be satisfied. The other two conditions are met — the adapter does stream multiple chunks, and llm_node_ttft is reported — so the only missing piece is the token count.
Suggested fix
LangChain's AIMessageChunk already carries the information as usage_metadata: UsageMetadata | None, with input_tokens / output_tokens / total_tokens, which maps onto CompletionUsage directly:
usage = None
if isinstance(msg, BaseMessageChunk) and (um := getattr(msg, "usage_metadata", None)):
usage = llm.CompletionUsage(
completion_tokens=um["output_tokens"],
prompt_tokens=um["input_tokens"],
total_tokens=um["total_tokens"],
)
return llm.ChatChunk(
id=message_id,
delta=llm.ChoiceDelta(role="assistant", content=content),
usage=usage,
)
One caveat worth noting: some providers only emit usage_metadata when explicitly asked (for example ChatOpenAI(stream_usage=True)), so the chunk carrying usage may have empty content. The current implementation returns None early for any chunk without text content, which would drop a usage-only chunk even after the mapping above is added.
Happy to open a PR if this direction looks right.
Confirmed on current main (2cc38f4), and the downstream half checks out too — which makes your suggested fix land cleanly.
The plugin side, as you described it. _to_chat_chunk builds the chunk without usage (langgraph.py:256), and grep -rn usage livekit-plugins/livekit-plugins-langchain/ returns nothing at all, so there is no path by which the field is ever populated.
The caveat you raised is the sharper half. langgraph.py:253 is:
if not content:
return None
so a provider that emits usage on a separate, content-free chunk — ChatOpenAI(stream_usage=True) being the common case — has that chunk dropped before a ChatChunk is ever constructed. Adding the usage_metadata mapping alone would not fix those providers.
What I checked that is not in the issue: the framework is already ready for a usage-only chunk. In voice/generation.py:
elif isinstance(chunk, ChatChunk):
if chunk.usage is not None: # 262
usage = chunk.usage
if not chunk.delta: # 264
continue
Two things follow. Usage is recorded at 262 before the empty-delta skip at 264, so emitting a ChatChunk that carries only usage and no delta is already handled correctly — nothing downstream needs to change. And 263 is last-wins rather than a sum, so LangChain's usage_metadata being cumulative across chunks is safe here; it will not double count.
So the fix is the mapping you wrote plus relaxing the early return so a chunk is still emitted when there is no content but there is usage.
You offered to open the PR and it is your find — it should be yours. If you would rather not, say so and I am happy to put it up with a test that pins both halves: usage reported on a normal streaming turn, and usage still reported when it arrives on a content-free chunk.
Hi, I'm Himanshu, new to contributing here. I saw #6949 and #7593 already add usage handling. Could I help validate usage-only chunks and cumulative usage across multiple chunks, checking that downstream session totals aren't double-counted? Please point me to any remaining test gap and the PR to coordinate with; I haven't run it yet.