melodic-hamburger-67426
08/18/2025, 5:13 AMtall-airline-16756
08/18/2025, 5:13 AMtall-airline-16756
08/18/2025, 5:14 AMmax_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...melodic-hamburger-67426
08/18/2025, 5:14 AMtall-airline-16756
08/18/2025, 5:15 AMConnectionPool
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:
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:
self._timeout_task = asyncio.create_task(
asyncio.sleep(self._max_session_duration)
)
1. Then, add a close
method to handle closing the session properly:
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.pymelodic-hamburger-67426
08/18/2025, 5:17 AMtall-airline-16756
08/18/2025, 5:17 AMmelodic-hamburger-67426
08/18/2025, 5:18 AMtall-airline-16756
08/18/2025, 5:18 AMimport 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!melodic-hamburger-67426
08/18/2025, 5:23 AMtall-airline-16756
08/18/2025, 5:24 AMimport 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!melodic-hamburger-67426
08/18/2025, 6:22 AMtall-airline-16756
08/18/2025, 6:22 AMConnectionPool
is by using aclose
, not close_all
. No worries though, here’s a quick update to your code that should do the trick:
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.pymelodic-hamburger-67426
08/18/2025, 7:26 AMtall-airline-16756
08/18/2025, 7:26 AMawait session.close()
once you’re done. Here’s a quick example:
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:
async with aiohttp.ClientSession() as session:
# Perform requests
1. Optimize Connection Pooling: You can manage connections more efficiently with aiohttp.TCPConnector
like this:
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:
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.pymelodic-hamburger-67426
08/18/2025, 7:31 AMtall-airline-16756
08/18/2025, 7:31 AMawait tts._pool.aclose()
. Here’s how the updated shutdown callback function looks:
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!