Can i somehow invoke the Session ’s LLM session so...
# ask-ai
v
Can i somehow invoke the Session ’s LLM session so that i can preload the LLM with the Prompt making it generate reply as soon as user pickup call , no generate_reply becouse it makes a new context i want to use session’s real context
t
Thanks for reaching out, Ravi. I'm an AI assistant and I'm here to help. Give me a moment while I research this topic.
To help you preload an LLM session with a prompt and use the session's real context without relying on
generate_reply
, you can simply initialize the session with a predefined chat context using the
chat_ctx
parameter in the
Agent
or
AgentTask
constructor. This way, the session starts off with the context you want already loaded, making things smoother for you. Here’s a friendly example to guide you:
Copy code
chat_ctx = llm.ChatContext.empty()
chat_ctx.add_message(role="system", content="You are a helpful assistant.")
chat_ctx.add_message(role="user", content="I need help planning a trip.")

agent = Agent(
    llm=openai.realtime.RealtimeModel(),
    chat_ctx=chat_ctx  # Preloads full history
)
This approach helps you avoid triggering speech or response generation right away and directly injects the context into the session. If you’d like, I’m here to help with any further questions or to provide more examples—just let me know! Sources: Workflows | LiveKit Docs | examples/voice_agents/multi_agent.py | pipeline-llm/large_context.py | Real-time AI Models & Streaming | livekit/livekit_composite | DeepWiki | Agent speech and audio | LiveKit Docs
v
i want to trigger it as soon as call is picked up
t
To get the LLM session going right when a call is picked up, a great way is to use the
participant_connected
event. This helps you detect the start of the call and immediately set up the session with the context or response you want. Here’s a simple example to guide you:
Copy code
@ctx.room.on("participant_connected")
def on_participant_connected(participant):
    if "sip_" in participant.identity:
        agent.chat_ctx.add_message(role="system", content="Welcome to our service. How can I assist you today?")
        agent.start(ctx.room, participant)
        session.generate_reply()  # Trigger immediate response
This way, the LLM is all set and ready to respond as soon as the call connects. If you’d like me to walk you through it more or provide some extra examples, just let me know—I’m here to help! Sources: Real-time AI Models & Streaming | livekit/livekit_composite | DeepWiki | External data and RAG | LiveKit Docs | sip-refer/README.mdoc | examples/voice_agents/realtime_load_chat_history.py | SIP Integration & Telephony | livekit/livekit_composite | DeepWiki