in a multi agent environment, is there a way to ke...
# ask-ai
w
in a multi agent environment, is there a way to keep the agents warmed up to take control instead of initializing them when the need arises?
t
Thanks for reaching out, Sunil. I'm an AI assistant and I'm here to help. Give me a moment while I research this topic.
To help keep your agents nicely warmed up in a multi-agent environment, you can use the prewarm function along with process pool management. This is a great way to load shared resources or models ahead of time, so your agents are ready to jump in without any initialization delays. Here’s a quick rundown on how you can set this up: • Create a
prewarm
function to load shared resources (like models or plugins) and save them in
proc.userdata
. • Use the
num_idle_processes
parameter to decide how many warmed-up processes you want available. • If your environment is more complex, you can load resources specific to each agent or shared ones for different agent types. • You can also adjust the target number of idle processes on the fly with
set_target_idle_processes
, depending on how your workload changes. If you’d like more detailed guidance or some examples tailored to your setup, just let me know—I’m here to help! Sources: External data and RAG | LiveKit Docs | main.py | Worker options | LiveKit Docs | examples/README.md | agents/src/generator.ts
w
can I keep the agents initialized without providing chat history to them?
t
Absolutely, you can keep agents initialized without providing chat history. By default, each new agent starts with a fresh conversation history, so including chat history is completely optional when setting up your agents. If you'd like your agent to start fresh, just skip the
chat_ctx
parameter during initialization. Here's a simple example:
Copy code
agent = Agent(
    instructions="You are a helpful assistant.",
    llm=openai.realtime.RealtimeModel(voice="alloy"),
    # No chat_ctx parameter needed - will start fresh
)
If you have any questions or want me to walk you through more examples that fit your needs, please don’t hesitate to ask—I’m here to help! Sources: Workflows | LiveKit Docs | examples/voice_agents/realtime_load_chat_history.py | agents/src/voice/agent_session.ts
w
so how do I give the chat context when transferring to the agent?
t
When you want to share the chat context while transferring to a new agent, a helpful approach is to copy the previous agent's conversation history and pass it along. Let me walk you through how you can do this: Python Example:
Copy code
# Copy the previous agent's chat context
truncated_chat_ctx = previous_agent.chat_ctx.copy(
    exclude_instructions=True, 
    exclude_function_call=False
).truncate(max_items=6)

# Pass the copied context to the new agent
new_agent = Agent(
    instructions="You are a helpful assistant.",
    llm=openai.realtime.RealtimeModel(voice="alloy"),
    chat_ctx=truncated_chat_ctx
)
Key Points: • You can use
chat_ctx.copy()
to make a duplicate of the context from the previous agent. • It's a good idea to truncate the context to keep only the most relevant messages, like the last 6. • Then, just pass this truncated context when you initialize the new agent. If you’d like, I’m here to help with more examples or to assist you with your specific situation—just let me know!
w
how do I pass the chat context to an already initialized warm agent?
t
To pass chat context to an already initialized warm agent, you can use the
update_chat_ctx()
method. This is a handy way to update the agent's chat context dynamically, even if the agent is already up and running. Example:
Copy code
# Update the chat context of an already initialized agent
await agent.update_chat_ctx(
    chat_ctx,  # The new or updated chat context
    exclude_invalid_function_calls=True  # Optional: Removes invalid function calls
)
Key Points: • It’s a good idea to use
chat_ctx.copy()
to safely duplicate and tweak the context before updating. • For realtime models, this method helps keep the updated context in sync with the ongoing session. • Wrapping this method in a try-catch block is wise to gracefully handle any potential errors, especially with realtime models. If you have any questions or would like some tailored examples for your specific use case, please don’t hesitate to ask—I’m here to help! Sources: agent/main.py | examples/warm-transfer/warm_transfer.py | Agent Session & Client Integration | livekit/livekit_composite | DeepWiki | Agents v0.x migration guide - Python | LiveKit Docs
w
is the on_enter() executed when the agent is initialised or when the control is transferred to agent?
t
The
on_enter()
method in LiveKit agents is called when control is handed over to the agent, rather than during its initialization. This means it kicks in when the agent becomes the active participant in a session. People often use this method to do things like create warm greetings or set up the conversation context as the agent steps in. If you'd like, I’m here to help with more details or examples—just let me know! Sources: Pipeline nodes and hooks | LiveKit Docs | livekit-agents/livekit/agents/job.py
w
give me example of prewarm method where all agents in multiagent system is initialised
t
Here’s a friendly example of a
prewarm
method to help you initialize all agents in a multi-agent system smoothly:
Copy code
def prewarm(proc: JobProcess):
    # Load shared resources for all agents
    proc.userdata["vad"] = silero.VAD.load()
    proc.userdata["shared_llm"] = openai.LLM(model="gpt-4o-mini")
    proc.userdata["stt"] = deepgram.STT(model="nova-3")
    proc.userdata["tts"] = openai.TTS(voice="echo")

async def entrypoint(ctx: JobContext):
    # Access prewarmed resources
    session = AgentSession(
        vad=ctx.proc.userdata["vad"],
        llm=ctx.proc.userdata["shared_llm"],
        stt=ctx.proc.userdata["stt"],
        tts=ctx.proc.userdata["tts"],
    )

    # Start with one agent and allow handoff to others
    await session.start(agent=IntroAgent(), room=ctx.room)
Key Points: • Shared resources like VAD, LLM, STT, and TTS are loaded just once in the
prewarm
function to keep things efficient. • These resources are safely stored in
proc.userdata
, so all agents can easily access them during their sessions. •
AgentSession
helps you manage multiple agents smoothly and supports handoffs between them. If you’d like, I’m here to help craft a more tailored example that fits your specific needs—just let me know! Sources: main.py | agent/main.py | Template System & Example Applications | livekit/livekit_composite | DeepWi... | agent-examples/agent_examples/pipeline_agent.py | Worker options | LiveKit Docs