> ## Documentation Index
> Fetch the complete documentation index at: https://docs.xpander.ai/llms.txt
> Use this file to discover all available pages before exploring further.

# Stop Task

> Stop a running task

Cancel a running task. Sends a stop signal to the worker executing the task, marks the execution as `stopped`, and sets `is_manually_stopped: true`. Returns the updated task record.

The endpoint is idempotent: calling it on a task that is already in a terminal state (`completed`, `failed`, `stopped`) returns the current execution unchanged.

## Path Parameters

<ParamField path="task_id" type="string" required>
  Unique identifier of the task to stop (UUID format)
</ParamField>

## Response

Returns the updated [Agent Execution](/api-reference/v1/tasks/get-task) object with `status` set to `stopped` and `is_manually_stopped` set to `true`.

## Example Request

```bash theme={"dark"}
curl -X POST -H "x-api-key: <your-api-key>" \
  "https://api.xpander.ai/v1/tasks/<task-id>/stop"
```

## Example Response

```json theme={"dark"}
{
  "id": "fbd08e4d-cfa7-4838-b6e6-e51855ac2ba3",
  "agent_id": "<agent-id>",
  "organization_id": "<org-id>",
  "status": "stopped",
  "is_manually_stopped": true,
  "created_at": "2026-05-13T10:12:04.123456Z",
  "started_at": "2026-05-13T10:12:05.001234Z",
  "finished_at": "2026-05-13T10:12:42.998877Z",
  "result": null,
  "source": "api"
}
```

## Response Status Codes

| Status | Meaning                                                    |
| ------ | ---------------------------------------------------------- |
| 200    | Stop signal accepted (or task already in a terminal state) |
| 401    | Unauthorized (invalid API key)                             |
| 403    | Forbidden (task belongs to a different organization)       |
| 404    | Task not found                                             |
| 422    | Validation error (malformed `task_id`)                     |

## Important Notes

* **Idempotent** - Tasks already in `completed`, `failed`, or `stopped` are returned unchanged
* **Asynchronous** - The worker may take a few seconds to observe the stop signal and exit cleanly. Poll [Get Task](/api-reference/v1/tasks/get-task) until `status` becomes `stopped`
* **Self-hosted environments** - For agents running in a self-hosted environment, the stop request is routed cross-environment via the asset-command channel
* **Manual flag** - `is_manually_stopped: true` distinguishes user-initiated cancels from system-side terminations

## Example: Stop and confirm

```bash theme={"dark"}
TASK_ID="<task-id>"

curl -s -X POST -H "x-api-key: <your-api-key>" \
  "https://api.xpander.ai/v1/tasks/$TASK_ID/stop" > /dev/null

while true; do
  STATUS=$(curl -s -H "x-api-key: <your-api-key>" \
    "https://api.xpander.ai/v1/tasks/$TASK_ID" | jq -r '.status')

  if [[ $STATUS == "stopped" || $STATUS == "completed" || $STATUS == "failed" ]]; then
    echo "Final status: $STATUS"
    break
  fi

  sleep 2
done
```

## Use Cases

* **Cancel a runaway agent** - Halt an execution that is consuming too much time or tokens
* **User-initiated cancel** - Wire a "Stop" button in your UI to this endpoint
* **Abort long-running tasks** - Free up worker capacity when a task is no longer needed
* **Recover from stuck tasks** - Force-terminate executions that are not progressing


## OpenAPI

````yaml POST /v1/tasks/{task_id}/stop
openapi: 3.1.0
info:
  title: xpander.ai API Service
  description: |2-

        The xpander.ai API Service provides a unified REST API for managing AI agents,
        executing tasks, managing knowledge bases, and integrating with external systems.
        
        Features:
        - Agent Management: Create, update, deploy, and delete AI agents
        - Task Execution: Invoke agents with support for sync, async, and streaming modes
        - Knowledge Bases: Manage knowledge bases and documents for RAG workflows
        - Tools: Discover, connect, and attach tools (connectors, custom functions, MCP servers, sub-agents, workflows) to agents and workflows
        - MCP Integration: Model Context Protocol support for standardized AI interactions
        
        Authentication: All endpoints require authentication via either an API key (`x-api-key`) or an OAuth2 JWT (`Authorization: Bearer <jwt>`).
        
  version: '0.001'
