Hello, I'm experiencing noticeable latency issues ...
# agents
i
Hello, I'm experiencing noticeable latency issues with Gemini Realtime in our telephony setup. Currently, responses are taking approximately 5–6 seconds after the caller finishes speaking. Does anyone know what steps typically reduce latency for real-time voice on telephony? Or is there anything I've done wrong here? Here’s the agent code I’m using (Google RealtimeModel):
Copy code
import logging
import asyncio
from livekit.agents import (
    Agent,
    AgentSession,
    JobContext,
    AgentServer,
    cli,
)
from livekit.plugins import google
from dotenv import load_dotenv
import os

load_dotenv()

api_key = os.getenv("GOOGLE_API_KEY")
LIVEKIT_API_KEY = os.getenv("LIVEKIT_API_KEY")
LIVEKIT_API_SECRET = os.getenv("LIVEKIT_API_SECRET")
LIVEKIT_ROOM_NAME = os.getenv("LIVEKIT_ROOM_NAME", "sip-room")

logging.basicConfig(
    level=logging.DEBUG, format="%(asctime)s - %(name)s - %(levelname)s - %(message)s"
)
logger = logging.getLogger(__name__)


class MyTelephonyAgent(Agent):
    def __init__(self):
        super().__init__(
            instructions="""
            You are a friendly, helpful voice assistant for customer service.
            Answer callers' questions clearly and politely.
            Speak in a warm, natural tone.
            Keep responses concise and helpful.
            """
        )


async def entrypoint(ctx: JobContext):
    try:
        <http://logger.info|logger.info>(f"Agent joining room: {ctx.room.name}")

        await ctx.connect()
        <http://logger.info|logger.info>(f"Agent connected to room: {ctx.room.name}")

        @ctx.room.on("connection_state_changed")
        def on_state_changed(state):
            <http://logger.info|logger.info>(f"Room connection state changed to: {state}")

        <http://logger.info|logger.info>("Initializing Google Realtime model...")
        session = AgentSession(
            llm=google.realtime.RealtimeModel(
                model="gemini-2.5-flash-native-audio-preview-09-2025",
                voice="Kore",
                temperature=0.8,
                api_key=api_key,
                instructions="Be a helpful assistant answering phone calls.",
            ),
        )

        <http://logger.info|logger.info>("Starting agent session...")
        await session.start(agent=MyTelephonyAgent(), room=ctx.room)
        <http://logger.info|logger.info>("Assistant session started")

        try:
            await session.generate_reply(
                instructions="Welcome the caller warmly and ask how you can help them today."
            )
            <http://logger.info|logger.info>("Agent ready and listening for user audio")
        except Exception as e:
            logger.error(f"Error generating initial reply: {e}")

        from livekit.rtc import ConnectionState

        <http://logger.info|logger.info>("Monitoring room participants...")
        while ctx.room.connection_state in [
            ConnectionState.CONN_CONNECTED,
            ConnectionState.CONN_RECONNECTING,
        ]:
            await asyncio.sleep(1)

        <http://logger.info|logger.info>(
            f"Room disconnected (state: {ctx.room.connection_state}), ending session"
        )

    except Exception as e:
        logger.error(f"Error in agent entrypoint: {e}", exc_info=True)
        raise
    finally:
        <http://logger.info|logger.info>("Call ended, cleaning up")


server = AgentServer()


@server.rtc_session(agent_name="gemini-voice-assistance")
async def handle(ctx: JobContext):
    await entrypoint(ctx)


if __name__ == "__main__":
    cli.run_app(server)
Thank you in advance.
👀 1