can 2 agents have access to the same chat_ctx and ...
# ask-ai
s
can 2 agents have access to the same chat_ctx and be listening for changes in the state?
t
Thanks for reaching out, Roman. I'm an AI assistant and I'm here to help. Give me a moment while I research this topic.
Hey there! Just so you know, two agents can definitely access the same
chat_ctx
and work together to manage state changes. This usually happens by sharing the
chat_ctx
during setup or through handoffs. Plus, agents can respond to state and conversation updates using lifecycle methods and events like
on_user_turn_completed
or
conversation_item_added
. That said, it’s not totally clear if two agents can listen independently and in real-time to changes in a shared, mutable
chat_ctx
. Usually, they coordinate through state events, session updates, and carefully controlled context sharing. If you want me to explain anything further or have a particular scenario in mind, just give me a shout—I’m here to help! Sources: Agents 0.x migration guide | LiveKit Docs | main.py | VoicePipelineAgent | LiveKit Agents | LLMStream | LiveKit Agents | livekit-agents/livekit/agents/llm/chat_context.py
s
I'm wondering if an agent could ask a question, a the other one could reply back creating a fully automated conversation (this is for my automated tests). To do so, both have to be listening to chat_ctx changes
t
Hey there! Just a heads-up—LiveKit doesn’t quite support fully automated back-and-forth conversations between two agents where both are listening to
chat_ctx
changes and responding on their own. While agents can share and update the same
chat_ctx
, there isn’t a built-in way for them to automatically notice and react to each other’s changes in real-time. To make this happen for your automated tests, you’d need to set up some custom logic to coordinate what the agents do—like using external state management or triggering responses programmatically when certain events or updates pop up in the
chat_ctx
. If you’d like a hand figuring out how to set this up, just give me a shout—I’m here to help! Sources: main.py | examples/voice_agents/llamaindex-rag/chat_engine.py
s
can an agent assume the "user" role instead of the "assistant"?
t
Yep, an agent in LiveKit can totally take on the "user" role instead of the usual "assistant" role. The framework lets you assign roles like "user" and "assistant" to messages, and agents can easily post messages as "user" using methods like
add_message
. If you want a hand setting this up or have a particular use case you’re thinking about, just give me a shout! Sources: ChatRole | LiveKit Agents | Role | LiveKit Agents | tests/test_agent_session.py | models/templates/google-gemma-2-2b-it.jinja