Within a deployed livekit agent, what method call ...
# ask-ai
b
Within a deployed livekit agent, what method call can i use to continue to add context to my agent responses?
t
Thanks for reaching out, Hasan. I'm an AI assistant and I'm here to help. Give me a moment while I research this topic.
Thanks — you're asking which method to call inside a deployed LiveKit agent to persist additional context for subsequent responses. Use the update_chat_ctx method on the agent instance. Call it after you modify the agent's ChatContext (for example in an on_user_turn_completed hook) so the added messages or data are saved into the chat history and available for future LLM generations. If you’d like, tell me whether you’re using Python or Node.js and I’ll show a short example. Sources: Pipeline nodes and hooks | LiveKit docs | External data and RAG | LiveKit docs
b
i am uding python
t
Thanks — since you’re using Python, here’s a concise example showing how to add context to the agent’s
ChatContext
and persist it with
update_chat_ctx
.
Copy code
# inside your agent class

async def on_user_turn_completed(self, turn_ctx: ChatContext, new_message: ChatMessage) -> None:
    # 1) Retrieve or build additional context (RAG result, DB lookup, etc.)
    rag_content = await my_rag_lookup(new_message.text_content)

    # 2) Add that content to the chat context so it becomes part of history
    turn_ctx.add_message(
        role="assistant",
        content=f"Additional context: {rag_content}"
    )

    # 3) Persist the updated ChatContext so future LLM generations see it
    await self.update_chat_ctx(turn_ctx)
Notes: • Place this in your agent handler (e.g., inside
on_user_turn_completed
) after you compute any extra context. •
turn_ctx.add_message(...)
can also use other roles and structured content if needed. •
update_chat_ctx
ensures the added message is stored for subsequent turns. Want me to adapt this to include metadata, multiple messages, or a specific RAG pattern?
b
assume i want to add it on init
t
Got it — you want to persist initial context when the agent starts. Add messages to a ChatContext during initialization and call
update_chat_ctx
to save them. Example for a Python agent:
Copy code
class MyAgent(Agent):
    async def on_init(self) -> None:
        # create a ChatContext (or load an existing one)
        initial_ctx = ChatContext()

        # add one or more messages you want persisted
        initial_ctx.add_message(role="system", content="You are a helpful assistant for Acme Corp.")
        initial_ctx.add_message(role="assistant", content="Here is some product knowledge: ...")

        # persist the chat context so future turns include this context
        await self.update_chat_ctx(initial_ctx)
Notes: • Use
role="system"
,
role="assistant"
, or other roles as appropriate. • You can load RAG results or DB content here before calling
update_chat_ctx
. • If your agent framework provides a pre-existing ChatContext on init, modify that instance instead of creating a new one.
b
where do i import chat context from?
t
Import
ChatContext
from
livekit.agents.llm
.
Copy code
from livekit.agents.llm import ChatContext
Would you like a brief example showing
on_init
using that import? Sources: External data and RAG | LiveKit docs