```async def on_enter(self) -> None: await ...
# ask-ai
b
Copy code
async def on_enter(self) -> None:
    await self.session.generate_reply()
how i can exclude agent message from chat context?
t
Thanks for reaching out, Aleh. I'm an AI assistant and I'm here to help. Give me a moment while I research this topic.
You're asking how to exclude the agent's own messages from the chat context when calling
await 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:
Copy code
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 Docs
b
i want to exclude only 1st greeting message from agent
t
Got it — you want to exclude only the agent's first greeting message from the context while keeping all other assistant messages. Do this by filtering out that single message in the context you pass to the LLM (override
llm_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:
Copy code
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):
Copy code
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.