astonishing-gpu-67869
09/10/2025, 9:46 AMasync def llm_node(
self,
chat_ctx: llm.ChatContext,
tools: list[FunctionTool],
model_settings: ModelSettings,
) -> AsyncIterable[llm.ChatChunk | str]:
"""Override llm_node to truncate chat history before sending to LLM"""
print(f"[LLM NODE] Inside llm_node")
# Truncate chat history to prevent context overflow
MAX_MESSAGES = 8
if len(chat_ctx.messages) > MAX_MESSAGES:
# Simply keep the last 8 messages
original_count = len(chat_ctx.messages)
chat_ctx.messages = chat_ctx.messages[-MAX_MESSAGES:]
print(f"[DEBUG] Truncated chat history from {original_count} to {len(chat_ctx.messages)} messages")
print(f"[LLM NODE] Inside llm_node after truncation logic")
# Call the default LLM node implementation - pass self as first argument to static method
async for chunk in Agent.default.llm_node(self, chat_ctx, tools, model_settings):
yield chunk
tall-airline-16756
09/10/2025, 9:46 AMtall-airline-16756
09/10/2025, 9:46 AMastonishing-gpu-67869
09/10/2025, 9:47 AM[LLM NODE] Inside llm_node
ERROR:livekit.agents:Error in _llm_inference_task
tall-airline-16756
09/10/2025, 9:47 AMastonishing-gpu-67869
09/10/2025, 9:49 AMchat_ctx
.items instead of chat_ctx.messages
tall-airline-16756
09/10/2025, 9:49 AMchat_ctx.items
instead of chat_ctx.messages
is the way to go when truncating chat history. The items
attribute is the correct one in the current LiveKit version, and it’s designed to handle richer conversation structures, including function calls and outputs.
If you’ve been using chat_ctx.messages
, that’s totally understandable—it was part of older versions. Updating your code to use items
will help you stay in sync with the latest API design and make things run more smoothly. If you need any help with this update, I’m here for you!
Sources: Agents 0.x migration guide | LiveKit Docs | livekit-agents/livekit/agents/llm/remote_chat_context.py