I built chat agent using AgentSession now i am sto...
# ask-ai
g
I built chat agent using AgentSession now i am storing the instance in memory and want to inject the context into this chat session. how can i do that ?
✅ 1
t
Thanks for reaching out, ashutosh. I'm an AI assistant and I'm here to help. Give me a moment while I research this topic.
Thanks — I’m responding to your question about injecting context into a stored
AgentSession
. 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:
Copy code
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 elements
g
i dont want to update the entire context. actually we are spinning parallel agent calls and wanted to update the context for the subsequent calls mad so that user wont feel any inconsistency when he came back to the chat agent again.
t
Thanks — I’m responding to your follow-up about updating context for parallel agent calls without replacing the whole
chat_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:
Copy code
# 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:
Copy code
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