better-sandwich-65834
12/12/2025, 12:59 PMasync def on_enter(self) -> None:
await self.session.generate_reply()
how i can exclude agent message from chat context?tall-airline-16756
12/12/2025, 12:59 PMtall-airline-16756
12/12/2025, 1:00 PMawait self.session.generate_reply() in on_enter. You cannot pass a filtered context into generate_reply() directly — it uses the agent's chat_ctx. To exclude assistant messages you should filter the context before the LLM sees it by overriding the agent's llm_node.
Example (Python) — override llm_node to remove assistant messages:
async def llm_node(self, chat_ctx, tools, model_settings):
filtered = chat_ctx.copy()
filtered.items = [it for it in filtered.items if it.role != "assistant"]
async for chunk in Agent.default.llm_node(self, filtered, tools, model_settings):
yield chunk
Place this on your agent class so every LLM call (including generate_reply()) uses the filtered context. If you want only the last user message instead, filter filtered.items to include just the final user item (or system + last user).
Want me to show the variant that keeps only the last user message or to adapt this for your agent class structure?
Sources: Pipeline nodes and hooks | LiveKit docs | Agents v0.x migration guide - Python | LiveKit docs | Agents v0.x migration guide - Node.js | LiveKit Docsbetter-sandwich-65834
12/12/2025, 1:26 PMtall-airline-16756
12/12/2025, 1:26 PMllm_node so generate_reply() uses the filtered context).
Two simple ways to identify the greeting to remove:
• By position (remove the first assistant message), or
• By content (remove the assistant message whose text matches your greeting).
Example — remove the first assistant message by position:
async def llm_node(self, chat_ctx, tools, model_settings):
filtered = chat_ctx.copy()
items = list(filtered.items)
# find index of first assistant message and drop it
for i, it in enumerate(items):
if it.role == "assistant":
items.pop(i)
break
filtered.items = items
async for chunk in Agent.default.llm_node(self, filtered, tools, model_settings):
yield chunk
Example — remove by matching greeting text (safer if position can vary):
GREETING_TEXT = "Hello! I'm your assistant." # exact greeting text used initially
async def llm_node(self, chat_ctx, tools, model_settings):
filtered = chat_ctx.copy()
filtered.items = [
it for it in filtered.items
if not (it.role == "assistant" and it.content == GREETING_TEXT)
]
async for chunk in Agent.default.llm_node(self, filtered, tools, model_settings):
yield chunk
Choose position-based if the first assistant message is always the greeting; choose text-based if timing varies. Want a version that removes the greeting only once even if duplicates exist? I can provide that too.