Best practices for integrating DAS Agent with the Chat API
The Chat API is an asynchronous interface for DAS Agent that supports knowledge-based Q&A, performance diagnostics, and multi-turn conversations. It returns the agent's reasoning process and final answer as a Server-Sent Events (SSE) stream. This topic describes how to integrate the Chat API by using the Java, Python, and Go SDKs, with complete examples for SSE event parsing and multi-turn conversations.
Prerequisites
DAS Agent is activated, and the region of the managed instance matches the country or region of DAS Agent. The instance is bound to DAS Agent.
The latest version of the Alibaba Cloud DAS SDK is installed.
The region is set to
cn-shanghaiand the endpoint is set todas.cn-shanghai.aliyuncs.com.The
ALIBABA_CLOUD_ACCESS_KEY_IDandALIBABA_CLOUD_ACCESS_KEY_SECRETenvironment variables are configured, or the Alibaba Cloud default credential chain is used.
The Chat API is a paid interface that is billed based on the number of input and output characters. For more information, see DAS Agent billing.
Core events
The SSE stream follows the ag-ui protocol. The following table describes the main event types.
Event type | Key fields | Description |
|
| Indicates that the task has started. Marks the beginning of the chat session. |
|
| Indicates that the task has ended. No more events are produced after this event. |
|
| Marks the beginning of a text message. |
|
| Contains an incremental text fragment. Concatenate the |
|
| Marks the end of the text message. |
|
| A heartbeat or status event from the agent, such as |
|
| Indicates that the agent has initiated a tool call, such as |
|
| Streams tool parameters as JSON text fragments. Concatenate the |
|
| Indicates that all tool parameters have been sent and the tool is about to execute. |
|
| Returns the tool execution result. The |
Typical event sequence
The following example uses the prompt "Apply SQL throttling to instance rm-uf63bopu77b*******" to illustrate the complete SSE event sequence.
1. Task start
After the server receives the request, it sends a RUN_STARTED event that marks the beginning of the session. The client can use this event to start a timer or initialize the UI.
{"Type":"RUN_STARTED","RunId":"58abc22e-5742-4e9b-802e-5f060a0ca2e3"}2. User input echo (ignorable)
The server echoes the user message as a text message with Role=user. The client typically does not need to display this message. Filter by Role to skip it.
{"Type":"TEXT_MESSAGE_START","Role":"user","MessageId":"20d2bc27-1644-47e5-8816-b0e764e84a6e"}
{"Type":"TEXT_MESSAGE_CONTENT","MessageId":"20d2bc27-1644-47e5-8816-b0e764e84a6e","Delta":"Apply SQL throttling to instance rm-uf63bopu77b*******"}
{"Type":"TEXT_MESSAGE_END","MessageId":"20d2bc27-1644-47e5-8816-b0e764e84a6e"}3. Agent heartbeat (ignorable)
During the model's reasoning phase, ACTIVITY_DELTA events serve as heartbeat signals. Skip these events in the client.
{"Type":"ACTIVITY_DELTA","ActivityType":"waiting_for_agent_thinking","Patch":[],"MessageId":""}4. Agent analysis output (Role=assistant)
The model streams its reasoning through TEXT_MESSAGE_CONTENT.Delta events. Concatenate the Delta values for the same MessageId to assemble the full response.
{"Type":"TEXT_MESSAGE_START","Role":"assistant","MessageId":"36aaafdb-ea7f-4475-bad7-136e12117959"}
{"Type":"TEXT_MESSAGE_CONTENT","MessageId":"36aaafdb-ea7f-4475-bad7-136e12117959","Delta":"I need to check the SQL execution status of this instance first to determine which SQL statements require throttling. Let me query the recent SQL audit logs.\n\n"}
{"Type":"TEXT_MESSAGE_END","MessageId":"36aaafdb-ea7f-4475-bad7-136e12117959"}5. Agent tool call
When the agent invokes an external tool such as das_api, the events follow this sequence: TOOL_CALL_START → multiple TOOL_CALL_ARGS → TOOL_CALL_END → TOOL_CALL_RESULT.
Call start
{"Type":"TOOL_CALL_START","ToolCallId":"call_0fd4d07290b54dd7b7064cc2","ToolCallName":"das_api","ParentMessageId":"36aaafdb-ea7f-4475-bad7-136e12117959"}Streaming parameters
Multiple TOOL_CALL_ARGS.Delta events must be concatenated by ToolCallId. After concatenation, parse the result as a complete JSON object:
{
"command": "execute",
"api_name": "getdassqlloghotdata",
"parameters": {
"instance_id": "rm-uf63bopu77b*******",
"start": "2026-03-05T15:54:16+08:00",
"end": "2026-03-05T16:54:16+08:00",
"max_records_per_page": 10,
"include_fields": ["sql_text", "execution_count", "avg_consume"],
"security_risk": "LOW"
}
}Parameter end and execution result
{"Type":"TOOL_CALL_END","ToolCallId":"call_0fd4d07290b54dd7b7064cc2"}
{"Type":"TOOL_CALL_RESULT","ToolCallId":"call_0fd4d07290b54dd7b7064cc2","MessageId":"36aaafdb-ea7f-4475-bad7-136e12117959","Content":"API call succeeded. Response: ..."}6. Task end
A RUN_FINISHED event indicates the end of the SSE stream. The client can stop the timer and close the connection.
{"Type":"RUN_FINISHED","RunId":"58abc22e-5742-4e9b-802e-5f060a0ca2e3"}SDK examples
Usage notes
For multi-turn conversations, always pass the same
SessionId. Otherwise, the model cannot retain context from previous turns.The SSE stream contains heartbeat events (
ACTIVITY_DELTA). Skip these events in the client.The Chat API is billed based on the number of input and output characters. During development, start with simple test queries to avoid unexpected charges.