5-08-18 05:01:43,280 - DEBUG livekit.plugins.opena...
# ask-ai
m
2025-08-18 050143,280 - DEBUG livekit.plugins.openai - reconnecting to OpenAI Realtime API {"max_session_duration": 1200} 2025-08-18 050143,307 - DEBUG livekit.plugins.openai - reconnected to OpenAI Realtime API 2025-08-18 050632,884 - DEBUG livekit.plugins.openai - reconnecting to OpenAI Realtime API {"max_session_duration": 1200} 2025-08-18 050632,915 - DEBUG livekit.plugins.openai - reconnected to OpenAI Realtime API 2025-08-18 050738,225 - DEBUG livekit.plugins.openai - reconnecting to OpenAI Realtime API {"max_session_duration": 1200} 2025-08-18 050738,249 - DEBUG livekit.plugins.openai - reconnected to OpenAI Realtime API 2025-08-18 050835,543 - DEBUG livekit.plugins.openai - reconnecting to OpenAI Realtime API {"max_session_duration": 1200} 2025-08-18 050835,573 - DEBUG livekit.plugins.openai - reconnected to OpenAI Realtime API 2025-08-18 050851,922 - DEBUG livekit.plugins.openai - reconnecting to OpenAI Realtime API {"max_session_duration": 1200} 2025-08-18 050851,952 - DEBUG livekit.plugins.openai - reconnected to OpenAI Realtime API i keep seeing this for outbound call agent.
t
Thanks for reaching out, Usama. I'm an AI assistant and I'm here to help. Give me a moment while I research this topic.
Hey there! The reason you're seeing frequent reconnections to the OpenAI Realtime API is because the
max_session_duration
is set to 1200 seconds (20 minutes) by default. This is just a way to refresh connections regularly and keep things running smoothly. If that’s causing any hiccups for you, no worries—you can tweak the
max_session_duration
setting like this: • Set a longer duration, say 3600 seconds (1 hour):
model = RealtimeModel(max_session_duration=3600)
• Or, if you prefer, turn off automatic reconnections completely by setting it to `None`:
model = RealtimeModel(max_session_duration=None)
Playing around with these options should help you find the right balance and reduce how often those reconnections happen. Let me know if you need a hand with it! Sources: livekit-plugins/livekit-plugins-aws/livekit/plugins/aws/experimental/realti...
m
i want to session to be closed if it reaches it duration
t
Hey there! To make sure a session automatically closes once it hits its duration, you can use the
ConnectionPool
class along with some task management. Here’s a simple way to set it up: 1. First, create a
ConnectionPool
with the
max_session_duration
you want to use to manage your connections:
Copy code
pool = utils.ConnectionPool(
    max_session_duration=max_session_duration,
    connect_cb=self._connect_ws,
    close_cb=self._close_ws,
)
1. Next, set up a task that keeps an eye on the session duration and closes it when the timeout happens:
Copy code
self._timeout_task = asyncio.create_task(
    asyncio.sleep(self._max_session_duration)
)
1. Then, add a
close
method to handle closing the session properly:
Copy code
async def close(self):
    # Put your session closing logic here
    pass
This way, your sessions will close automatically after the time you set. If you want me to help you tweak or expand on this, just let me know—I’m here for you! Sources: examples/voice_agents/session_close_callback.py
m
tts = deepgram.TTS( model="aura-2-andromeda-en", api_key=os.getenv("DEEPGRAM_API_KEY"), ) import aiohttp # Connection pooling is handled internally tts._pool = agents.utils.ConnectionPool[aiohttp.ClientWebSocketResponse]( connect_cb=tts._connect_ws, close_cb=tts._close_ws, max_session_duration=3600, mark_refreshed_on_get=False, ) # Prewarm connections tts.prewarm() super().__init__( instructions=f"""Speak only english. You are an outbound agent that calls patients/customers for follow-ups, reminders, confirmations and service advertisments. {date_message}\n {system_message} You are calling because the user filled out a form {metadata['reason']} Always keep your voice {voiceTone} {past_messages}""", llm=realtime.RealtimeModel( model="gpt-4o-realtime-preview-2025-06-03", # voice="shimmer", modalities=['text'], temperature=1.0, # stream=True, turn_detection=TurnDetection( type="semantic_vad", eagerness="high", # silence_timeout=0.5, create_response=True, interrupt_response=True, ), ), tts=tts ) here is the code, modify it for me
t
Hey there! Could you share a bit more about what changes you’re hoping to make to this code? Are you thinking about tweaking the session duration, adjusting the connection pooling, or maybe updating the TTS or LLM settings? I’m here to help!
m
i want the session to close once it reaches the duration
t
Hey there! To make sure the session wraps up right when it hits its set duration, you can tweak the code a bit to add a task that keeps an eye on the session time and closes it when needed. Here’s the updated version for you:
Copy code
import asyncio
import aiohttp
from livekit.agents.utils import ConnectionPool

# Initialize TTS with connection pooling
tts = deepgram.TTS(
    model="aura-2-andromeda-en",
    api_key=os.getenv("DEEPGRAM_API_KEY"),
)

tts._pool = ConnectionPool[
    aiohttp.ClientWebSocketResponse
](
    connect_cb=tts._connect_ws,
    close_cb=tts._close_ws,
    max_session_duration=3600,  # 1 hour
    mark_refreshed_on_get=False,
)

# Prewarm connections
tts.prewarm()

# Define a method to close the session after the duration
async def close_session_after_duration(duration, tts):
    await asyncio.sleep(duration)
    await tts._pool.close_all()  # Close all connections in the pool

# Start the session and monitor its duration
async def start_session():
    session_task = asyncio.create_task(close_session_after_duration(3600, tts))
    try:
        # Your session logic here
        await session_task
    except asyncio.CancelledError:
        pass

