Create a session, bind it to an agent and runtime environment, and submit the first input.
Endpoint
POST /agents/sessions
A non-streaming request returns 201 JSON; with stream=true, the response is 201 text/event-stream. A 201 means the session and first input were accepted, not that the turn has completed.
Request body
| Field | Required | Type | Description |
|---|---|---|---|
environment | Yes | object | Runtime environment, see below |
agent_id | Conditional | string / null | Saved agent ID; up to 64 characters |
agent | Conditional | object | Inline agent configuration, when provided with agent_id, acts as session-level overrides, and fields not overridden keep the saved values |
input | Conditional | string or array / null | First input. A string is shorthand for one user text message; an empty string is rejected |
metadata | No | object / null | Metadata, string key-value pairs, up to 16 items; key up to 64 characters, value up to 512 characters; null values are not accepted |
stream | No | boolean | Whether to return an SSE stream, when true, the create response is an SSE stream that ends after the session becomes idle or failed, omitted is false, and null is invalid |
vault_ids | No | array<string> / null | Vaults to attach, up to 10, omitted or null becomes [] |
agent.model | Conditional | string | Model ID; required for inline creation; can be inherited when referencing a saved agent. The create precheck allows up to 256 characters, and the model must match a model available in the current workspace. For available models, see Available models for creating an agent. |
agent.instructions | No | string / null | System instructions, up to 1,048,576 Unicode characters; merged into the system prompt at execution |
agent.name | No | string / null | Display name; removed from the execution configuration |
agent.reasoning.effort | No | string / null | Reasoning effort, none, minimal, low, medium, high, xhigh, max, or ultra; returns none when unspecified |
agent.reasoning.summary | No | string / null | Reasoning summary type, auto, concise, detailed, or none; returns auto when unspecified. The string none differs from JSON null |
agent.text.format | No | object / null | Text format configuration; only type: text is accepted; unknown fields are rejected, omitted or null becomes {"type":"text"} |
agent.text.verbosity | No | string / null | Output verbosity, only medium is accepted, omitted or null becomes medium |
agent.tools | No | array | Tool list, omitted becomes an empty list, an explicit null is not accepted. Up to 32 function tools and 16 MCP servers |
agent.multi_agent | No | object / null | Multi-agent configuration, omitted or null disables subagents, when the object is present, enabled can be omitted and defaults to false, an explicit null is rejected |
agent.multi_agent.max_concurrent_subagents | No | integer | Maximum concurrent subagents, an explicit value requires enabled=true and must be 1..32, an explicit null is rejected, when enabled, omitted defaults to 6, when disabled, the returned value is null |
agent.tools[].function.type | Yes | string | Tool type, fixed as function |
agent.tools[].function.name | Yes | string | Function name, 1-64 bytes with characters A-Za-z0-9_.:-; must be unique within the agent |
agent.tools[].function.description | Yes | string | Function description, must be non-empty after trimming and no more than 1024 bytes |
agent.tools[].function.parameters | Yes | object | Parameter definition; must be a JSON Schema with type: object |
agent.tools[].mcp.type | Yes | string | Tool type, fixed as mcp |
agent.tools[].mcp.server_label | Yes | string | MCP server label, 1-64 bytes and unique within the agent |
agent.tools[].mcp.transport | Yes | object | HTTP or stdio transport configuration |
agent.tools[].mcp.allowed_tools | No | array<string> / null | Allowed tool names; null means no filter |
agent.tools[].mcp.connection_origin | No | string | Connection origin, service or environment, defaults to service |
agent.tools[].mcp.required | No | boolean | Whether MCP initialization must succeed |
agent.tools[].mcp.transport.type | Yes | string | Transport type, http or stdio |
agent.tools[].mcp.transport.server_url | Conditional | string | MCP server URL; service accepts only Model Studio locator URLs (/api/v1/mcps/{code}/mcp); environment accepts ordinary HTTP addresses reachable from the environment |
agent.tools[].mcp.transport.headers | No | object<string,string> / null | Additional HTTP headers |
agent.tools[].mcp.transport.authorization | No | string / null | Used only for the HTTP branch with connection_origin=environment |
agent.tools[].mcp.transport.command | Conditional | string | Command to start the server |
agent.tools[].mcp.transport.cwd | Conditional | string | Absolute path; stdio requires an environment |
agent.tools[].mcp.transport.args | No | array<string> | Command arguments |
agent.tools[].web_search.type | Yes | string | Tool type, fixed as web_search |
input[].role | Yes | string | Message role, fixed as user |
input[].type | No | string | Message type, if set, only message |
input[].content | Yes | array | Message content; cannot be empty |
input[].content[].type | Yes | string | Content part type; input_text or input_image |
input[].content[].text | Conditional | string | Text content; up to 1,048,576 characters; the same item cannot also contain image_url |
input[].content[].image_url | Conditional | string | Image URL; up to 1,048,576 characters; the same item cannot also contain text; URL reachability is not guaranteed |
Environment
| Form | Description |
|---|---|
{"type":"none"} | No sandbox; no built-in shell or file tools |
{"type":"openai_hosted"} | Create a managed environment with the inline configuration |
{"type":"openai_hosted","environment_id":"..."} | Reference an existing environment; do not add files, skills, or other configuration at the same time |
self_hosted is not supported. Creating a session that references the environment can fail before preparation completes; confirm the status is ready before using the environment.
When referencing an existing environment,
vault_ids must match the vault_ids used to create the environment. You cannot add new vaults on the session.Agent configuration
agent is used for inline creation or to override a saved agent. When agent_id is also provided, top-level replacement applies: omitted fields keep the saved values, and an explicit null follows the field's own rule. The final effective configuration runs the same precheck as an inline configuration.
Example with both fields: {"agent_id":"agent_xxx","agent":{"instructions":"Reply with OK only"},"environment":{"type":"none"},"input":"Reply with OK only"}.
agent.id is generated in the response or comes from a saved agent and cannot be specified inline. Unknown fields such as agent.metadata, compaction, and experimental are rejected. A tool of one type must not carry fields from another branch; a boolean null is not equivalent to an omitted field.
Message and content
When input is an array, each item is a user message. Unknown fields in message and content are rejected.
Unknown top-level fields are rejected. The request body limit is 16 MiB. After removing instructions, the compact JSON of agent + metadata + environment is limited to 1,310,720 bytes.
Request example
Response example
Response fields
null when the fields are incomplete. The status in the create response can be in_progress. HTTP 201 means the session and first input were accepted, not that the turn succeeded; use the turn's terminal state or the SSE agent.session.idle event to determine the end of the turn.
| Field | Type | Description |
|---|---|---|
id | string | Session ID; reuse it for follow-up turns |
object | string | Session object type, fixed as agent.session |
created_at | integer | Creation time in Unix seconds |
last_active_at | integer | Last activity time in Unix seconds; not a version number or lock |
status | string | Session status: idle, in_progress, requires_action, or failed |
agent | object | Effective agent snapshot. |
agent.id | string | Agent ID; inline configurations return a session snapshot ID, not a reusable saved agent, and it cannot be retrieved through Agent GET |
agent.model | string | Model ID |
agent.instructions | string / null | Custom instructions |
agent.name | string / null | Display name |
agent.reasoning | object / null | Reasoning configuration, contains effort and summary |
agent.text | object / null | Text output configuration; contains format and verbosity |
agent.tools | array | Tool configuration |
agent.multi_agent | object / null | Subagent configuration; contains enabled and max_concurrent_subagents |
environment | object | Safe environment summary; only type for none. |
environment.type | string | Environment type; none or openai_hosted |
environment.id | string | Environment ID, omitted when there is no authoritative value |
environment.status | string | Environment status: omitted when there is no authoritative value |
environment.environment_template_id | string | Environment template ID, omitted when there is no authoritative value |
environment.workspace_directory | string | Workspace directory, omitted when there is no authoritative value |
environment.error | string / null | Environment error description, omitted when there is no authoritative value |
environment.desktop | object / null | Desktop settings; contains enabled |
environment.capability_directories | array<string> / null | Capability discovery directories |
environment.network | object / null | Network policy; contains access and allowed_domains |
environment.packages | object / null | Package lists; contains npm, python, and system |
environment.files | array | Safe environment file metadata |
environment.skills | array | Safe skill metadata |
environment.plugins | array | Plugin information; [] for managed environments |
metadata | object | Session metadata; an empty value is {} |
vault_ids | array<string> | Vault IDs recorded at creation |
required_actions | array | Function calls that need client handling; [] when empty. |
required_actions[].type | string | Item type, fixed as function_call |
required_actions[].turn_id | string | Public turn ID |
required_actions[].call_id | string | Function call ID |
required_actions[].name | string | Function name |
required_actions[].arguments | object | Parsed call arguments |
error | string / null | Session-level error message, different from a turn's structured error |
usage | object / null | Token usage. |
usage.input_tokens | integer | Number of input tokens |
usage.input_tokens_details | object | Input token details. |
usage.input_tokens_details.cached_tokens | integer | Number of input tokens that hit the cache |
usage.output_tokens | integer | Number of output tokens |
usage.output_tokens_details | object | Output token details. |
usage.output_tokens_details.reasoning_tokens | integer | Number of reasoning tokens |
usage.total_tokens | integer | Total number of tokens |
spend_control | object | Spend control; returned only when a historical record has a value; creation and update do not accept writes. |
spend_control.limit | integer | Spending limit in cents |
spend_control.consumed | integer / null | Consumed amount in cents |
Error codes
Business errors return an error object with type, code, message, and param. Common errors:
| HTTP status | Error type / code | Solution |
|---|---|---|
400 | invalid_request_error / missing_required_parameter | Provide environment, for example {"type":"none"} or {"type":"openai_hosted"} |
400 | invalid_request_error / specific code depends on the field | Provide input for the applicable case; provide agent.model when there is no agent_id |
400 | invalid_request_error / invalid_type, invalid_value, unknown_parameter, invalid_json | Remove unknown fields, fix field types and enum values, and send valid JSON |
400 | invalid_request_error / specific code depends on the field | Shorten metadata, vault_ids, or the string field as described |
401 | authentication_error / invalid_api_key | Send a valid Authorization: Bearer $DASHSCOPE_API_KEY header |
404 | not_found_error / not_found_error | Use a resource ID that exists in the current workspace |
502 | server_error / agents_api_upstream_unavailable | Retry later. If it keeps failing, submit a ticket with the x-request-id from the response headers |
503 | server_error / specific service error code | Retry later. If it keeps failing, submit a ticket with x-request-id |