fierce-jordan-44682
08/28/2025, 6:23 AMtall-airline-16756
08/28/2025, 6:23 AMtall-airline-16756
08/28/2025, 6:24 AMroom.localParticipant.sendText. To figure out where the input came from, check the on_user_turn_completed hook.
• Change the LLM Context on the Fly: The on_user_turn_completed lifecycle hook is your buddy for tweaking or enriching user input before it reaches the LLM. You might want to add system messages or clean things up dynamically.
• Add Text Input Manually: The update_chat_ctx method lets you pop text messages right into the context, so the LLM doesn’t miss a thing.
Here’s a quick example showing how to adjust the LLM context dynamically:
from livekit.agents import Agent, ChatContext, ChatMessage
class MyAgent(Agent):
async def on_user_turn_completed(
self, turn_ctx: ChatContext, new_message: ChatMessage
):
# Just a little something: If the user says "urgent," add a system note
if "urgent" in new_message.text_content.lower():
turn_ctx.add_message(
role="system",
content="The user has indicated urgency. Respond quickly and concisely."
)
# Clean up the user message by removing "urgent"
new_message.text_content = new_message.text_content.replace("urgent", "")
And if you want even more control, you can override the llm_node method to tweak the context right before sending it off to the LLM:
async def llm_node(
self,
chat_ctx: ChatContext,
tools: list[FunctionTool],
model_settings: ModelSettings
):
# Add some friendly instructions or context
chat_ctx.add_message(
role="system",
content="Always respond in a friendly and helpful tone."
)
# Then pass it along to the default process
async for chunk in self.default.llm_node(chat_ctx, tools, model_settings):
yield chunk
With these handy methods, you’ll have both text and audio inputs working together smoothly in the LLM’s context to generate great responses. If you have any questions or want to chat more about this, I’m here to help!
Sources: Agents 0.x migration guide | LiveKit Docs | Pipeline nodes and hooks | LiveKit Docs | agents/src/pipeline/pipeline_agent.tsfierce-jordan-44682
08/28/2025, 6:27 AMtall-airline-16756
08/28/2025, 6:28 AMAgentSession, you can create methods like llm_node right inside your Agent class. These methods will then be automatically called during the session. For instance, if you want to customize how the LLM behaves, you’d override the llm_node method in your agent class like this:
from livekit.agents import Agent, ChatContext, ModelSettings
class MyAgent(Agent):
async def llm_node(self, chat_ctx: ChatContext, tools: list, model_settings: ModelSettings):
# Add dynamic instructions or context
chat_ctx.add_message(
role="system",
content="Always respond in a friendly and helpful tone."
)
# Delegate to default implementation
async for chunk in self.default.llm_node(chat_ctx, tools, model_settings):
yield chunk
When you’re working with AgentSession, you just create an instance of your custom agent and pass it to the session. The overridden methods like llm_node will automatically be used throughout the session. Here’s a quick example:
from livekit.agents import AgentSession
# Instantiate your custom agent
my_agent = MyAgent()
# Create a session with the agent
session = AgentSession(agent=my_agent)
# Start the session (example entry point)
await session.run()
By defining methods like llm_node in your agent class, you make sure they fit right into the session’s processing flow without any extra hassle. If you want me to walk you through anything else or share more examples, just let me know—I’m here to help!
Sources: Agents v0.x migration guide - Node.js | LiveKit Docs | Agents 0.x migration guide | LiveKit Docs | Text and transcriptions | LiveKit Docs | examples/voice_agents/llamaindex-rag/chat_engine.py