Session Management Best Practices

Effective strategies for managing streaming API sessions and preventing resource leaks

Welcome to Session Management!

The Streaming API sessions encapsulate live user interaction including voice activity, task processing, and other real-time events. Implementing proper session lifecycle management is crucial for:

  • Ensuring predictable session termination
  • Preventing resource leakage and memory issues
  • Identifying and resolving unclosed sessions that can cause quota and concurrency errors
  • Maintaining optimal system performance and reliability

To streamline and strengthen session management, we've enhanced our approach by introducing activity-based timeout mechanisms and explicit keep-alive functionality, replacing the previous idle timeout system.

Session Timeout Configuration

activity_idle_timeout (in /v1/streaming.new)

This parameter defines the maximum duration a session can remain inactive before automatic closure.

Configuration Details

  • Valid Range: 30 seconds minimum, 3599 seconds maximum
  • Default Value: 120 seconds (2 minutes)
  • Activity Sources: User tasks, voice input, and other detectable interaction events
  • Behavior: Timer resets on any detected activity; session closes automatically if threshold is exceeded

Keep-Alive API (/v1/streaming.keep_alive)

A RESTful endpoint that explicitly resets the idle timer for active sessions. This is particularly useful when you know the user is present but no discrete activity events have been detected.

Usage Pattern

Call this endpoint before the activity_idle_timeout expires to maintain session continuity during expected quiet periods.

bash
curl -X POST https://api.konpro.ai/v1/streaming.keep_alive \
  -H "Authorization: Bearer $API_KEY" \
  -H "Content-Type: application/json" \
  -d '{"session_id": "your_session_id"}'

Recommendations & Operational Notes

Configuration Best Practices

  • • Set disable_idle_timeout to false (deprecated feature)
  • • Configure activity_idle_timeout based on your application's interaction patterns
  • • Use shorter timeouts for highly interactive flows
  • • Implement longer timeouts for sessions with expected pauses

Keep-Alive Strategy

Utilize the keep-alive endpoint during periods without detectable activity but when the session should remain active from the user's perspective.

Monitoring & Troubleshooting

  • • Monitor for concurrent limit errors consistently
  • • Persistent "concurrent limit" or quota errors often indicate unclosed sessions
  • • Contact support if you suspect lingering sessions are causing issues
  • • Support can inspect and manually close problematic sessions

Frequently Asked Questions

Q: Why should I avoid using disable_idle_timeout for indefinite sessions?

A: There is no "forever" mode available. Instead, use a high activity_idle_timeout value (up to 3599 seconds) and supplement with keep-alive calls during known quiet periods. The disable_idle_timeout feature is deprecated because it led to unstable session lifecycles and resource management issues.

Q: What should I do if my integration keeps hitting concurrent session limits?

A: First, audit your codebase for unclosed or stale sessions that may not have been properly terminated. If the issue persists or you're unsure about the root cause, contact KonPro technical support. Support teams can inspect your account and manually close problematic sessions to free up capacity.

Q: How do I determine the optimal activity_idle_timeout value?

A: Consider your application's natural interaction patterns. For real-time applications with frequent user input, use shorter timeouts (30-120 seconds). For applications with longer thinking periods or reading sessions, use longer timeouts (300-1800 seconds). Monitor your session closure patterns and adjust accordingly.

Q: What happens if I don't implement proper session management?

A: Poor session management can lead to resource leaks, increased costs, and degraded performance. Unclosed sessions consume server resources and may count against your concurrent session limits, potentially blocking new sessions from being created.

Implementation Example

Here's a practical example of how to implement proper session management in your application:

javascript
// Session management example
class SessionManager {
  constructor(apiKey, baseUrl) {
    this.apiKey = apiKey;
    this.baseUrl = baseUrl;
    this.sessionId = null;
    this.keepAliveInterval = null;
  }

  async createSession(activityTimeout = 120) {
    const response = await fetch(`${this.baseUrl}/v1/streaming.new`, {
      method: 'POST',
      headers: {
        'Authorization': `Bearer ${this.apiKey}`,
        'Content-Type': 'application/json'
      },
      body: JSON.stringify({
        activity_idle_timeout: activityTimeout
      })
    });
    
    const data = await response.json();
    this.sessionId = data.session_id;
    this.startKeepAlive();
    return data;
  }

  startKeepAlive() {
    // Send keep-alive every 60 seconds
    this.keepAliveInterval = setInterval(async () => {
      if (this.sessionId) {
        await this.sendKeepAlive();
      }
    }, 60000);
  }

  async sendKeepAlive() {
    try {
      await fetch(`${this.baseUrl}/v1/streaming.keep_alive`, {
        method: 'POST',
        headers: {
          'Authorization': `Bearer ${this.apiKey}`,
          'Content-Type': 'application/json'
        },
        body: JSON.stringify({
          session_id: this.sessionId
        })
      });
    } catch (error) {
      console.error('Keep-alive failed:', error);
    }
  }

  async closeSession() {
    if (this.keepAliveInterval) {
      clearInterval(this.keepAliveInterval);
    }
    // Implement session closure logic
    this.sessionId = null;
  }
}

Best Practices Summary

✅ Do

  • • Always implement proper session cleanup
  • • Use activity-based timeouts appropriately
  • • Monitor for unclosed sessions regularly
  • • Implement keep-alive for long sessions
  • • Handle connection errors gracefully

❌ Don't

  • • Leave sessions open indefinitely
  • • Ignore concurrent limit errors
  • • Use deprecated disable_idle_timeout
  • • Forget to clear intervals/timeouts
  • • Skip error handling in session management

Conclusion

Proper session management is essential for maintaining stable and efficient streaming API operations. By implementing activity-based timeouts, utilizing keep-alive mechanisms, and monitoring for resource leaks, you can ensure optimal performance and prevent common issues like concurrent session limits. Remember to tailor your timeout settings to your application's specific use case and always implement proper cleanup procedures.

Table of Contents