gorgeous-hydrogen-44699
04/10/2026, 1:48 PMtall-airline-16756
04/10/2026, 1:48 PMtall-airline-16756
04/10/2026, 1:49 PMAgentSession.
You can’t modify the session directly; update the current agent’s chat context instead. Steps:
1. Get the current agent: current_agent = session.current_agent.
2. Copy or build a ChatContext, add your injected messages (system/user/assistant or multimodal content).
3. Call await current_agent.update_chat_ctx(new_ctx) to replace the agent’s context.
Example:
from livekit.agents import ChatContext
current_agent = session.current_agent
new_ctx = current_agent.chat_ctx.copy()
new_ctx.add_message(role="system", content="User prefers concise responses.")
await current_agent.update_chat_ctx(new_ctx)
If you want the injected context to persist across agent handoffs, pass chat_ctx when transferring agents. Do you want an example that injects images or a different message role?
Sources: Pipeline nodes and hooks | LiveKit Documentation | External data and RAG | LiveKit Documentation | Images | LiveKit Documentation | Agents and handoffs | LiveKit Documentation, with interactive elementsgorgeous-hydrogen-44699
04/10/2026, 1:51 PMtall-airline-16756
04/10/2026, 1:51 PMchat_ctx.
You can inject incremental context so subsequent calls see the updates and users don’t experience inconsistency. Options:
• Append messages directly to the existing context:
# use the current agent's chat_ctx
ctx = session.current_agent.chat_ctx
ctx.add_message(role="system", content="Background update: user prefers evening slots.")
# then persist/merge the change
await session.current_agent.update_chat_ctx(ctx)
• If parallel workers produce updates, have each worker return its update (message or summary). Merge those into a copy of chat_ctx and call update_chat_ctx() once with the merged additions to avoid race conditions:
ctx_copy = session.current_agent.chat_ctx.copy()
ctx_copy.add_message(role="assistant", content=worker_summary)
await session.current_agent.update_chat_ctx(ctx_copy)
Guidance to avoid inconsistencies:
• Only append finalized/contextual messages (summaries, tool results, user-preference flags).
• Batch/serialize updates (collect parallel results, then merge) to prevent conflicting writes.
• Don’t replace the entire chat_ctx unless you intend to reset history.
Would you like a short example that shows collecting updates from multiple parallel tasks and merging them safely?
Sources: Pipeline nodes and hooks | LiveKit Documentation | Images | LiveKit Documentation | Agent speech and audio | LiveKit Documentation, with interactive elements