Overview
Cancel the current task execution. The session remains active and can receive new messages to start a different task. Use this when you want to abandon the current task but keep the session.
Request
Bearer token for authentication
Response
Whether the operation completed successfully
{
"success": true,
"message": "Session cancelled successfully"
}
Example
curl -X POST https://api.agi.tech/v1/sessions/<session_id>/cancel \
-H "Authorization: Bearer $AGI_API_KEY"
Use Cases
Emergency Stop
# User clicks "stop" button
def emergency_stop(session_id):
try:
result = cancel_session(session_id)
print("Task cancelled")
except Exception as e:
# Fallback: delete entire session
delete_session(session_id)
User Rejection
# Pause for user approval, cancel if rejected
pause_session(session_id)
print("Agent wants to make a purchase. Approve? (y/n)")
approval = input()
if approval.lower() == 'y':
resume_session(session_id)
else:
cancel_session(session_id)
Task Abandonment
# Cancel current task and start a new one
cancel_session(session_id)
# Send new task
send_message(session_id, "Find flights to Paris instead")
Cancel vs Delete
| Operation | Effect | Use When |
|---|
| Cancel | Stop task, session stays active | Want to abandon current task |
| Delete | Destroy entire session | Completely done with session |
Best Practices
Use cancel when you want to stop the current task but keep the session active for a new task.
After cancelling, the session remains active. Send a new message to start a different task, or delete the session to clean up.
Cancelled sessions can still receive new messages to start fresh tasks. Deleted sessions are permanently removed.