# Initialize the RealtimeModel
super().__init__(
    instructions=f"""Speak only english. You are an outbound agent that calls patients/customers for follow-ups, reminders, confirmations and service advertisments.
    {date_message}\n {system_message}
    You are calling because the user filled out a form {metadata['reason']}
    Always keep your voice {voiceTone} {past_messages}""",
    llm=realtime.RealtimeModel(
        model="gpt-4o-realtime-preview-2025-06-03",
        modalities=['text'],
        temperature=1.0,
        turn_detection=TurnDetection(
            type="semantic_vad",
            eagerness="high",
            create_response=True,
            interrupt_response=True,
        ),
    ),
    tts=tts
)

# Run the session
asyncio.run(start_session())
This little change makes sure your session will close automatically after the set time (3600 seconds here). If you want me to walk you through any part or adjust it further, just let me know—I’m here to help!
m
agent takaes a long time shutting down. add functionality in shutdown callbock that manually shuts it down
t
Hey there! To help with the agent taking a bit too long to shut down, you can add a manual shutdown callback that makes sure everything wraps up smoothly and quickly. Here’s the updated code with that shutdown callback included:
Copy code
import asyncio
import aiohttp
from livekit.agents.utils import ConnectionPool

# Initialize TTS with connection pooling
tts = deepgram.TTS(
    model="aura-2-andromeda-en",
    api_key=os.getenv("DEEPGRAM_API_KEY"),
)

tts._pool = ConnectionPool[
    aiohttp.ClientWebSocketResponse
](
    connect_cb=tts._connect_ws,
    close_cb=tts._close_ws,
    max_session_duration=3600,  # 1 hour
    mark_refreshed_on_get=False,
)

# Prewarm connections
tts.prewarm()

# Define a method to close the session after the duration
async def close_session_after_duration(duration, tts):
    await asyncio.sleep(duration)
    await tts._pool.close_all()  # Close all connections in the pool

# Define a manual shutdown callback
async def manual_shutdown():
    print("Shutting down agent...")
    await tts._pool.close_all()  # Ensure all connections are closed
    print("Agent shutdown complete.")

# Start the session and monitor its duration
async def start_session():
    session_task = asyncio.create_task(close_session_after_duration(3600, tts))
    try:
        # Your session logic here
        await session_task
    except asyncio.CancelledError:
        pass
    finally:
        # Trigger the manual shutdown callback
        await manual_shutdown()

# Initialize the RealtimeModel
super().__init__(
    instructions=f"""Speak only english. You are an outbound agent that calls patients/customers for follow-ups, reminders, confirmations and service advertisments.
    {date_message}\n {system_message}
    You are calling because the user filled out a form {metadata['reason']}
    Always keep your voice {voiceTone} {past_messages}""",
    llm=realtime.RealtimeModel(
        model="gpt-4o-realtime-preview-2025-06-03",
        modalities=['text'],
        temperature=1.0,
        turn_detection=TurnDetection(
            type="semantic_vad",
            eagerness="high",
            create_response=True,
            interrupt_response=True,
        ),
    ),
    tts=tts
)

# Run the session
asyncio.run(start_session())
This little tweak helps make sure the agent shuts down quickly by manually closing all the connections when it’s time. If you want me to walk you through it or tweak anything else, just give me a shout!
m
AttributeError: 'ConnectionPool' object has no attribute 'close_all'
t
Hey there! The error happens because the right way to close all connections in a
ConnectionPool
is by using
aclose
, not
close_all
. No worries though, here’s a quick update to your code that should do the trick:
Copy code
async def manual_shutdown():
    print("Shutting down agent...")
    await tts._pool.aclose()  # Properly close all connections
    print("Agent shutdown complete.")
Just swap out any
close_all
calls with
aclose
, and you’ll be all set with cleaning up the connection pool. If you run into any other bumps, feel free to reach out—I’m here to help! Sources: tests/test_connection_pool.py
m
closing httpclient ctx takes too long to close
t
To help speed up closing the HTTP client context, here are a few friendly tips you can try out: 1. Explicitly Close the Session: Make sure to close the session explicitly using
await session.close()
once you’re done. Here’s a quick example:
Copy code
session = utils.http_context.http_session()
try:
    # Use the session for your requests
finally:
    await session.close()
1. Use Context Managers: Wrapping your session inside an
async with
block is a neat way to ensure it cleans up properly:
Copy code
async with aiohttp.ClientSession() as session:
    # Perform requests
1. Optimize Connection Pooling: You can manage connections more efficiently with
aiohttp.TCPConnector
like this:
Copy code
connector = aiohttp.TCPConnector(
    limit_per_host=50,
    keepalive_timeout=120,
)
async with aiohttp.ClientSession(connector=connector) as session:
    # Use the session
1. Proper Shutdown Logic: Don’t forget to close the session when shutting down, like so:
Copy code
async def aclose(self):
    if self._http_session is not None:
        await self._http_session.close()
        self._http_session = None
Following these steps should help your HTTP client context close faster and more smoothly. If you want, I’m here to help you with any part of the implementation—just let me know! Sources: livekit-plugins/livekit-plugins-baseten/livekit/plugins/baseten/stt.py | livekit-api/livekit/api/livekit_api.py
m
what line to add in shutdown callback function to end the session
t
To wrap up the session in the shutdown callback function, you’ll want to explicitly close the HTTP session by using
await tts._pool.aclose()
. Here’s how the updated shutdown callback function looks:
Copy code
async def manual_shutdown():
    print("Shutting down agent...")
    await tts._pool.aclose()  # Properly close all connections in the pool
    print("Agent shutdown complete.")
This way, all the connections in the session get closed properly when shutting down. If you have any questions or want me to walk you through it, just let me know—I’m here to help!