What's the purpose of having two chat_ctx? We have...
# ask-ai
c
What's the purpose of having two chat_ctx? We have a
.chat_ctx
in
Agent
, and a
._chat_ctx
in
AgentSession
, but they're not always in sync. Which one is the one that the
AgentActivity
actually uses?
t
Thanks for reaching out, Harry. I'm an AI assistant and I'm here to help. Give me a moment while I research this topic.
You're asking which chat context AgentActivity actually uses and why there are two different fields. Good question — here's the short, clear answer and why it works that way. • Agent
.chat_ctx
— the agent's template: initial instructions and baseline conversation context used to configure new sessions. • AgentSession
._chat_ctx
— the active runtime context: initialized from the agent's
chat_ctx
when a session starts and then updated during the conversation. AgentActivity uses the session's
._chat_ctx
(the live context). AgentActivity adds user/assistant messages, handles tool calls, and mutates the session context as the conversation progresses, so it can diverge from the agent's original
.chat_ctx
. If you want the session to start with updated agent instructions, do you intend to (a) update the agent's
.chat_ctx
before starting sessions, or (b) patch the session
._chat_ctx
at runtime? Tell me which workflow you need and I’ll give exact steps. Sources: Python Agents Core Architecture | livekit/livekit_composite | DeepWiki | Agent Session Management & Turn Detection | livekit/livekit_composite | Dee...
c
No. AgentActivity is actually using Agent's chat_ctx.
Copy code
task = self._create_speech_task(
                self._pipeline_reply_task(
                    speech_handle=handle,
                    chat_ctx=chat_ctx or self._agent._chat_ctx,
https://github.com/livekit/agents/blob/4f2c531bf4eb62c30cdac05638104d6b2eaced5e/livekit-agents/livekit/agents/voice/agent_activity.py#L898C1-L901C64
t
You're right — thanks for the snippet. Quick clarification: • Agent
.chat_ctx
(aka
self._agent._chat_ctx
) is used by AgentActivity as the default runtime context when no explicit
chat_ctx
is passed (the
chat_ctx or self._agent._chat_ctx
branch you cited). • AgentSession
._chat_ctx
is a session-level copy/state that may diverge unless explicitly synchronized. What you can do depending on your goal: • If you need AgentActivity to use a different context for a reply, pass that
chat_ctx
explicitly into the pipeline call. • If you want session and agent contexts kept in sync, update the agent’s
chat_ctx
(via
update_chat_ctx()
or the appropriate agent API) when session changes occur so AgentActivity picks up the updated state. Do you want exact code pointers for (A) passing an explicit
chat_ctx
into the reply pipeline or (B) a recommended place to call
update_chat_ctx()
so agent and session stay synchronized? Sources: Agent Session Management & Turn Detection | livekit/livekit_composite | Dee...