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:
Response
Status:200 OK
What Happens on End
When you call/end, the following cleanup operations run:
1. Billing Finalization
2. Memory Ingestion (Graphiti)
3. Post-Call Analysis
4. Webhook Notification
5. Session Removal
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:
-
User Closes Chat Widget
-
User Navigates Away
-
Agent Calls end_chat Tool
-
Session Timeout (Idle)
- Handled automatically by server
- But explicit is better than implicit
❌ Don’t Call When:
-
Session Already Expired
- Token invalid, will return 401
- No cleanup needed
-
Mid-Conversation
- User is still chatting
- Wait for natural end
Auto-Expiry vs Explicit End
Best Practices
1. Use keepalive
2. Handle Errors Gracefully
3. Clear Local State
4. Debounce Rapid Calls
Related Endpoints
- Create Session — Start new session
- Send Message — Send messages with SSE
- Get History — Retrieve message history
Next Steps
Create New Session
Start a fresh conversation with a new session.
View Analytics
Check session costs and usage in dashboard.