Skip to main content

Overview

Explicitly ends a chat session and triggers all cleanup operations. Always call this endpoint when the user closes the chat or navigates away. Authentication: Authorization: Bearer {session_token} Important: Sessions auto-expire after 30 minutes of inactivity, but explicit ending ensures proper cleanup.

Request

Path Parameters: Headers:
Body: None

Response

Status: 200 OK

What Happens on End

When you call /end, the following cleanup operations run:

1. Billing Finalization

Result: Credits consumed based on actual usage

2. Memory Ingestion (Graphiti)

Result: Conversation memory available for future sessions Timeout: 25 seconds (fire-and-forget)

3. Post-Call Analysis

Result: QA analysis if configured on agent

4. Webhook Notification

Webhook Payload:
Timeout: 7 seconds

5. Session Removal

Result: Session removed from memory, conversation input cleared

Error Responses

401 Unauthorized

403 Forbidden

500 Server Error


Examples

cURL

JavaScript (Fetch)

React (Cleanup on Unmount)

React (With keepalive for Tab Close)

beforeunload Handler

Next.js (Route Handler)


When to Call /end

✅ Always Call When:

  1. User Closes Chat Widget
  2. User Navigates Away
  3. Agent Calls end_chat Tool
  4. Session Timeout (Idle)
    • Handled automatically by server
    • But explicit is better than implicit

❌ Don’t Call When:

  1. Session Already Expired
    • Token invalid, will return 401
    • No cleanup needed
  2. Mid-Conversation
    • User is still chatting
    • Wait for natural end

Auto-Expiry vs Explicit End

Always call /end explicitly. Auto-expiry relies on background cleanup loops that may have delays. Explicit ending ensures immediate cleanup.

Best Practices

1. Use keepalive

2. Handle Errors Gracefully

3. Clear Local State

4. Debounce Rapid Calls



Next Steps

Create New Session

Start a fresh conversation with a new session.

View Analytics

Check session costs and usage in dashboard.