famous-translator-29827
09/04/2025, 9:59 AMtall-airline-16756
09/04/2025, 9:59 AMtall-airline-16756
09/04/2025, 10:00 AMllm_node in LiveKit, you can do this by overriding the llm_node method in your own custom Agent class. This way, you get to tweak the chat_ctx (that’s your conversation history) before it goes to the LLM for processing, or even adjust the LLM’s output before it moves on to the next step.
Here’s a simple example in Python to get you started:
from livekit.agents import Agent
class CustomAgent(Agent):
async def llm_node(self, chat_ctx, tools, model_settings=None):
# Append a system message
chat_ctx.append(
role="system",
text="Always respond in pirate-speak with nautical metaphors."
)
# Pass to default LLM for inference
return await Agent.default.llm_node(self, chat_ctx, tools, model_settings)
Just a few things to keep in mind:
• Use chat_ctx.append() when you want to add some temporary context for the current turn.
• Make sure to return the result of Agent.default.llm_node() or a valid stream so your pipeline keeps running smoothly.
• If you want changes to stick around longer, try using update_chat_ctx().
If you want me to walk you through more examples or explain anything else, just give me a shout!
Sources: Pipeline nodes and hooks | LiveKit Docs | Agents v0.x migration guide - Node.js | LiveKit Docs | livekit-agents/livekit/agents/voice/agent_activity.py | Modifying LLM output before TTS | LiveKit Docs | pipeline-llm/replacing_llm_output.pyfamous-translator-29827
09/04/2025, 10:18 AMtall-airline-16756
09/04/2025, 10:18 AMllm_node behaves for a particular model, you can use the model_settings parameter in the llm_node method. This parameter holds important config info, like which model you’re working with. You can check the model type and then adjust things accordingly.
Here’s a little example to make it clearer:
from livekit.agents import Agent
class CustomAgent(Agent):
async def llm_node(self, chat_ctx, tools, model_settings=None):
# Check the model type
if model_settings and model_settings.model_name == "specific_model_name":
# Modify the chat context for this model
chat_ctx.append(
role="system",
text="This is a custom instruction for the specific model."
)
# Pass to default LLM for inference
return await Agent.default.llm_node(self, chat_ctx, tools, model_settings)
Just a few things to keep in mind:
• You can use model_settings.model_name to spot which model you’re working with.
• Feel free to add conditional logic to change the chat_ctx or other params based on that model.
• Make sure your method still returns a valid stream or result so everything in the pipeline keeps running smoothly.
If you want a hand with a specific model or need help customizing further, just give me a shout!famous-translator-29827
09/04/2025, 12:26 PMtall-airline-16756
09/04/2025, 12:27 PMllm_node, you can do this by tweaking the chat_ctx object, which holds the conversation history. Basically, you grab the last user message, add your string to it, and update the context.
Here’s a quick example to help you out:
from livekit.agents import Agent
class CustomAgent(Agent):
async def llm_node(self, chat_ctx, tools, model_settings=None):
# First, check if there’s a last user message
if chat_ctx and chat_ctx[-1].role == "user":
# Then, append your string to that message
chat_ctx[-1].text += " [Appended String]"
# Finally, pass it along to the default LLM for processing
return await Agent.default.llm_node(self, chat_ctx, tools, model_settings)
Just a few things to keep in mind:
• Make sure the last message actually exists and is from the user by checking chat_ctx[-1].role == "user".
• You’ll want to modify the text property of that last message to add your string.
• Don’t forget to return the updated chat_ctx so the conversation keeps flowing smoothly.
If you want me to walk you through it more or need some extra examples, just give me a shout!