Creates a session instance that binds an agent with a runtime environment. A full snapshot of the agent's latest version is captured at session creation. Session-level agent configuration overrides and MCP request headers are supported.
Prerequisites
Endpoint and authentication configuration must be completed. For details, see Overview and authentication. Before creating a session, you need an agent and an environment. For details, see Agent and Environment. To mount a Bailian memory store, the Bailian memory service must be activated.
Endpoint
POST /sessions
Request bodyagentobject (Required)The bound agent reference object; pass only id to simply reference the agent. Can pin a version and optionally establish session-level overrides—see Session-level configuration overridesenvironment_id string (Required)The bound environment ID. If the environment has pre-installed packages configured, warm-up may be required after creation; a preparing environment cannot be bound to sessions (returns invalid_parameter), so wait for warm-up to completesandbox object (Optional)Sandbox snapshot persistence configuration. If omitted, the original session creation behavior is preserved
Properties enable_snapshot boolean (Required)Whether to enable snapshot persistence for the current session. Required when sandbox is passedsnapshot_persistent_seconds integer (Conditional)Retention period of a single snapshot in seconds, ranging from 86400 to 2592000 (1–30 days). Required when enable_snapshot=truestring (Optional)Session title for easy identification in listsresources array (Optional)Resources to mount at creation.
File items
Memory items
array of string (Optional)Vault ID list. Injects credentials from the specified vaults into the session runtime environment, allowing the agent to access the corresponding secretsmetadata object (Optional)Custom key-value pairs for business use; does not affect model behaviorenvironment_variables object (Optional)Environment variables injected into the session runtime as string key-value pairs; sandbox code can read them directly by namemcp_configs array (Optional)Session-level MCP request headers, independent of agent—see Session-level MCP request headers. Defaults to an empty array when omitted
When one session binds multiple vaults, secret_name values must be unique across vaults and must not share a prefix (for example, API_KEY and API_KEY_V2). Otherwise, session creation returns 400 invalid_parameter ("Invalid request"). | Request exampleBailian Memory Mount ExampleTo mount a Bailian memory store, setprovider to agentstudio_memory and provide entity_id:memory_store_id is the Bailian memory store ID; the memstore_ prefix is not required. For default behavior of rules and profile schemas, see Memory Store.To enable sandbox persistence, add sandbox to the request body, for example: |
Session-level configuration overrides
The agent object can simply reference the agent, or carry override fields to establish session-level overrides: only the configuration snapshot of the current session is modified—the agent itself is never changed, and multiple sessions can reuse the same agent with different customizations. The object fields are:
| Field | Required | Type | Description |
|---|---|---|---|
type | No | string | agent (reference only) or agent_with_overrides (reference with overrides). When carrying override fields, agent_with_overrides must be set explicitly; otherwise invalid_parameter is returned |
id | Yes | string | Agent ID |
version | No | integer | Agent version to pin; the current latest version is used when omitted. Returns not_found if the version does not exist |
system | No | string / null | Overrides the system prompt. Inherits the agent's source configuration when omitted; an empty string explicitly uses an empty prompt |
tools | No | array / null | Overrides the tool configuration; same structure as tools in Create Agent |
mcp_servers | No | array / null | Overrides the mounted MCP server list; same structure as mcp_servers in Create Agent |
skills | No | array / null | Overrides the mounted skill list; same structure as skills in Create Agent |
| Value | Effect |
|---|---|
| Omitted | Inherit the agent's source configuration |
null | Do not establish an override; inherit the source configuration |
[] (array fields) | Explicitly override to empty |
"" (only for system) | Explicitly use an empty prompt |
Session-level MCP request headers
mcp_configs injects custom headers into every request to the specified MCP servers, commonly used to pass tenant IDs or auth tokens. Each item contains mcp_server_name (MCP server name) and headers (key-value pairs where both key and value are strings). Passing a non-existent MCP server name does not raise an error; the configuration simply has no effect. For the per-session workflow and examples, see Custom MCP and Skills.
Full request example for creating a session:
Response fieldsThe response is a Session object with the following fields:idstringSession ID, format sesn_<ULID>type stringFixed as sessionstatus stringSession status: idle / running / terminated. Newly created sessions are idleagent objectFull agent configuration snapshot, including id/version/name/model/system/tools, etc.; session-level overrides are merged into the snapshotenvironment_id stringThe bound environment IDresources arrayMounted resource list.
File items
Memory items
Bailian memory items
Built-in memory items
string / objectSame as request bodysandbox objectSandbox snapshot persistence configuration. Omitted when not configured
Properties enable_snapshot booleanWhether snapshot persistence is enabled for the current sessionsnapshot_persistent_seconds integerRetention period of the configured snapshot in seconds; omitted when not configuredstring / nullArchive time; null when not archivedcreated_at / updated_at stringCreation/last update time, ISO 8601request_id stringUnique identifier for this requestenvironment_variables objectEnvironment variables injected into the session runtime as string key-value pairs; sandbox code can read them directly by nameA Bailian memory mount returns only the rule and profile schema IDs selected at creation; no default rules or profile schemas are added, and memory bodies, profile values, and full rule definitions are not returned.This response does not contain mcp_configs; query it via Get Session. | Response exampleThe session details embed a full agent snapshot. |
Sandbox configuration response example
When a session is created with sandbox persistence enabled, sandbox returns only the configuration:
Configuration lock
A full snapshot of the agent's latest version is captured at session creation (embedded in the agent field of the session details). Subsequent edits to the agent only affect new sessions; existing sessions continue using the configuration from the snapshot. Session-level configuration is currently API-only; sessions created in the console are read-only snapshots. Session-level configuration can be modified after creation—see Update Session.