numerous-receptionist-14192
12/18/2025, 2:02 PMserver = AgentServer()
def prewarm(proc: JobProcess):
...
server.setup_fnc = prewarm
class MyAgent(Agent):
def __init__(self) -> None:
prompt = langfuse_client.get_prompt(PROMPT_NAME, fallback=PROMPT_FALLBACK).compile()
super().__init__(instructions=prompt)
async def on_enter(self) -> None:
await self.session.say(text="Hello")
@server.rtc_session(agent_name=AGENT_NAME)
async def entrypoint(ctx: JobContext) -> None:
# Connect to the LiveKit room, subscribing only to audio
await ctx.connect(auto_subscribe=AutoSubscribe.AUDIO_ONLY)
session: AgentSession = AgentSession(
stt=...,
llm=...,
tts=...,
vad=...,
)
await session.start(room=ctx.room, agent=MyAgent())
if __name__ == "__main__":
cli.run_app(server)tall-airline-16756
12/18/2025, 2:03 PMtall-airline-16756
12/18/2025, 2:03 PMMyAgent.__init__ can call langfuse_client.get_prompt(...). Initialize Langfuse inside the @server.rtc_session entrypoint, after connecting (await ctx.connect(...)) and before creating the AgentSession or instantiating MyAgent.
Minimal example change:
@server.rtc_session(agent_name=AGENT_NAME)
async def entrypoint(ctx: JobContext) -> None:
await ctx.connect(auto_subscribe=AutoSubscribe.AUDIO_ONLY)
# Initialize Langfuse here so it's available when MyAgent() runs
langfuse_client = setup_langfuse() # your Langfuse init function / config
session: AgentSession = AgentSession(
stt=...,
llm=...,
tts=...,
vad=...,
)
await session.start(room=ctx.room, agent=MyAgent())
If MyAgent needs the client instance directly, pass it into the agent instead of relying on a global:
class MyAgent(Agent):
def __init__(self, lf_client):
prompt = lf_client.get_prompt(PROMPT_NAME, fallback=PROMPT_FALLBACK).compile()
super().__init__(instructions=prompt)
# then:
await session.start(room=ctx.room, agent=MyAgent(langfuse_client))
Would you like an example setup_langfuse() snippet for your environment or guidance on making langfuse_client safe for tests?
Sources: Observability data hooks | LiveKit docs