Skip to main content

Manage Sessions

After a session is created, its execution flow can be controlled via a state machine, tool call approvals, and interruption mechanisms—and archived or deleted upon task completion.

State Machine

The session status appears at the top of the page. The table below outlines the trigger conditions, next states, and available actions for each status:
StatusTrigger ConditionNext StatusAvailable Actions
idle (interactive: stop_reason is null / end_turn / retries_exhausted)Session just created (null), one round finished (end_turn), or retries exhausted (retries_exhausted)On receiving a message → running; On archiving or deletion → terminatedSend message, attach files, archive, delete
idle (stop_reason=requires_action)An always_ask tool approval was triggered and is awaiting a decisionOnce all decisions are collected → running; interrupt → ends the current approval batchSubmit approvals (tool_approval_response), interrupt (interrupt); a plain message cannot continue the turn
runningMessage received and agent begins processingOn completion → idle; On approval trigger → idle (requires_action); On unrecoverable error → terminatedInterrupt
terminatedArchived, deleted, or unrecoverable error occurredTerminal state—no further transitions possibleView event history; create a new session to continue
While in idle, the session_status event (and GET /sessions/{session_id}) carries a stop_reason indicating why. stop_reason only determines the interaction state while the session is idle: null when just created with no end reason yet, otherwise end_turn, retries_exhausted, or requires_action. It is null while running; terminated is a terminal state — clients should not depend on or interpret its stop_reason. The client should map the four idle projections to UI actions per the table below, deciding interactivity by whether it is requires_action — not by idle alone:
stop_reasonMeaningPlain message allowedApprove / interrupt
nullSession just created, not yet processedAllowedNo pending approval
end_turnThe model voluntarily ended the turnAllowedNo pending approval
retries_exhaustedRetries exhausted, the turn is overAllowedNo pending approval
requires_actionPending always_ask approvals; carries pending_batch_id / pending_call_idsForbidden (sending one alone yields pending_tool_approval_unresolved)Submit approvals / send interrupt
  • Interactive idle (null / end_turn / retries_exhausted): no approval barrier exists; you may send a plain message to start a new turn.
  • requires_action: you may only submit approvals or send interrupt. After partial decisions the state stays requires_action and pending_call_ids shrinks to the remaining calls; only once all are collected does it return to running. Sending a plain message alone here does not continue processing and yields pending_tool_approval_unresolved; the legal interrupt + plain message combination is handled as defined in Session Event Stream.
When driving UI from state, decide interactivity by stop_reason, not by idle alone: only requires_action disables plain messages and enables approve/interrupt; every other interactive idle (null / end_turn / retries_exhausted) allows a new turn's message. Never treat retries_exhausted as permanently un-chattable.
While a session is running, you can mount, list, and unmount file resources dynamically via the API—see Mount Session Resource.

Tool Calls

The agent autonomously decides when and which tool to invoke based on system prompts and the current message. All tools—built-in, MCP, or skills—are executed within the runtime environment bound to the session, and their invocation process is visible in real time via the event panel.

Invocation Flow

  1. Agent Decision: The model outputs a tool call instruction. The event panel displays a Tool Call event containing the tool name and parameters.
  2. Approval Branch:
    • always_allow tools: proceed directly to execution without blocking.
    • always_ask tools: emit a tool_approval_request, and the session enters idle (stop_reason=requires_action) awaiting decisions. Approved (allow) calls run only after all decisions for the batch are collected; a partial decision leaves the session in requires_action, and no always_ask tool in the batch runs until then. A deny call does not run and returns a tool output with is_error set to true, with deny_message as the error content sent back to the model. Approval covers only the always_ask set — always_allow tools may execute before approval completes.
  3. Execution: The tool runs inside a sandbox; its output is sent back to the model.
  4. Response or Continue: The model decides whether to reply or initiate another tool call, based on the tool’s output.

Archiving and Deletion

Sessions support two distinct operations: archiving and deletion.
  • Archiving: Changes the session status to terminated (a terminal state), while preserving the full event history for future reference. Recommended for completed sessions.
  • Deletion: Permanently removes all session metadata, event history, and internally copied resources—this action is irreversible. Use archiving instead if you need to retain event history.
To archive a session programmatically, see the Session API.
curl -X POST "https://{workspace_id}.cn-beijing.maas.aliyuncs.com/api/v1/agentstudio/sessions/sesn_xxx/archive" \
  -H "Authorization: Bearer $DASHSCOPE_API_KEY"
To delete a session via API:
curl -X DELETE "https://{workspace_id}.cn-beijing.maas.aliyuncs.com/api/v1/agentstudio/sessions/sesn_xxx" \
  -H "Authorization: Bearer $DASHSCOPE_API_KEY"

Next Steps

Agent Development
Agent Context
Agent Governance & Evolution
Store
Application Marketplace