servers:
  - url: https://api.xpander.ai
security: []
paths:
  /v1/tasks/{task_id}/stop:
    post:
      tags:
        - API v1
        - Tasks
        - Tasks CRUD
      summary: Stop Task
      description: Stop a running task
      operationId: Stop_Task_v1_tasks__task_id__stop_post
      parameters:
        - name: task_id
          in: path
          required: true
          schema:
            type: string
            title: Task Id
      responses:
        '200':
          description: Successful Response
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/AgentExecution'
        '422':
          description: Validation Error
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/HTTPValidationError'
      security:
        - APIKeyHeader: []
components:
  schemas:
    AgentExecution:
      properties:
        id:
          type: string
          title: Id
        agent_id:
          type: string
          title: Agent Id
        organization_id:
          type: string
          title: Organization Id
        input:
          $ref: '#/components/schemas/AgentExecutionInput-Output'
        status:
          anyOf:
            - $ref: >-
                #/components/schemas/xpander_dev_utils__models__executions__AgentExecutionStatus
            - type: 'null'
          default: pending
        internal_status:
          anyOf:
            - type: string
            - type: 'null'
          title: Internal Status
        last_executed_node_id:
          anyOf:
            - type: string
            - type: 'null'
          title: Last Executed Node Id
        agent_version:
          anyOf:
            - type: string
            - type: 'null'
          title: Agent Version
        created_at:
          type: string
          format: date-time
          title: Created At
        started_at:
          anyOf:
            - type: string
              format: date-time
            - type: 'null'
          title: Started At
        paused_at:
          anyOf:
            - type: string
              format: date-time
            - type: 'null'
          title: Paused At
        finished_at:
          anyOf:
            - type: string
              format: date-time
            - type: 'null'
          title: Finished At
        result:
          anyOf:
            - type: string
            - type: 'null'
          title: Result
        parent_execution:
          anyOf:
            - type: string
            - type: 'null'
          title: Parent Execution
        sub_executions:
          anyOf:
            - items:
                type: string
              type: array
            - type: 'null'
          title: Sub Executions
          default: []
        finished_sub_executions:
          anyOf:
            - items:
                type: string
              type: array
            - type: 'null'
          title: Finished Sub Executions
          default: []
        should_update_parent:
          anyOf:
            - type: boolean
            - type: 'null'
          title: Should Update Parent
          default: false
        is_manually_stopped:
          anyOf:
            - type: boolean
            - type: 'null'
          title: Is Manually Stopped
          default: false
        is_background:
          anyOf:
            - type: boolean
            - type: 'null'
          title: Is Background
          default: false
        payload_extension:
          anyOf:
            - additionalProperties: true
              type: object
            - type: 'null'
          title: Payload Extension
        hitl_request:
          anyOf:
            - $ref: '#/components/schemas/HumanInTheLoopRequest'
            - type: 'null'
        pending_eca_request:
          anyOf:
            - $ref: '#/components/schemas/PendingECARequest'
            - type: 'null'
        source:
          anyOf:
            - type: string
            - type: 'null'
          title: Source
        worker_id:
          anyOf:
            - type: string
            - type: 'null'
          title: Worker Id
        additional_context:
          anyOf:
            - type: string
            - type: 'null'
          title: Additional Context
        instructions_override:
          anyOf:
            - type: string
            - type: 'null'
          title: Instructions Override
        expected_output:
          anyOf:
            - type: string
            - type: 'null'
          title: Expected Output
        test_run_node_id:
          anyOf:
            - type: string
            - type: 'null'
          title: Test Run Node Id
        is_orchestration:
          anyOf:
            - type: boolean
            - type: 'null'
          title: Is Orchestration
          default: false
        is_gateway:
          anyOf:
            - type: boolean
            - type: 'null'
          title: Is Gateway
          default: false
        output_format:
          anyOf:
            - $ref: '#/components/schemas/OutputFormat'
            - type: 'null'
          default: text
        voice_id:
          anyOf:
            - type: string
            - type: 'null'
          title: Voice Id
        output_schema:
          anyOf:
            - additionalProperties: true
              type: object
            - type: 'null'
          title: Output Schema
        events_streaming:
          anyOf:
            - type: boolean
            - type: 'null'
          title: Events Streaming
          default: false
        used_mutating_tools:
          anyOf:
            - type: boolean
            - type: 'null'
          title: Used Mutating Tools
          default: false
        is_continuous:
          anyOf:
            - type: boolean
            - type: 'null'
          title: Is Continuous
          default: false
        mcp_servers:
          anyOf:
            - items:
                additionalProperties: true
                type: object
              type: array
            - type: 'null'
          title: Mcp Servers
          default: []
        triggering_agent_id:
          anyOf:
            - type: string
            - type: 'null'
          title: Triggering Agent Id
        title:
          anyOf:
            - type: string
            - type: 'null'
          title: Title
        think_mode:
          anyOf:
            - $ref: '#/components/schemas/ThinkMode'
            - type: 'null'
          default: default
        disable_attachment_injection:
          anyOf:
            - type: boolean
            - type: 'null'
          title: Disable Attachment Injection
          default: false
        deep_planning:
          anyOf:
            - $ref: '#/components/schemas/DeepPlanning'
            - type: 'null'
        execution_attempts:
          anyOf:
            - type: integer
            - type: 'null'
          title: Execution Attempts
          default: 1
        user_tokens:
          anyOf:
            - additionalProperties: true
              type: object
            - type: 'null'
          title: User Tokens
        user_oidc_token:
          anyOf:
            - type: string
            - type: 'null'
          title: User Oidc Token
        return_metrics:
          anyOf:
            - type: boolean
            - type: 'null'
          title: Return Metrics
          default: false
        tokens:
          anyOf:
            - $ref: '#/components/schemas/LLMTokens'
            - type: 'null'
        llm_model_provider:
          anyOf:
            - type: string
            - type: 'null'
          title: Llm Model Provider
          description: >-
            Snapshot of the LLM provider used for this execution (request
            override or agent default at run time).
        llm_model_name:
          anyOf:
            - type: string
            - type: 'null'
          title: Llm Model Name
          description: >-
            Snapshot of the LLM model name used for this execution (request
            override or agent default at run time).
        llm_reasoning_effort:
          anyOf:
            - $ref: '#/components/schemas/LLMReasoningEffort'
            - type: 'null'
          description: >-
            Snapshot of the reasoning effort applied during this execution, when
            supported by the model.
      type: object
      required:
        - id
        - agent_id
        - organization_id
        - input
        - created_at
      title: AgentExecution
    HTTPValidationError:
      properties:
        detail:
          items:
            $ref: '#/components/schemas/ValidationError'
          type: array
          title: Detail
      type: object
      title: HTTPValidationError
    AgentExecutionInput-Output:
      properties:
        text:
          anyOf:
            - type: string
            - type: 'null'
          title: Text
          default: ''
        files:
          anyOf:
            - items:
                type: string
              type: array
            - type: 'null'
          title: Files
          default: []
        user:
          anyOf:
            - $ref: '#/components/schemas/User-Output'
            - type: 'null'
      type: object
      title: AgentExecutionInput
    xpander_dev_utils__models__executions__AgentExecutionStatus:
      type: string
      enum:
        - pending
        - executing
        - paused
        - error
        - failed
        - completed
        - stopped
      title: AgentExecutionStatus
    HumanInTheLoopRequest:
      properties:
        wait_node_id:
          type: string
          title: Wait Node Id
      type: object
      required:
        - wait_node_id
      title: HumanInTheLoopRequest
      description: |-
        Model representing human-in-the-loop approval records for tasks.

        Attributes:
            wait_node_id (str): The id of the node that triggered this HITL.
    PendingECARequest:
      properties:
        connector_name:
          type: string
          title: Connector Name
        auth_url:
          type: string
          title: Auth Url
      type: object
      required:
        - connector_name
        - auth_url
      title: PendingECARequest
    OutputFormat:
      type: string
      enum:
        - text
        - markdown
        - json
        - voice
      title: OutputFormat
    ThinkMode:
      type: string
      enum:
        - default
        - harder
      title: ThinkMode
    DeepPlanning:
      properties:
        enabled:
          anyOf:
            - type: boolean
            - type: 'null'
          title: Enabled
          default: false
        enforce:
          anyOf:
            - type: boolean
            - type: 'null'
          title: Enforce
          default: false
        started:
          anyOf:
            - type: boolean
            - type: 'null'
          title: Started
          default: false
        question_raised:
          anyOf:
            - type: boolean
            - type: 'null'
          title: Question Raised
          default: false
        tasks:
          anyOf:
            - items:
                $ref: '#/components/schemas/DeepPlanningItem'
              type: array
            - type: 'null'
          title: Tasks
          default: []
      type: object
      title: DeepPlanning
    LLMTokens:
      properties:
        completion_tokens:
          anyOf:
            - type: integer
            - type: 'null'
          title: Completion Tokens
          default: 0
        prompt_tokens:
          anyOf:
            - type: integer
            - type: 'null'
          title: Prompt Tokens
          default: 0
        total_tokens:
          anyOf:
            - type: integer
            - type: 'null'
          title: Total Tokens
          default: 0
      type: object
      title: LLMTokens
    LLMReasoningEffort:
      type: string
      enum:
        - low
        - medium
        - high
        - xhigh
      title: LLMReasoningEffort
    ValidationError:
      properties:
        loc:
          items:
            anyOf:
              - type: string
              - type: integer
          type: array
          title: Location
        msg:
          type: string
          title: Message
        type:
          type: string
          title: Error Type
      type: object
      required:
        - loc
        - msg
        - type
      title: ValidationError
    User-Output:
      properties:
        id:
          anyOf:
            - type: string
            - type: 'null'
          title: Id
        first_name:
          anyOf:
            - type: string
            - type: 'null'
          title: First Name
        last_name:
          anyOf:
            - type: string
            - type: 'null'
          title: Last Name
        email:
          type: string
          title: Email
        additional_attributes:
          anyOf:
            - additionalProperties: true
              type: object
            - type: 'null'
          title: Additional Attributes
        role:
          anyOf:
            - type: string
            - type: 'null'
          title: Role
          default: member
        is_super_admin:
          anyOf:
            - type: boolean
            - type: 'null'
          title: Is Super Admin
          default: false
        timezone:
          anyOf:
            - type: string
            - type: 'null'
          title: Timezone
        display_name:
          type: string
          title: Display Name
          readOnly: true
      type: object
      required:
        - email
        - display_name
      title: User
      description: |-
        Represents the details of a user.

        Attributes:
            id (Optional[str]): The unique identifier of the user. Defaults to None.
            first_name (Optional[str]): The first name of the user. Defaults to None.
            last_name (Optional[str]): The last name of the user. Defaults to None.
            email (str): The email address of the user. This field is required.
            additional_attributes (Optional[dict]): Possible additional parameters for the assistant's service.
            role (Optional[str]): The user's role in the organization. Defaults to "member".
            is_super_admin (Optional[bool]): Whether the user is a super admin. Defaults to False.
            timezone (Optional[str]): The user's timezone in IANA format (e.g. "America/New_York"). Defaults to None.
    DeepPlanningItem:
      properties:
        id:
          type: string
          title: Id
        title:
          type: string
          title: Title
        description:
          anyOf:
            - type: string
            - type: 'null'
          title: Description
        input:
          anyOf:
            - {}
            - type: 'null'
          title: Input
        output:
          anyOf:
            - {}
            - type: 'null'
          title: Output
        tool_name:
          anyOf:
            - type: string
            - type: 'null'
          title: Tool Name
        completed:
          anyOf:
            - type: boolean
            - type: 'null'
          title: Completed
          default: false
      type: object
      required:
        - id
        - title
      title: DeepPlanningItem
  securitySchemes:
    APIKeyHeader:
      type: apiKey
      description: API Key for authentication
      in: header
      name: x-api-key

````