Get the full Managed Agents workflow running in five minutes: create an Agent and Environment, open a Session, submit a task, and stream the results.
Prerequisites
-
You have activated Model Studio and created an API Key (
sk-xxx), and obtained your workspace ID (ws_xxxxxxxxxxxx). For details, see Overview and authentication. -
Export the API Key and Endpoint as environment variables. The following examples reference them directly:
export DASHSCOPE_API_KEY="sk-xxx" export AGENTSTUDIO_URL="https://ws_xxxxxxxxxxxx.cn-beijing.maas.aliyuncs.com/api/v1/agentstudio"
Workflow
A complete task execution consists of five steps:
-
Create an Agent: Define the model and system prompt to obtain an
agent_xxx. An Agent is typically created once and reused. -
Create an Environment: Define the runtime environment to obtain an
env_xxx. An Environment is typically created once and reused. -
Create a Session: Bind an Agent to an Environment to obtain a
sesn_xxx. Create a new Session for each task. -
Send an Event: Submit a task instruction to the Session, which triggers the Agent to enter the
runningstate. -
Subscribe to SSE: Receive execution results via an event stream until the Session returns to
idle.
Install the SDK
Before using the Python or Java SDK, install the corresponding DashScope package. The Managed Agents module requires Python SDK v1.26.2 or later, and Java SDK v2.22.24 or later. If you have an older version installed, re-run the install command to upgrade.
pip install dashscope<dependency>
<groupId>com.alibaba</groupId>
<artifactId>dashscope-sdk-java</artifactId>
</dependency>End-to-end example
The following example creates a code-generation Agent, opens a Session, submits a coding task, and streams the execution results.
## 1. Create an Agent
AGENT_ID=$(curl -s -X POST "$AGENTSTUDIO_URL/agents" \
-H "Authorization: Bearer $DASHSCOPE_API_KEY" \
-H "Content-Type: application/json" \
-d '{
"name": "quickstart-coder",
"model": {"id": "qwen-plus"},
"system": "You are a Python expert. When the user submits a coding task, output concise, runnable Python code directly."
}' | jq -r '.id')
echo "Agent: $AGENT_ID"
## 2. Create an Environment
ENV_ID=$(curl -s -X POST "$AGENTSTUDIO_URL/environments" \
-H "Authorization: Bearer $DASHSCOPE_API_KEY" \
-H "Content-Type: application/json" \
-d '{"name": "quickstart-env", "config": {"type": "cloud"}}' | jq -r '.id')
echo "Environment: $ENV_ID"
## 3. Create a Session
SESSION_ID=$(curl -s -X POST "$AGENTSTUDIO_URL/sessions" \
-H "Authorization: Bearer $DASHSCOPE_API_KEY" \
-H "Content-Type: application/json" \
-d "{\"agent\": \"$AGENT_ID\", \"environment_id\": \"$ENV_ID\"}" | jq -r '.id')
echo "Session: $SESSION_ID"
## 4. Submit a task
curl -X POST "$AGENTSTUDIO_URL/sessions/$SESSION_ID/events" \
-H "Authorization: Bearer $DASHSCOPE_API_KEY" \
-H "Content-Type: application/json" \
-d '{
"input": [
{"role": "user", "type": "message",
"content": [{"type": "text", "text": "Write a Python function that takes a list of integers and returns a sorted list with duplicates removed."}]}
]
}'
## 5. Subscribe to the SSE event stream to receive results
curl -N "$AGENTSTUDIO_URL/sessions/$SESSION_ID/events/stream" \
-H "Authorization: Bearer $DASHSCOPE_API_KEY" \
-H "Accept: text/event-stream"import os
from dashscope.agentstudio import Client, user_message
client = Client(
api_key=os.environ["DASHSCOPE_API_KEY"],
workspace="ws_xxxxxxxxxxxx",
region="cn-beijing",
)
# 1. Create an Agent
agent = client.agents.create(
name="quickstart-coder",
model="qwen-plus",
system_prompt="You are a Python expert. When the user submits a coding task, output concise, runnable Python code directly.",
)
# 2. Create an Environment
env = client.environments.create(name="quickstart-env", config={"type": "cloud"})
# 3. Create a Session
session = client.sessions.create(agent=agent.id, environment_id=env.id)
print(f"Agent: {agent.id}, Environment: {env.id}, Session: {session.id}")
# 4. Submit a task
client.sessions.events.send(
session.id, events=[user_message("Write a Python function that takes a list of integers and returns a sorted list with duplicates removed.")]
)
# 5. Stream execution results
with client.sessions.events.stream(session.id) as stream:
for event in stream:
print(event, flush=True)
if event.session_status in ("idle", "terminated"):
breakimport com.alibaba.dashscope.agentstudio.*;
import com.alibaba.dashscope.agentstudio.model.*;
import com.alibaba.dashscope.agentstudio.param.*;
import java.util.Collections;
// 1. Create a client (automatically reads the DASHSCOPE_API_KEY environment variable)
AgentStudioClient client = AgentStudioClient.builder()
.workspace("ws_xxxxxxxxxxxx")
.build();
// 2. Create an Agent
Agent agent = client.agents().create(AgentCreateParam.builder()
.name("quickstart-coder")
.model("qwen-plus")
.instructions("You are a Python expert. When the user submits a coding task, output concise, runnable Python code directly.")
.build());
// 3. Create an Environment
Environment env = client.environments().create(EnvironmentCreateParam.builder()
.name("quickstart-env")
.config(Map.of("type", "cloud"))
.build());
// 4. Create a Session
Session session = client.sessions().create(SessionCreateParam.builder()
.agentId(agent.getId())
.environmentId(env.getId())
.build());
System.out.println("Agent: " + agent.getId() + ", Environment: " + env.getId() + ", Session: " + session.getId());
// 5. Submit a task and stream execution results
try (AgentStudioEventStream stream = client.sessions().events().stream(session.getId())) {
client.sessions().events().send(session.getId(),
Collections.singletonList(ClientEvents.userMessage("Write a Python function that takes a list of integers and returns a sorted list with duplicates removed.")));
for (Message event : stream) {
System.out.print(event);
}
}